How to: Compress and Extract Files
The 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:
The following examples show some of the functions you can perform when working with compressed files.
This example shows how to create and extract a compressed file that has a .zip file name extension by using the 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 ZipFile class, you must reference the System.IO.Compression.FileSystem assembly in your project.
Imports System.IO Imports System.IO.Compression Module Module1 Sub Main() Dim startPath As String = "c:\example\start" Dim zipPath As String = "c:\example\result.zip" Dim extractPath As String = "c:\example\extract" ZipFile.CreateFromDirectory(startPath, zipPath) ZipFile.ExtractToDirectory(zipPath, extractPath) End Sub End Module
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 ZipArchive class to access an existing .zip file, and the ZipArchiveEntry class to inspect the individual entries in the compressed file. It uses an extension method (ExtractToFile) for the ZipArchiveEntry object. The extension method is available in the System.IO.Compression.ZipFileExtensions class. To use the ZipFileExtensions class, you must reference the System.IO.Compression.FileSystem assembly in your project.
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 next example uses the 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.
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
You can also use the GZipStream and DeflateStream classes to compress and decompress data. They use the same compression algorithm. Compressed 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 GZipStream. This example shows how to compress and decompress a directory of files by using the GZipStream class.
Imports System.IO Imports System.IO.Compression Module Module1 Sub Main() Dim directoryPath As String = "c:\users\public\reports" Dim directorySelected As DirectoryInfo = New DirectoryInfo(directoryPath) For Each fileToCompress As FileInfo In directorySelected.GetFiles() Compress(fileToCompress) Next For Each fileToDecompress As FileInfo In directorySelected.GetFiles("*.gz") Decompress(fileToDecompress) Next End Sub Private Sub Compress(ByVal fileToCompress As FileInfo) Using originalFileStream As FileStream = fileToCompress.OpenRead() If (File.GetAttributes(fileToCompress.FullName) And FileAttributes.Hidden) <> FileAttributes.Hidden And fileToCompress.Extension <> ".gz" Then Using compressedFileStream As FileStream = File.Create(fileToCompress.FullName + ".gz") Using compressionStream As GZipStream = New GZipStream(compressedFileStream, CompressionMode.Compress) originalFileStream.CopyTo(compressionStream) Console.WriteLine("Compressed {0} from {1} to {2} bytes.", _ fileToCompress.Name, fileToCompress.Length.ToString(), compressedFileStream.Length.ToString()) End Using End Using End If End Using End Sub Private Sub Decompress(ByVal fileToDecompress As FileInfo) Using originalFileStream As FileStream = fileToDecompress.OpenRead() Dim currentFileName As String = fileToDecompress.FullName Dim newFileName = currentFileName.Remove(currentFileName.Length - fileToDecompress.Extension.Length) Using decompressedFileStream As FileStream = File.Create(newFileName) Using decompressionStream As GZipStream = New GZipStream(originalFileStream, CompressionMode.Decompress) decompressionStream.CopyTo(decompressedFileStream) Console.WriteLine("Decompressed: {0}", fileToDecompress.Name) End Using End Using End Using End Sub End Module