Available for: UI for ASP.NET MVC | UI for ASP.NET AJAX | UI for Blazor | UI for WPF | UI for WinForms | UI for Silverlight | UI for Xamarin | UI for WinUI | UI for ASP.NET Core | UI for .NET MAUI

New to Telerik Document Processing? Download free 30-day trial

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. 
    } 
} 
Using stream As Stream = File.Open("test.zip", FileMode.Open) 
    Using archive As ZipArchive = ZipArchive.Update(stream, Nothing) 
        ' Display the list of the files in the selected zip file using the ZipArchive.Entries property. 
    End Using 
End Using 

Add Entry

In order to add a new entry into the ZIP archive, you should perform the following steps:

  1. Use CreateEntry() method of the ZipArchive object to create a new entry.

  2. Open the entry to obtain a stream for writing.

  3. Write the necessary information into the entry.

  4. 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(); 
} 
Using entry As ZipArchiveEntry = archive.CreateEntry("text.txt") 
    Dim writer As New StreamWriter(entry.Open()) 
    writer.WriteLine("Hello world!") 
    writer.Flush() 
End Using 

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(); 
} 
Dim entry As ZipArchiveEntry = archive.GetEntry("text.txt") 
If entry IsNot Nothing Then 
    entry.Delete() 
End If 

Update Entry

In order to update an existing entry in the ZIP archive, you should perform the following steps:

  1. Use GetEntry() method of the ZipArchive object to obtain existing entry.

  2. Open entry to get a stream for reading/writing.

  3. Read/Write the necessary information from/to the entry.

  4. 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(); 
} 
Dim entry As ZipArchiveEntry = archive.GetEntry("text.txt") 
If entry IsNot Nothing Then 
    Dim entryStream As Stream = entry.Open() 
    Dim reader As New StreamReader(entryStream) 
    Dim content As String = reader.ReadToEnd() 
 
    entryStream.Seek(0, SeekOrigin.End) 
    Dim writer As New StreamWriter(entryStream) 
    writer.WriteLine("Updated line.") 
    writer.Flush() 
End If 

See Also

In this article