ZipArchive
Class
Definition
Represents a package of compressed files in the zip archive format.
public class ZipArchive : IDisposable
- Inheritance
-
ZipArchive
- Implements
Inherited Members
System.Object
Examples
The first example shows how to create a new entry and write to it by using a stream.
using System;
using System.IO;
using System.IO.Compression;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
using (FileStream zipToOpen = new FileStream(@"c:\users\exampleuser\release.zip", FileMode.Open))
{
using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update))
{
ZipArchiveEntry readmeEntry = archive.CreateEntry("Readme.txt");
using (StreamWriter writer = new StreamWriter(readmeEntry.Open()))
{
writer.WriteLine("Information about this package.");
writer.WriteLine("========================");
}
}
}
}
}
}
Imports System.IO
Imports System.IO.Compression
Module Module1
Sub Main()
Using zipToOpen As FileStream = New FileStream("c:\users\exampleuser\release.zip", FileMode.Open)
Using archive As ZipArchive = New ZipArchive(zipToOpen, ZipArchiveMode.Update)
Dim readmeEntry As ZipArchiveEntry = archive.CreateEntry("Readme.txt")
Using writer As StreamWriter = New StreamWriter(readmeEntry.Open())
writer.WriteLine("Information about this package.")
writer.WriteLine("========================")
End Using
End Using
End Using
End Sub
End Module
The following example shows how to open a zip archive and iterate through the collection of entries.
using System;
using System.IO;
using System.IO.Compression;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string zipPath = @"c:\example\start.zip";
string extractPath = @"c:\example\extract";
using (ZipArchive archive = ZipFile.OpenRead(zipPath))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
if (entry.FullName.EndsWith(".txt", StringComparison.OrdinalIgnoreCase))
{
entry.ExtractToFile(Path.Combine(extractPath, entry.FullName));
}
}
}
}
}
}
Imports System.IO
Imports System.IO.Compression
Module Module1
Sub Main()
Dim zipPath As String = "c:\example\start.zip"
Dim extractPath As String = "c:\example\extract"
Using archive As ZipArchive = ZipFile.OpenRead(zipPath)
For Each entry As ZipArchiveEntry In archive.Entries
If entry.FullName.EndsWith(".txt", StringComparison.OrdinalIgnoreCase) Then
entry.ExtractToFile(Path.Combine(extractPath, entry.FullName))
End If
Next
End Using
End Sub
End Module
The third example shows how to use extension methods to create a new entry in a zip archive from an existing file and extract the archive contents. You must reference the System.IO.Compression.FileSystem assembly to execute the code.
using System;
using System.IO;
using System.IO.Compression;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
string zipPath = @"c:\users\exampleuser\start.zip";
string extractPath = @"c:\users\exampleuser\extract";
string newFile = @"c:\users\exampleuser\NewFile.txt";
using (ZipArchive archive = ZipFile.Open(zipPath, ZipArchiveMode.Update))
{
archive.CreateEntryFromFile(newFile, "NewEntry.txt");
archive.ExtractToDirectory(extractPath);
}
}
}
}
Imports System.IO
Imports System.IO.Compression
Module Module1
Sub Main()
Dim zipPath As String = "c:\users\exampleuser\end.zip"
Dim extractPath As String = "c:\users\exampleuser\extract"
Dim newFile As String = "c:\users\exampleuser\NewFile.txt"
Using archive As ZipArchive = ZipFile.Open(zipPath, ZipArchiveMode.Update)
archive.CreateEntryFromFile(newFile, "NewEntry.txt", CompressionLevel.Fastest)
archive.ExtractToDirectory(extractPath)
End Using
End Sub
End Module
Remarks
The methods for manipulating zip archives and their file entries are spread across three classes: ZipFile, ZipArchive, and ZipArchiveEntry.
| To | Use |
|---|---|
| Create a zip archive from a directory | ZipFile.CreateFromDirectory |
| Extract the contents of a zip archive to a directory | ZipFile.ExtractToDirectory |
| Add new files to an existing zip archive | ZipArchive.CreateEntry |
| Retrieve a file from a zip archive | ZipArchive.GetEntry |
| Retrieve all the files from a zip archive | ZipArchive.Entries |
| Open a stream to a single file contained in a zip archive | ZipArchiveEntry.Open |
| Delete a file from a zip archive | ZipArchiveEntry.Delete |
When you create a new entry, the file is compressed and added to the zip package. The CreateEntry method enables you to specify a directory hierarchy when adding the entry. You include the relative path of the new entry within the zip package. For example, creating a new entry with a relative path of AddedFolder\NewFile.txt creates a compressed text file in a directory named AddedFolder.
If you reference the System.IO.Compression.FileSystem assembly in your project, you can access three extension methods (from the ZipFileExtensions class) for the ZipArchive class: CreateEntryFromFile, CreateEntryFromFile, and ExtractToDirectory. These extension methods enable you to compress and decompress the contents of the entry to a file. The System.IO.Compression.FileSystem assembly is not available for Windows 8.x Store apps. In Windows 8.x Store apps, you can compress and decompress files by using the DeflateStream or GZipStream class, or you can use the Windows Runtime types Compressor and Decompressor.
Constructors
| ZipArchive(Stream) |
Initializes a new instance of the ZipArchive class from the specified stream. |
| ZipArchive(Stream, ZipArchiveMode) |
Initializes a new instance of the ZipArchive class from the specified stream and with the specified mode. |
| ZipArchive(Stream, ZipArchiveMode, Boolean) |
Initializes a new instance of the ZipArchive class on the specified stream for the specified mode, and optionally leaves the stream open. |
| ZipArchive(Stream, ZipArchiveMode, Boolean, Encoding) |
Initializes a new instance of the ZipArchive class on the specified stream for the specified mode, uses the specified encoding for entry names, and optionally leaves the stream open. |
Properties
| Entries |
Gets the collection of entries that are currently in the zip archive. |
| Mode |
Gets a value that describes the type of action the zip archive can perform on entries. |
Methods
| CreateEntry(String) |
Creates an empty entry that has the specified path and entry name in the zip archive. |
| CreateEntry(String, CompressionLevel) |
Creates an empty entry that has the specified entry name and compression level in the zip archive. |
| Dispose() |
Releases the resources used by the current instance of the ZipArchive class. |
| Dispose(Boolean) |
Called by the Dispose() and Finalize() methods to release the unmanaged resources used by the current instance of the ZipArchive class, and optionally finishes writing the archive and releases the managed resources. |
| GetEntry(String) |
Retrieves a wrapper for the specified entry in the zip archive. |