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

Getting Started

RadZipLibrary allows you to load data from existing ZIP archives or create and edit ZIPs that can be used by other applications. You can also create ZIP archives in memory or use a stream to get data and add it to a ZIP archive.

If you still don't have Telerik Document Processing installed, check the First Steps topic to learn how you can obtain the packages through the different suites.

The ZIP archive is represented by ZipArchive class. It can be used in 3 modes:

  • Read: Allows reading of the existing ZIP archive. In this mode it is not possible to add or update archive entries.

  • Create: Allows creation of a new archive. In this mode you can add new entries in the archive but cannot read or update content of the entries which have been written already.

  • Update: Allows update of the existing ZIP archive. In this mode you can add new entries, read and update existing entries.

Assembly References

The libraries support .NET 6/.NET 8/.NET 9. .NET Standard-compatible binaries are available as well. The assemblies for .NET Standard don't include 'Windows' in their names. (e.g. Telerik.Zip.dll).

.NET Framework .NET Standard-compatible
Telerik.Windows.Zip.dll Telerik.Zip.dll

The binaries compatible with .NET Standard are distributed with the packages targeting .NET Standard and .NET Core. You can obtain the assemblies through the UI for ASP.NET Core, UI for Blazor and UI for Xamarin suites. There are NuGet packages as well that you can access if you have a license for one of the above mentioned suites.

Open Zip Archive

The code snippet from Example 1 demonstrates how to open existing Zip archive using the ZipArchive class.

Example 1: Open archive

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

The archive variable holds the files that are compressed in the selected zip. You can access the list of these files through the ZipArchive.Entries property. It holds a collection of ZipArchiveEntry elements - the elements that describe the files archived in the zip file. You can use these elements to get the name of the compressed file, its uncompressed and compressed size and other file attributes.

Create Zip Archive

Example 2 shows how to create a new Zip archive using the ZipArchive class and place a text file in it.

Example 2: Create archive

using (Stream stream = File.Open("test.zip", FileMode.Create)) 
{ 
    using (ZipArchive archive = ZipArchive.Create(stream, null)) 
    { 
        using (ZipArchiveEntry entry = archive.CreateEntry("text.txt")) 
        { 
            StreamWriter writer = new StreamWriter(entry.Open()); 
            writer.WriteLine("Hello world!"); 
            writer.Flush(); 
        } 
    } 
} 
Using stream As Stream = File.Open("test.zip", FileMode.Create) 
    Using archive As ZipArchive = ZipArchive.Create(stream, Nothing) 
        Using entry As ZipArchiveEntry = archive.CreateEntry("text.txt") 
            Dim writer As New StreamWriter(entry.Open()) 
            writer.WriteLine("Hello world!") 
            writer.Flush() 
        End Using 
    End Using 
End Using 

If you use StreamWriter to write content to the stream, you should call the Flush() method in order to flush the data to the stream.

Do not close the stream opened by the ZipArchiveEntry.Open() method. Otherwise the result is unpredictable.

The constructor of ZipArchive lets you specify whether you would like to keep the stream associated to the instance open. If you decide to set the leaveOpen parameter to false, the underlying stream will be closed once the ZipArchive instance is disposed. In case you need to continue working with that stream (to send it as a responce, for example), you should pass true for the leaveOpen parameter.

Example 3: Create archive in a MemoryStream

Stream memoryStream = new MemoryStream(); 
 
// The third parameter of ZipArchive's constructor specifies that the stream should be left open  
using (ZipArchive archive = ZipArchive.Create(memoryStream, null)) 
{ 
    using (ZipArchiveEntry entry = archive.CreateEntry("text.txt")) 
    { 
        Stream entryStream = entry.Open(); 
        StreamWriter writer = new StreamWriter(entryStream); 
        writer.WriteLine("Hello world!"); 
        writer.Flush(); 
    } 
} 
 
// Save memoryStream to a file or send it to client 

See Also

In this article