Unzipping Archives Programmatically in .NET
Unzipping compressed archives is a common task in software development. Despite the misconception that .zip and .gz files are interchangeable, they are distinct file formats with different compression methods. This article explains how to unzip .zip files programmatically in .NET, without relying on external libraries or applications.
Using .NET 4.5
From .NET 4.5 onward, the framework includes a built-in class, System.IO.Compression.ZipFile, for handling .zip archives. To use this class:
Create a ZIP File:
string startPath = @"c:\example\start"; string zipPath = @"c:\example\result.zip"; // Create a ZIP file from the specified directory System.IO.Compression.ZipFile.CreateFromDirectory(startPath, zipPath);
Extract a ZIP File:
string extractPath = @"c:\example\extract"; // Extract the contents of the ZIP file to the specified directory System.IO.Compression.ZipFile.ExtractToDirectory(zipPath, extractPath);
Add a reference to the assembly System.IO.Compression.FileSystem.dll for the ZipFile class to be available.
Additional Considerations
The above is the detailed content of How to Programmatically Unzip .ZIP Files in .NET?. For more information, please follow other related articles on the PHP Chinese website!