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 do not have Telerik Document Processing installed, check the First Steps topic to learn how you can get the packages through the different suites.
The ZipArchive class represents the ZIP archive. You can use it in the following modes:
-
Read: Allows reading from an existing ZIP archive. In this mode, you cannot add or update archive entries. -
Create: Allows creation of a new archive. In this mode, you can add new entries to the archive but cannot read or update entries that are already written. -
Update: Allows updating an existing ZIP archive. In this mode, you can add new entries, read existing entries, and update existing entries.
Required References
Find the required references in the ZipLibrary NuGet packages section.
Open ZIP Archive
The following code snippet shows how to open an existing ZIP archive with the ZipArchive class.
Example 1: Open Archive
using (Stream stream = File.Open("test.zip", FileMode.Open))
{
using (ZipArchive archive = ZipArchive.Create(stream))
{
//Display the list of the files in the selected zip file using the ZipArchive.Entries property.
}
}
The archive variable holds the compressed files in the selected ZIP. You can access these files through the ZipArchive.Entries property. It holds a collection of ZipArchiveEntry elements that describe the archived files. 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 with the ZipArchive class and add a text file to 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();
}
}
}
If you use
StreamWriterto write content to the stream, call theFlush()method 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 set whether to keep the stream associated with the instance open. If you set the leaveOpen parameter to false, the library closes the underlying stream when the ZipArchive instance is disposed. If you need to continue working with that stream (to send it as a response, for example), 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
For more examples, go to the Developer Focused Examples section of the library.