Updating a ZIP file
suggest changeTo update a ZIP file, open it with ZipArchiveMode.Update. ZipArchive and CreateEntryFromFile were added in .NET Framework 4.5. CreateEntryFromFile is in System.IO.Compression.FileSystem.
using (FileStream fs = new FileStream("archive.zip", FileMode.Open))
using (ZipArchive archive = new ZipArchive(fs, ZipArchiveMode.Update))
{
// Add file to root
archive.CreateEntryFromFile("test.txt", "test.txt");
// Add file to subfolder
archive.CreateEntryFromFile("test.txt", "symbols/test.txt");
}
Inside that same using block, you can write an entry directly. archive is the ZipArchive opened above:
var entry = archive.CreateEntry("createentry.txt");
using(var writer = new StreamWriter(entry.Open()))
{
writer.WriteLine("Test line");
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents