Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

c# file class that can be added to zip archive.

Posted on 2012-12-29
5
Medium Priority
?
438 Views
Last Modified: 2012-12-29
I have a console application that creates several text files on disk, then adds them to a zip file.

What I would like to do is:
1. create the files as "virtual" files, ie create a class for each file with name and contents
2. write these class files to a zip archive.

Any thoughts appreciated.
0
Comment
Question by:dhenderson12
  • 2
  • 2
5 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38729572
So you want to serialize instances of this class to a file? Can you expand your explanation a tad?
0
 

Author Comment

by:dhenderson12
ID: 38729587
hey, kaufmed, thanks for the reply.

I was thinking that I don't really need a physical file on disk if I can create a class with a name property and a content property (the other file properties are not useful to me).  I could create this class instead of a physical file, and then add it to a zip archive as if it was a physical
file.  I'm just not sure how to go about it.

If I create the  physical file then I would need to delete it after it is added to the zip archive.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 38729616
Ah. Here's an example using DotNetZip (which you can install using NuGet).

using System.Text;
using Ionic.Zip;

namespace ConsoleApplication42
{
    class Program
    {
        static void Main(string[] args)
        {
            TheClass instance = new TheClass() { Name = "example.txt", Content = "Hello World!" };
            ZipFile zip = new ZipFile("example.zip");
            byte[] data = Encoding.ASCII.GetBytes(instance.Content);

            zip.AddEntry(instance.Name, data);
            zip.Save();
        }
    }

    public class TheClass
    {
        public string Name { get; set; }
        public string Content { get; set; }
    }
}

Open in new window


Screenshot
0
 

Author Comment

by:dhenderson12
ID: 38729709
kaufmed, you rock!  This works perfectly and is just what I was looking for.  Thanks for help.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question