0

This code is a useful way of creating a zip archive on the fly:

(I didn't think it would be helpful to post the entire code here)

http://www.codeproject.com/Articles/209731/Csharp-use-Zip-archives-without-external-libraries

I'm using it to zip text files for downloading via Response.BinaryWrite.

This is my implementation:

files and fileNames are each a List<string>

        // add the files to a zip
        var str = new MemoryStream();
        using (var arc = ZipArchive.OpenOnStream(str))
        {
            for (int i = 0; i < files.Count(); i++)
            {
                using (var fs = arc.AddFile(fileNames[i]).GetStream(FileMode.Open, FileAccess.ReadWrite))
                {
                    System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
                    Byte[] bytes = encoding.GetBytes(files[i]);
                    fs.Write(bytes, 0, bytes.Length);
                }
            }
        }

        result = str.GetBuffer();

        // return the zip
        return result;

My question is, can anyone think of a way to store the files in separate folders in the archive?

Thanks for any help!

CompanyDroneFromSector7G
  • 4,291
  • 13
  • 54
  • 97
  • 2
    http://stackoverflow.com/questions/15133626/creating-directories-in-a-ziparchive-c-sharp-net-4-5 in short, it's the same thing as an `entry`, except that you include a path. See http://msdn.microsoft.com/en-us/library/system.io.compression.ziparchive.aspx – Robert Harvey Jun 29 '13 at 16:29
  • Thanks - it's that easy! I'm such a dumbass! – CompanyDroneFromSector7G Jun 29 '13 at 16:37

0 Answers0