Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add files to an existing zip archive

Tags:

How can I add some file (almost always a single .csv file) to an existing zip file?

like image 350
user3408397 Avatar asked Mar 11 '14 23:03

user3408397


People also ask

Can you drag and drop into a ZIP file?

The drag and drop interface is one of the most natural ways to use WinZip®. Using drag and drop, you can create, open, update, extract from, print, and email Zip files--and more.


1 Answers

Since you are in .NET 4.5, you can use the ZipArchive (System.IO.Compression) class to achieve this. Here is the MSDN documentation: (MSDN).

Here is their example, it just writes text, but you could read in a .csv file and write it out to your new file. To just copy the file in, you would use CreateFileFromEntry, which is an extension method for ZipArchive.

using (FileStream zipToOpen = new FileStream(@"c:\users\exampleuser\release.zip", FileMode.Open)) {    using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update))    {        ZipArchiveEntry readmeEntry = archive.CreateEntry("Readme.txt");        using (StreamWriter writer = new StreamWriter(readmeEntry.Open()))        {            writer.WriteLine("Information about this package.");            writer.WriteLine("========================");        }    } } 
like image 144
BradleyDotNET Avatar answered Oct 09 '22 14:10

BradleyDotNET