SoFunction
Updated on 2025-03-07

C# uses native to realize zip compression and decompression

zip is a very common compression package format. This article is mainly used to explain how to use code files or folders to compress zip compression packages and their decompression operations.
We use Microsoft's official implementation, so we don't need to install third-party component packages.

Remember when using itusing ;

/// <summary>
/// Compress the specified directory into a Zip file/// </summary>
/// <param name="folderPath">Folder Address D:/1/ </param>/// <param name="zipPath">zip address D:/ </param>public static void CompressDirectoryZip(string folderPath, string zipPath)
{
    DirectoryInfo directoryInfo = new(zipPath);

    if ( != null)
    {
        directoryInfo = ;
    }

    if (!)
    {
        ();
    }

    (folderPath, zipPath, , false);
}

inCompressionLevelIt is an enumeration that supports the following four types

enumerate value annotation
Optimal 0 The compression operation should balance the compression speed and output size in the optimal way.
Fastest 1 Even if the result file is not optionally compressed, the compression operation should be completed as soon as possible.
NoCompression 2 The file should not be compressed.
SmallestSize 3 The compression operation should create output as small as possible, even if the operation takes longer to complete.

My method is directly fixed here, and you can adjust it according to your personal needs.

/// &lt;summary&gt;
/// Compress the specified file into a Zip file/// &lt;/summary&gt;
/// <param name="filePath">File address D:/ </param>/// <param name="zipPath">zip address D:/ </param>public static void CompressFileZip(string filePath, string zipPath)
{

    FileInfo fileInfo = new FileInfo(filePath);
    string dirPath = ?.Replace("\\", "/") + "/";
    string tempPath = dirPath + () + "_temp/";
    if (!(tempPath))
    {
        (tempPath);
    }
    (tempPath + );
    CompressDirectoryZip(tempPath, zipPath);
    DirectoryInfo directory = new(path);
    if ()
    {
        //Set the folder attribute to normal, such as: read-only folder to normal         = ;

        (true);
    }
}

The logic of compressing a single file is to first copy the file we want to compress to a temporary directory, and then perform compression actions on the temporary directory. After the compression is completed, the temporary directory is deleted.

/// &lt;summary&gt;
/// Unzip the Zip file to the specified directory/// &lt;/summary&gt;
/// <param name="zipPath">zip address D:/</param>/// <param name="folderPath">Folder Address D:/1/</param>public static void DecompressZip(string zipPath, string folderPath)
{
    DirectoryInfo directoryInfo = new(folderPath);

    if (!)
    {
        ();
    }

    (zipPath, folderPath);
}

At this point, C# uses native to implement zip compression and decompression. I have finished explaining. If you don’t understand, you can comment or send me a private message below the article. Everyone is welcome to actively discuss and communicate. Interested friends can pay attention to a .NET basic framework project I am currently maintaining. The project address is as follows

/berkerdong/

/berkerdong/

This is the article about C# using native implementation of zip compression and decompression. For more related content on C# implementation of zip compression and decompression, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!