Solved

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

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

729 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