Home > Backend Development > C++ > How to Programmatically Unzip .ZIP Files in .NET?

How to Programmatically Unzip .ZIP Files in .NET?

Patricia Arquette
Release: 2025-01-05 14:42:44
Original
813 people have browsed it

How to Programmatically Unzip .ZIP Files in .NET?

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:

  1. 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);
    Copy after login
  2. 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);
    Copy after login

Add a reference to the assembly System.IO.Compression.FileSystem.dll for the ZipFile class to be available.

Additional Considerations

  • For versions of .NET prior to 4.5, third-party libraries can be used for ZIP file manipulation.
  • Some archives may be password-protected. In such cases, additional mechanisms are required to provide the password.
  • Ensure that the paths specified for extraction and creation are valid and accessible.

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template