SoFunction
Updated on 2025-03-07

C# Example method of decompressing files


#region Unzip File zip format rar format
        /// <summary>
///Decompress the file
        /// </summary>
/// <param name="fileFromUnZip">File path before decompression (absolute path)</param>
/// <param name="fileToUnZip">Decompressed file directory (absolute path)</param>
        public static void UnpackFile(string fileFromUnZip, string fileToUnZip)
        {
//Get compression type
            string unType = ((".") + 1, 3).ToLower();
            switch (unType)
            {
                case "rar":
                    UnRar(fileFromUnZip, fileToUnZip);
                    break;
                case "zip":
                    UnZip(fileFromUnZip, fileToUnZip);
                    break;
            }
        }
//Decompress files in rar format
        private static void UnRar(string fileFromUnZip, string fileToUnZip)
        {
using (Process Process1 = new Process())// Open a process and perform decompression work
            {
string ServerDir = ["UnpackFile"].ToString();//The installation path of the rar tool is required WinRAR. //Example in: C:\Program Files (x86)\WinRAR\
                = false;
                = true;
                = true;
                = true;
                = true;
                = ServerDir;
                = " x -inul -y " + fileFromUnZip + " " + fileToUnZip;
();//Decompression starts
                ();
                ();
            }
        }
// Unzip the zip file
        public static void UnZip(string fileFromUnZip, string fileToUnZip)
        {
            ZipInputStream inputStream = new ZipInputStream((fileFromUnZip));
            ZipEntry theEntry;
            while ((theEntry = ()) != null)
            {
                fileToUnZip += "/";
                string fileName = ();
                string path = (fileToUnZip) + "/";
// (path);// Generate the decompressed directory
                if (fileName != )
                {
FileStream streamWriter = (path + fileName);//Decompress the file to the specified directory
                    int size = 2048;
                    byte[] data = new byte[2048];
                    while (true)
                    {
                        size = (data, 0, );
                        if (size > 0)
                        {
                            (data, 0, size);
                        }
                        else
                        {
                            break;
                        }
                    }
                    ();
                }
            }
            ();
        }
        #endregion