Writing to an existing ZipEntry.

Jul 6, 2011 at 2:31 PM

Currently, I am adding a new entry to a ZipFile both when trying to create a new entry and when updating an existing entry. When doing the latter, I am having to first remove the entry from the ZipFile, and then re-add it with a stream containing the updated data. I was just wondering if there was a way, or if there is going to be a way, where you could directly write into an existing entry. So something like:

private ZipFile mZipFile;

public void WriteIntoExistingEntry(string entryName, Stream stream)
{
    // Get the entry
    ZipEntry entry = mZipFile[entryName];

    // Get a stream to write into the entry
    using (var entryStream = entry.OutputStream)
    {
        // Copy the new data across
        stream.CopyTo(entryStream);
    }
}

Kind regards

Coordinator
Jul 6, 2011 at 3:37 PM

Try the ZipFile.UpdateEntry method that accepts a stream argument.

It updates the named entry in the zipfile, replacing the content for that entry with the content read from the given stream. Read all about it in the documentation.

In your case the code might look like this:

public void WriteIntoExistingEntry(string entryName, Stream stream)
{
    mZipFile.UpdateEntry(entryName, stream);
}

The content from the stream is read, then compressed and written into the zipfile when your application calls ZipFile.Save(). Therefore the stream must remain open and readable (not disposed) until after the call to ZipFile.Save().