2008-09-30 4 views
0

SharpZipLib 버전 0.85.5를 사용하여 파일을 압축 해제합니다. 내 코드는 ZIP 파일을 찾을 때까지 2 개월 동안 훌륭하게 작동했습니다.SharpZipLib - ZipException "추가 데이터 끝"-이 예외가 발생하는 이유는 무엇입니까?

 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); 
     } 
    } 

파일을 사용하여 미세 압축을 풀고 XP에 내장 된 우편 지원,에서 WinZip, 7-우편 번호 :

ICSharpCode.SharpZipLib.Zip.ZipException: End of extra data  
    at ICSharpCode.SharpZipLib.Zip.ZipExtraData.ReadCheck(Int32 length) in C:\C#\SharpZLib\Zip\ZipExtraData.cs:line 933  
    at ICSharpCode.SharpZipLib.Zip.ZipExtraData.Skip(Int32 amount) in C:\C#\SharpZLib\Zip\ZipExtraData.cs:line 921  
    at ICSharpCode.SharpZipLib.Zip.ZipEntry.ProcessExtraData(Boolean localHeader) in C:\C#\SharpZLib\Zip\ZipEntry.cs:line 925  
    at ICSharpCode.SharpZipLib.Zip.ZipInputStream.GetNextEntry() in C:\C#\SharpZLib\Zip\ZipInputStream.cs:line 269  
    at Constellation.Utils.Tools.UnzipFile(String sourcePath, String targetDirectory) in C:\C#\Constellation2\Utils\Tools.cs:line 90  
--- End of inner exception stack trace --- 

여기 내 압축 해제 방법이다. 예외는 s.GetNextEntry()에 던져지고 있습니다.

답변

1

다른 zip 도구가 손상된 추가 데이터를 무시하고 있거나 #ZipLib에 버그가있을 수도 있습니다. (나는 잠시 전에 특정 옵션으로 파일을 압축하고 압축을 풀지 않을 특정 파일을 발견했습니다.)

이 특별한 경우에는 개발자의 관심을 끌기 위해 #ZipLib 포럼에 게시하는 것이 좋습니다. . 파일에 중요한 데이터가 포함되어 있지 않고 짧지 만 완전한 프로그램을 얻을 수 있다면 엄청난 도움이 될 것으로 생각됩니다.

0

나는 Jon에 동의합니다. 주석에 다음과 맞지 않을 수 없습니다 :

이 쉽게이 같은 것을 사용하지 않는 것입니다 (이 질문에 대답하지 않지만) :

public static void UnzipFile(string sourcePath, string targetDirectory) 
{ 
    try 
    { 
     FastZip fastZip = new FastZip(); 
     fastZip.CreateEmptyDirectories = false; 
     fastZip.ExtractZip(sourcePath, targetDirectory,""); 
    } 
    catch(Exception ex) 
    { 
     throw new Exception("Error unzipping file \"" + sourcePath + "\"", ex); 
    } 
} 
0

official ZIP specification을 참조하십시오.

ZIP 보관 파일의 각 파일에는 '추가'입력란이 있습니다. #ZipLib은 주어진 '추가'필드 길이가 읽을 수있는 데이터의 양보다 길다는 것을 알려주고 있다고 생각합니다. 즉, ZIP 파일이 잘 렸을 가능성이 큽니다.

0

012.의 4.5.3에 따르면, 추가 로컬 데이터의 압축 크기는 "해당 로컬 또는 중앙 디렉터리 레코드 필드가 0xFFFF 또는 0xFFFFFFFF"로 설정된 경우에만 나타나야합니다.

그러나 SharpZipLib은 "useZip64_ == UseZip64.On"경우에만 ZipFile.WriteCentralDirectoryHeader 메서드에서 씁니다. entry.IsZip64Forced() 상태를 추가하고 버그가 사라졌습니다.

  if (entry.CentralHeaderRequiresZip64) { 
      ed.StartNewEntry(); 

      if ((entry.Size >= 0xffffffff) || (useZip64_ == UseZip64.On) || entry.IsZip64Forced()) 
      { 
       ed.AddLeLong(entry.Size); 
      } 

      if ((entry.CompressedSize >= 0xffffffff) || (useZip64_ == UseZip64.On) || entry.IsZip64Forced()) 
      { 
       ed.AddLeLong(entry.CompressedSize); 
      }