How can I add some file (almost always a single .csv file) to an existing zip file?
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("========================");
}
}
}
ZipArchive
class was added in .NET 4.5. –
Poland CreateEntryFromFile
method: msdn.microsoft.com/en-us/library/hh485720(v=vs.110).aspx If you wanted to post a new quesiton, I'd be happy to provide a code sample. I don't feel that this use case is all that important to this question. –
Lingcod For creating, extracting, and opening zip archives, we can use ZipFile Class with reference: System.IO.Compression.FileSystem. For .NET 4.5.2 and below, we also need to add reference: System.IO.Compression. Here's the method for adding files to zip:
public static void AddFilesToZip(string zipPath, string[] files)
{
if (files == null || files.Length == 0)
{
return;
}
using (var zipArchive = ZipFile.Open(zipPath, ZipArchiveMode.Update))
{
foreach (var file in files)
{
var fileInfo = new FileInfo(file);
zipArchive.CreateEntryFromFile(fileInfo.FullName, fileInfo.Name);
}
}
}
foreach
to support folder by using CreateEntryFromFolder
method –
Porky CreateEntryFromFile
at the documentation of the ZipFile class. This method, and two other useful ones (ExtractToDirectory
and ExtractToFile
) are defined namely as extension methods in the ZipFileExtensions class. –
Antitoxin CreateEntryFromDirectory
method and work recursively just fine.. –
Rheumy The easiest way is to get DotNetZip at http://dotnetzip.codeplex.com/
Adding files can be as easy as
String[] filenames = { @"ReadMe.txt",
@"c:\data\collection.csv" ,
@"c:\reports\AnnualSummary.pdf"
} ;
using ( ZipFile zip = new ZipFile() )
{
zip.AddFiles(filenames);
zip.Save("Archive.zip");
}
Other sorts of updates are just as trivial:
using (ZipFile zip = ZipFile.Read("ExistingArchive.zip"))
{
// update an existing item in the zip file
zip.UpdateItem("Portfolio.doc");
// remove an item from the zip file
zip["OldData.txt"].RemoveEntry();
// rename an item in the zip file
zip["Internationalization.doc"].FileName = "i18n.doc";
// add a comment to the archive
zip.Comment = "This zip archive was updated " + System.DateTime.ToString("G");
zip.Save();
}
Edited To Note: DotNetZip used to live at Codeplex. Codeplex has been shut down. The old archive is still [available at Codeplex][1]. It looks like the code has migrated to Github:
- https://github.com/DinoChiesa/DotNetZip. Looks to be the original author's repo.
- https://github.com/haf/DotNetZip.Semverd. This looks to be the currently maintained version. It's also packaged up an available via Nuget at https://www.nuget.org/packages/DotNetZip/
© 2022 - 2024 — McMap. All rights reserved.