Update ZipArchive
With RadZipLibrary you can update existing ZIP archive in order to add new entries to it, delete or update existing ones.
The ZipArchive class provides three modes: Read, Create and Update. More information on creating and reading an archive is available here.
The code snippet from Example 1 opens a ZIP archive in update mode using ZipArchive class.
Example 1: Open for update
using (Stream stream = File.Open("test.zip", FileMode.Open))
{
using (ZipArchive archive = ZipArchive.Update(stream, null))
{
// Display the list of the files in the selected zip file using the ZipArchive.Entries property.
}
}
Add Entry
In order to add a new entry into the ZIP archive, you should perform the following steps:
-
Use CreateEntry() method of the ZipArchive object to create a new entry.
-
Open the entry to obtain a stream for writing.
-
Write the necessary information into the entry.
-
Dispose entry when all necessary information is written. In the Update mode this step is optional. You can omit it if you are going to add/delete/update other entries in the archive.
More information about ZipArchiveEntry you can find in ZipArchiveEntry help article.
Example 2: Add entry
using (ZipArchiveEntry entry = archive.CreateEntry("text.txt"))
{
StreamWriter writer = new StreamWriter(entry.Open());
writer.WriteLine("Hello world!");
writer.Flush();
}
Delete Entry
The ZipArchive class provides a GetEntry() method, which allows you access to a particular entry in the archive.
Example 3 shows how you could obtain an entry and delete it from the ZIP archive using the Delete() method.
Example 3: Delete entry
ZipArchiveEntry entry = archive.GetEntry("text.txt");
if (entry != null)
{
entry.Delete();
}
Update Entry
In order to update an existing entry in the ZIP archive, you should perform the following steps:
-
Use GetEntry() method of the ZipArchive object to obtain existing entry.
-
Open entry to get a stream for reading/writing.
-
Read/Write the necessary information from/to the entry.
-
Dispose entry when all necessary information is written. In the Update mode this step is optional. You can omit it if you are going to add/delete/update other entries in the archive.
Example 4: Update entry
ZipArchiveEntry entry = archive.GetEntry("text.txt");
if (entry != null)
{
Stream entryStream = entry.Open();
StreamReader reader = new StreamReader(entryStream);
string content = reader.ReadToEnd();
entryStream.Seek(0, SeekOrigin.End);
StreamWriter writer = new StreamWriter(entryStream);
writer.WriteLine("Updated line.");
writer.Flush();
}
Copy Entry
Starting with the Q2 2025 release, the RadZipLibrary introduces a new extension method CopyTo for the Stream class. This method allows you to copy data from one stream to another with an optional timeout parameter, providing more control over long-running operations. This is particularly useful when working with large files in ZIP archives.
The following example demonstrates how to use the CopyTo method to copy the contents of a large file from a ZIP archive to a memory stream with a specified timeout.
Example 5: Copy entry with timeout
using (Stream zipStream = File.Open("input.zip", FileMode.Open))
{
using (ZipArchive archive = ZipArchive.Read(zipStream))
{
ZipArchiveEntry entry = archive.Entries.ElementAt(2); // Access the third entry in the archive
using (Stream entryStream = entry.Open())
using (MemoryStream memoryStream = new MemoryStream())
{
// Copy the entry's content to the memory stream with a timeout of 5 seconds
entryStream.CopyTo(memoryStream);
}
}
}
This method ensures that the operation will not hang indefinitely, making it a valuable addition for scenarios involving large files or limited resources.