Solved

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

Posted on 2012-12-29
5
435 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

628 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