2010-12-14 67 views
5

我正在使用SharpZipLib解壓縮文件。我的代碼已經被很好地工作,除了爲zip文件都zipfiles我現在所提取...SharpZipLib - ZipException「System.ArgumentOutOfRangeException」 - 爲什麼我得到這個異常?

得到這個異常:

System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. 
Parameter name: length 

異常正以size = s.Read(data, 0, data.Length);

拋出Hereb是我的代碼...

public static void UnzipFile(string sourcePath, string targetDirectory) 
    { 
     try 
     { 
      using (ZipInputStream s = new ZipInputStream(File.OpenRead(sourcePath))) 
      { 
       ZipEntry theEntry; 
       while ((theEntry = s.GetNextEntry()) != null) 
       { 
        //string directoryName = Path.GetDirectoryName(theEntry.Name); 
        string fileName = Path.GetFileName(theEntry.Name); 

        if (targetDirectory.Length > 0) 
        { 
         Directory.CreateDirectory(targetDirectory); 
        } 

        if (fileName != String.Empty) 
        { 
         using (FileStream streamWriter = File.Create(targetDirectory + fileName)) 
         { 
          int size = 2048; 
          byte[] data = new byte[2048]; 
          while (true) 
          { 
           size = s.Read(data, 0, data.Length); 
           if (size > 0) 
           { 
            streamWriter.Write(data, 0, size); 
           } 
           else 
           { 
            break; 
           } 
          } 
         } 
        } 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      throw new Exception("Error unzipping file \"" + sourcePath + "\"", ex); 
     } 
    } 
+0

我剛剛試過你的例子,它對我的​​工作很好。 無論是zip包還是庫版本問題。 我使用'v1.1.4322' – 2010-12-14 07:21:41

+0

@Andrey:沒有這樣的SharpZipLib版本 - 最新版本是0.86。我認爲你對.NET 1.1的更長版本號感到困惑。 – 2010-12-14 07:38:44

+0

啊,對,它是運行時版本屬性值。抱歉。 庫的版本是0.84 – 2010-12-14 07:53:10

回答

5

看起來像一個bug給我。幸運的是,您可以訪問代碼,因此您應該能夠準確查看錯誤發生的位置。我建議你構建一個SharpZipLib的調試版本,在引發異常的那一行中斷,並且看看它實際正在測試的內容。

即使沒有2K的數據,也應該可以讀入2K緩衝區。我不會真的寫代碼,但是這是另一回事,我也將它移到它自己的實用方法中 - 將所有數據從一個流複製到另一個流的過程非常漂亮不需要將其綁定到zip)

+0

我已經檢查過所有的文件,並且一個0 kb的sql文件是導致這個異常的原因 – bala3569 2010-12-14 09:22:30

+0

如何解決這個問題 – bala3569 2010-12-14 09:46:26

+0

@ bala3569:我已經建議你查看代碼並找出bug的位置。然後,您可以修復該錯誤並將其提交回SharpZipLib。 – 2010-12-14 09:55:02

-1

看着代碼,你再次讀取同一組字節(並推進位置)。

size = s.Read(data, 0, data.Length);

here一個例子示出了第二參數應該是一個移動位置&不是一個固定的數目。

+0

@shahkalpesh:不,它正在讀取不同的數據,但一次又一次地進入相同的緩衝區......這是好的,因爲OP然後*將緩衝區寫出到另一個流。沒關係。 – 2010-12-14 07:07:29

+0

@Jon:那麼,鏈接上的示例不正確?請注意,文檔是'System.IO.Stream'而不是'ZipInputStream'。 – shahkalpesh 2010-12-14 07:25:37

+0

@shahkalpesh:它正在嘗試做什麼是正確的,這與OP正在嘗試做的不一樣。 – 2010-12-14 07:29:16

-1

將代碼int size = 2048;更改爲int size = data.Length;。你不會採取OutOfRange異常。

using (FileStream streamWriter = File.Create(targetDirectory + fileName)) 
    { 
     int size = data.Length; 
     byte[] data = new byte[size]; 
     while (true) 
     { 
      size = s.Read(data, 0, data.Length); 
      if (size > 0) 
      { 
       streamWriter.Write(data, 0, size); 
      } 
      else 
      { 
       break; 
      } 
     } 
    } 
+0

您正在嘗試在聲明變量('size')之前使用變量。誠然,在原始代碼中,'size'的初始值甚至沒有被使用...但是將其改爲不同的初始值不會有幫助。 – 2010-12-14 07:30:26

相關問題