Solved

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

Posted on 2012-12-29
5
419 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 74

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
 
LVL 21

Expert Comment

by:Tapan Pattanaik
ID: 38729582
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 74

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now