SoFunction
Updated on 2025-03-07

C# about the packaging of zip compression and decompression help class with source code download


/// <summary>
/// Compress files or folders
        /// </summary>
/// <param name="_depositPath">Storage path of compressed files, such as C:\\windows\</param>
        /// <returns></returns>
        public bool CompressionZip(string _depositPath)
        {
            bool result = true;
            FileStream fs = null;
            try
            {
                ZipOutputStream ComStream = new ZipOutputStream((_depositPath));
(9);        //Compression level
                foreach (string path in AbsolutePaths)
                {
//If it is a directory
                    if ((path))
                    {
                        ZipFloder(path, ComStream, path);
                    }
else if ((path))//If it is a file
                    {
                         fs = (path);
                        byte[] bts = new byte[];
                        (bts, 0, );
                        ZipEntry ze = new ZipEntry(new FileInfo(path).Name);
(ze);
(bts, 0, );  //Write bytes
                    }
                }
(); // End compression
                ();
            }
            catch (Exception ex)
            {
                if (fs != null)
                {
                    ();
                }
                errorMsg = ;
                result = false;
            }
            return result;
        }
//Compress folder
        private void ZipFloder(string _OfloderPath, ZipOutputStream zos, string _floderPath)
        {
            foreach (FileSystemInfo item in new DirectoryInfo(_floderPath).GetFileSystemInfos())
            {
                if (())
                {
                    ZipFloder(_OfloderPath, zos, );
                }
else if (())//If it is a file
                {
                    DirectoryInfo ODir = new DirectoryInfo(_OfloderPath);
                    string fullName2 = new FileInfo().FullName;
string path = + (, - );//Get relative directory
                    FileStream fs = (fullName2);
                    byte[] bts = new byte[];
                    (bts, 0, );
                    ZipEntry ze = new ZipEntry(path);
(ze);
(bts, 0, );  //Write bytes
                }
            }
        }