Permalink
Fetching contributors…
Cannot retrieve contributors at this time
67 lines (51 sloc) 4.5 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic helpviewer_keywords ms.assetid caps.latest.revision author ms.author manager
How to: Compress and Extract Files
03/30/2017
.net
dotnet-standard
article
I/O [.NET Framework], compression
compression
compress files
e9876165-3c60-4c84-a272-513e47acf579
19
mairaw
mairaw
wpickett

How to: Compress and Extract Files

The xref:System.IO.Compression namespace contains the following types for compressing and decompressing files and streams. You can also use these types to read and modify the contents of a compressed file:

  • xref:System.IO.Compression.ZipFile

  • xref:System.IO.Compression.ZipArchive

  • xref:System.IO.Compression.ZipArchiveEntry

  • xref:System.IO.Compression.DeflateStream

  • xref:System.IO.Compression.GZipStream

The following examples show some of the functions you can perform when working with compressed files.

Example

This example shows how to create and extract a compressed file that has a .zip file name extension by using the xref:System.IO.Compression.ZipFile class. It compresses the contents of a folder into a new .zip file and then extracts that content to a new folder. To use the xref:System.IO.Compression.ZipFile class, you must reference the System.IO.Compression.FileSystem assembly in your project.

[!code-csharpSystem.IO.Compression.ZipFile#1] [!code-vbSystem.IO.Compression.ZipFile#1]

Example

The next example shows how to iterate through the contents of an existing .zip file and extract files that have a .txt extension. It uses the xref:System.IO.Compression.ZipArchive class to access an existing .zip file, and the xref:System.IO.Compression.ZipArchiveEntry class to inspect the individual entries in the compressed file. It uses an extension method (xref:System.IO.Compression.ZipFileExtensions.ExtractToFile%2A) for the xref:System.IO.Compression.ZipArchiveEntry object. The extension method is available in the xref:System.IO.Compression.ZipFileExtensions?displayProperty=nameWithType class. To use the xref:System.IO.Compression.ZipFileExtensions class, you must reference the System.IO.Compression.FileSystem assembly in your project.

[!code-csharpSystem.IO.Compression.ZipArchive#1] [!code-vbSystem.IO.Compression.ZipArchive#1]

Example

The next example uses the xref:System.IO.Compression.ZipArchive class to access an existing .zip file, and adds a new file to the compressed file. The new file gets compressed when you add it to the existing .zip file.

[!code-csharpSystem.IO.Compression.ZipArchiveMode#1] [!code-vbSystem.IO.Compression.ZipArchiveMode#1]

Example

You can also use the xref:System.IO.Compression.GZipStream and xref:System.IO.Compression.DeflateStream classes to compress and decompress data. They use the same compression algorithm. Compressed xref:System.IO.Compression.GZipStream objects that are written to a file that has an extension of .gz can be decompressed by using many common tools in addition to the methods provided by xref:System.IO.Compression.GZipStream. This example shows how to compress and decompress a directory of files by using the xref:System.IO.Compression.GZipStream class.

[!code-csharpIO.Compression.GZip1#1] [!code-vbIO.Compression.GZip1#1]

See Also

xref:System.IO.Compression.ZipArchive
xref:System.IO.Compression.ZipFile
xref:System.IO.Compression.ZipArchiveEntry
xref:System.IO.Compression.DeflateStream
xref:System.IO.Compression.GZipStream
File and Stream I-O