2012-02-21 29 views
0

我有一個ASP.NET應用程序,用戶可以上傳ZIP文件。我試圖使用ICSharpZipLib提取文件(我也試過DotNetZip,但有同樣的問題)。ICSharpZipLib - unziping文件問題

此zip文件包含單個xml文件(壓縮前爲9KB)。

當我用桌面上的其他應用程序(7zip,Windows資源管理器)打開此文件時,它似乎沒問題。 我的解壓縮方法拋出System.OutOfMemoryException,我不知道爲什麼。當我調試我unziping方法我注意到zipInputStreams'長度屬性會引發異常和不適:

Stream UnZipSingleFile(Stream memoryStream) 
     { 

      var zipInputStream = new ZipInputStream(memoryStream); 

      memoryStream.Position = 0; 

      zipInputStream.GetNextEntry(); 

      MemoryStream unzippedStream = new MemoryStream(); 

      int len; 
      byte[] buf = new byte[4096]; 
      while ((len = zipInputStream.Read(buf, 0, buf.Length)) > 0) 
      { 
       unzippedStream.Write(buf, 0, len); 
      } 

      unzippedStream.Position = 0; 
      memoryStream.Position = 0; 

      return unzippedStream; 
    } 

,這裏是我如何獲得unzippedStream的字符串:

string GetString() 
     { 
      var reader = new StreamReader(unzippedStream); 
      var result = reader.ReadToEnd(); 
      unzippedStream.Position = 0; 
      return result; 
     } 
+0

嘗試使用'string result = System.Text.Encoding.Default.GetString(unzippedStream);',因爲它可能是編碼的,並導致您在當前結果中看到的內容。 – 2012-02-21 14:19:29

+0

「unzippedStream」是您班級的一個屬性嗎? 'GetString()'怎麼樣?我想知道你是否有名字碰撞的地方。你可以發佈調用'GetString()'的方法嗎? – 2012-02-21 14:24:23

+0

你是怎麼調用這兩種方法的?你能舉一個例子嗎? – 2012-02-21 14:29:43

回答

0

從他們的維基:

「Sharpzip支持使用存儲和壓縮壓縮方法的Zip文件,並且還支持舊的(PKZIP 2.0)風格和AES加密」

你確定上傳的zip文件的格式可以接受SharpZipLib嗎?

+0

不,我不是,我不能。你有一定的意見。我不可能影響用戶選擇哪種格式。 – TrN 2012-02-21 14:39:26

+0

hm ... nope,這個是deflete – TrN 2012-02-21 14:47:17

+0

在他們的[例子](http://wiki.sharpdevelop.net/SharpZipLib-Zip-Samples.ashx)中,他們在使用ZipInputStream時使用StreamUtils.Copy來解壓縮。如果失敗,也許這種方法會回覆一些提示。 – Steve 2012-02-21 16:04:37