C#調用7z實現文件的壓縮與解壓


C#調用7z實現文件的壓縮與解壓

1.關於7z

首先在這里先介紹一下7z壓縮軟件,7z是一種主流的 壓縮格式,它擁有極高的壓縮比。在計算機科學中,7z是一種可以使用多種壓縮算法進行數據壓縮的檔案格式。主要有以下特點:

  • 來源且模塊化的組件結構
  • 最高的壓縮比
  • 強大的AES-256加密
  • 可更改配置的壓縮算法
  • 支持操大文件
  • 支持多線程壓縮
  • 具有多種壓縮文件格式
2.解壓縮實現代碼
實現對文件的解壓縮方法是通過cmd命令,調用7z程式通過cmd命令實現對文件進行解壓和壓縮的操作,具體實現代碼如下:
  • 壓縮代碼
       public object CompressZipFile(string sourceFile, string destinationFile)
            {
                object resObj;
                Process process = new Process();
                string tempDestinationFile = "";
    
                try
                {
                    if (process == null)
                    {
                        process = new Process();
                    }
                    tempDestinationFile = destinationFile.ToLower().EndsWith(".zip") ? destinationFile : destinationFile + ".zip";
                    process.StartInfo.FileName = "cmd.exe";
                    string command1 = @"cd c:\progra~1\7-zip";
                    process.StartInfo.UseShellExecute = false;
                    process.StartInfo.RedirectStandardInput = true;
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.RedirectStandardError = true;
                    process.StartInfo.CreateNoWindow = true;
                    process.Start();
                    process.StandardInput.WriteLine(@"c:");
                    process.StandardInput.WriteLine(@"cd\");
                    process.StandardInput.WriteLine(command1);
                    process.StandardInput.WriteLine(@"7Z a -tzip " + destinationFile + " " + sourceFile);
                    process.StandardInput.WriteLine("exit");
    
                    string output = process.StandardOutput.ReadToEnd();
                    if (output.ToUpper().IndexOf("EVERYTHING IS OK") > -1)
                    {
                        resObj = new object[] { 0, "Compress file[" + destinationFile + "] OK!" };
                    }
                    else
                    {
                        resObj = new object[] { 1, "Compress File[" + destinationFile + "] Error!" };
                    }
    
                }
                catch (Exception ex)
                {
                    resObj = new object[] { 1, "Compress File[" + destinationFile + "] exception:" + ex.Message };
    
                }
                return resObj;
            }
  • 解壓代碼
       public object decompressFile(string zipFilepath, string FileDir)
            {
                object resObj;
                try
                {
                    string batfiledata = @"c:\progra~1\7-zip\7z.exe x " + zipFilepath + " -o" + FileDir + " -y";
    
                    Process process = new Process();
                    process.StartInfo.FileName = "cmd.exe";
                    process.StartInfo.Arguments = "/c " + batfiledata;
                    process.StartInfo.UseShellExecute = false;
                    process.StartInfo.RedirectStandardInput = true;
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.RedirectStandardError = true;
                    process.StartInfo.CreateNoWindow = true;
                    process.Start();
    
                    string output = process.StandardOutput.ReadToEnd();
                    if (output.ToUpper().IndexOf("EVERYTHING IS OK") > -1)
                    {
                        resObj = new object[] { 0, "DeCompress file[" + zipFilepath + "] OK!" };
                    }
                    else
                    {
                        resObj = new object[] { 1, "DeCompress File[" + zipFilepath + "] Error!" };
                    }
    
                }
                catch (Exception ex)
                {
                    resObj = new object[] { 1, ex.Message };
                }
    
                return resObj;
    
            }

     


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM