Updating a ZIP file

suggest change

To 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");
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents