Solved

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

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 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

11 Experts available now in Live!

Get 1:1 Help Now