SoFunction
Updated on 2025-03-07

c# Use DotNetZip to encapsulate the class to operate zip file (create/read/update) instance


/// <summary>
/// Zip operation is based on DotNetZip package
    /// </summary>
    public static class ZipUtils
    {
        /// <summary>
/// Get the ZIP compressed stream object of the specified input stream [the original stream object will not be changed]
        /// </summary>
        /// <param name="sourceStream"></param>
        /// <returns></returns>
        public static Stream ZipCompress(Stream sourceStream, string entryName = "zip")
        {
            MemoryStream compressedStream = new MemoryStream();
            if (sourceStream != null)
            {
                long sourceOldPosition = 0;
                try
                {
                    sourceOldPosition = ;
                    = 0;
                    using (ZipFile zip = new ZipFile())
                    {
                        (entryName, sourceStream);
                        (compressedStream);
                        = 0;
                    }
                }
                catch
                {
                }
                finally
                {
                    try
                    {
                        = sourceOldPosition;
                    }
                    catch
                    {
                    }
                }
            }
            return compressedStream;
        }


        /// <summary>
/// Get the ZIP decompression stream object of the specified byte array
/// The current method is only suitable for compressed packages with only one compressed file, that is, only the first compressed file in the compressed package is taken in the method
        /// </summary>
        /// <param name="sourceStream"></param>
        /// <returns></returns>
        public static Stream ZipDecompress(byte[] data)
        {
            Stream decompressedStream = new MemoryStream();
            if (data != null)
            {
                try
                {
                    MemoryStream dataStream = new MemoryStream(data);
                    using (ZipFile zip = (dataStream))
                    {
                        if ( > 0)
                        {
                            ().Extract(decompressedStream);
// ms will be operated in the Extract method. The Stream position must be reset to zero for subsequent use, otherwise it will cause no data to be read in the future.
// Perform a position reset action before returning to the Stream object
                            = 0;
                        }
                    }
                }
                catch
                {
                }
            }
            return decompressedStream;
        }

        /// <summary>
/// Compress ZIP file
/// Supports multiple files and multiple directories, or compresses them together with multiple files and multiple directories
        /// </summary>
/// <param name="list">File or directory collection to be compressed</param>
/// <param name="strZipName">Compressed file name</param>
/// <param name="IsDirStruct">Whether to compress according to directory structure</param>
/// <returns>Success: true/Failed: false</returns>
        public static bool CompressMulti(List<string> list, string strZipName, bool IsDirStruct)
        {
            try
            {
using (ZipFile zip = new ZipFile())//Set encoding to solve the garbled Chinese code when compressing files
                {
                    foreach (string path in list)
                    {
string fileName = (path);//Get directory name
//If it's a directory
                        if ((path))
                        {
if (IsDirStruct)//Compress according to directory structure
                            {
                                (path, fileName);
                            }
else//The files in the directory are compressed to the root directory of Zip
                            {
                                (path);
                            }
                        }
if ((path))//If it is a file
                        {
                            (path);
                        }
                    }
(strZipName);//Compression
                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
/// Unzip the ZIP file
        /// </summary>
/// <param name="strZipPath">ZIP file to be unzipped</param>
/// <param name="strUnZipPath">Decompressed directory</param>
/// <param name="overWrite">Whether to overwrite</param>
/// <returns>Success: true/Failed: false</returns>
        public static bool Decompression(string strZipPath, string strUnZipPath, bool overWrite)
        {
            try
            {
                ReadOptions options = new ReadOptions();
= ;//Set encoding to solve the garbled Chinese code when decompressing files
                using (ZipFile zip = (strZipPath, options))
                {
                    foreach (ZipEntry entry in zip)
                    {
                        if ((strUnZipPath))
                        {
                            strUnZipPath = ('.').First();
                        }
                        if (overWrite)
                        {
(strUnZipPath, );//Decompress the file, if it already exists, overwrite it
                        }
                        else
                        {
(strUnZipPath, );//Decompress the file, if it already exists, it will not be overwritten
                        }
                    }
                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }


    }