關於SharpZipLib的壓縮與解壓問題

來源:互聯網
上載者:User

關於SharpZipLib的壓縮與解壓縮的實現代碼,網路上有一堆,千遍一律,連注釋也一模一樣,一模一樣的文章拷來拷去??

我使用SharpZipLib.dll中遇到的問題是:利用SharpZipLib壓縮後產生的*.rar檔案,利用其可以正常解壓,但如果使用檔案右擊壓縮產生的*.RAR檔案,在解壓過程中出錯,具體報錯資訊:Wrong Local header signature: 0x21726152 ;但*.zip檔案可正常解壓。

具體壓縮、解壓代碼實現參照網路上的代碼,貼出概要代碼:

壓縮檔        public bool ZipFile(string dirPath, string zipFilePath, ref string error)
        {         
            try
            {
                string[] filenames = Directory.GetFiles(dirPath);
                using (ZipOutputStream zs = new ZipOutputStream(File.Create(zipFilePath)))
                {
                    zs.SetLevel(9);
                    byte[] buffer = new byte[4096];

                    foreach (string file in filenames)
                    {
                        ZipEntry entry = new ZipEntry(Path.GetFileName(file));
                        entry.DateTime = DateTime.Now;
                        zs.PutNextEntry(entry);

                        using (FileStream fs = File.OpenRead(file))
                        {
                            int sourceBytes;
                            do
                            {
                                sourceBytes = fs.Read(buffer, 0, buffer.Length);
                                zs.Write(buffer, 0, sourceBytes);
                            }
                            while (sourceBytes > 0);
                        }
                    }
                    zs.Finish();
                    zs.Flush();
                    zs.Close();
                } 
                return true;        
            }
            catch (Exception ex)
            {
                return false;
            }
           
        }

 

 
解壓檔案(出錯)  public bool DeCompress(string sourceFilePath, string destinationPath)
         {
             try
             {
                 using (ZipInputStream zs = new ZipInputStream(File.OpenRead(sourceFilePath)))
                 {
                     ZipEntry entry = null;
                    //解壓縮*.rar檔案運行至此處出錯:Wrong Local header signature: 0x21726152,解壓*.zip檔案不出錯 
                     while ((entry = zs.GetNextEntry()) != null)
                     {
                         string directoryName = Path.GetDirectoryName(entry.Name);
                         string fileName = Path.GetFileName(entry.Name);
                       
                         if (!string.IsNullOrEmpty(fileName))
                         {
                             using (FileStream streamWriter = File.Create(destinationPath + entry.Name))
                             {
                                 int size = 2048;
                                 byte[] data = new byte[size];
                                 while (true)
                                 {
                                     size = zs.Read(data, 0, data.Length);
                                     if (size > 0)
                                     {
                                         streamWriter.Write(data, 0, size);
                                     }
                                     else
                                     {
                                         break;
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
             catch (System.Exception)
             {
                 return false;
             }
             return true;
         }

 

如果需解壓*.rar的壓縮檔在網路也可以找到相關的實現代碼,概要代碼:

解壓檔案(正確)public bool DeCompressRAR(string sourceFilePath, string destinationPath)
         {
             try
             {
                 string SeverDir = @"D:\Program Files\WinRAR";//rar.exe的要目錄
                 Process ProcessDecompression = new Process();
                 ProcessDecompression.StartInfo.FileName = SeverDir + "\\rar.exe";

                 Directory.CreateDirectory(sourceFilePath);

                 ProcessDecompression.StartInfo.Arguments = " X " + sourceFilePath + " " + destinationPath;

                 ProcessDecompression.Start();

                 while (!ProcessDecompression.HasExited)
                 {
                     //nothing to do here.
                 }
                 return true;
             }
             catch (System.Exception)
             {
                 return false;
             }
         }

 

 

我本想利用FileUpload控制項將上傳的壓縮檔解壓後儲存至相對應的目錄並更新資料庫檔案目錄,後發現一些較好的用於上傳的開源軟體:如NeatUpload,SWFUpload可以較方便的實現我的需求,遂沒有過多糾纏於SharpZipLib,可能關於SharpZipLib的壓縮與解壓有其它用法,不能被我誤導,以上代碼是從網路上整合出來的,因為它太過於重複和散亂。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.