2015-01-03 12 views
1

C#의 SharpZip 라이브러리 (버전 0.86.0)를 가지고 놀았습니다. 나는 기본적으로 여러 개의 파일을 하나의 정리 된 zip 파일로 패키지화하는 데 사용하고 있습니다. 내 함수는 zip 파일의 바이트 배열을 생성하는 것처럼 보입니다.C# ZipOutputStream 출력 스트림에서 잘못된 파일 가져 오는 중

public static byte[] CompressToZip(List<Tuple<byte[], string>> fileItemList, int zipLevel = 3) 
{ 
     MemoryStream zipMemoryStream = new MemoryStream(); 

     ZipOutputStream zOutput = new ZipOutputStream(zipMemoryStream); 
     zOutput.SetLevel(zipLevel); 

     ICSharpCode.SharpZipLib.Checksums.Crc32 crc = new ICSharpCode.SharpZipLib.Checksums.Crc32(); 

     foreach (var file in fileItemList) 
     { 
      ZipEntry entry = new ZipEntry(file.Item2); 
      entry.DateTime = DateTime.Now; 
      entry.Size = file.Item1.Length; 

      crc.Reset(); 
      crc.Update(file.Item1); 

      entry.Crc = crc.Value; 

      zOutput.PutNextEntry(entry); 
      zOutput.Write(file.Item1, 0, file.Item1.Length); 
     } 

     zOutput.IsStreamOwner = false; 
     zOutput.Finish(); 
     zOutput.Close(); 
     zipMemoryStream.Position = 0; 

     byte[] zipedFile = zipMemoryStream.ToArray(); 
     return zipedFile; 
    } 

이 함수는 하나의 항목이있는 파일에서 정상적으로 작동합니다. 그러나 내가 두 개 또는 그 이상의 이유를 가지고 추출/열 때 오류가 발생합니다.

PeaZip는 말한다 :

아카이브 읽을 수 없습니다

WinZip을 말한다 :

이 파일의 로컬 헤더에 저장된 압축 된 크기가 아니라 압축과 동일 중앙 헤더에 저장된 크기

하지만 키커가 있습니다. Windows 8 보관 도구는 파일과 잘 작동합니다. WinZip 오류 종류로 인해 스트림에 파일을 잘못 쓰는 것으로 생각됩니다. 그러나 그것은 나에게 잘 보인다.

여기 codemonkeys 입력에서 내 변화의

편집 .. 이런 무엇을 할 확실하지. 나에게 잘 보이지만 여전히 동일한 오류가 발생하고 있습니다.

public static byte[] CompressToZip(List<Tuple<byte[], string>> fileItemList, int zipLevel = 3) 
{ 
     MemoryStream zipMemoryStream = new MemoryStream(); 

     ZipOutputStream zOutput = new ZipOutputStream(zipMemoryStream); 
     zOutput.SetLevel(zipLevel); 

     ICSharpCode.SharpZipLib.Checksums.Crc32 crc = new ICSharpCode.SharpZipLib.Checksums.Crc32(); 

     foreach (var file in fileItemList) 
     { 
      ZipEntry entry = new ZipEntry(file.Item2); 
      entry.DateTime = DateTime.Now; 
      entry.Size = file.Item1.Length; 

      crc.Reset(); 
      crc.Update(file.Item1); 

      entry.Crc = crc.Value; 

      zOutput.PutNextEntry(entry); 
      var memStreamCurrentfile = new MemoryStream(file.Item1); 
      StreamUtils.Copy(memStreamCurrentfile, zOutput, new byte[4096]); 
      zOutput.CloseEntry(); 
     } 

     zOutput.IsStreamOwner = false; 
     zOutput.Finish(); 
     zOutput.Close(); 
     zipMemoryStream.Position = 0; 

     byte[] zipedFile = zipMemoryStream.ToArray(); 
     return zipedFile; 
    } 
+1

: 여기에 모두가 즐길 수의 최종 코드는 . –

+0

그냥 시도해 보았고 StreamUtils.Copy() 메서드도 사용했습니다. 여전히 같은 문제 :( – user2326106

답변

0

알아 냈어! 그것은 날 Crc 설정 및 파일 항목의 크기가 문제가 보인다. 나는 그것들을 정의하는데 도움이 될 것이라고 생각했다. 나는 틀렸어. 모든 항목 후`,

public static byte[] CompressToZip(List<Tuple<byte[], string>> fileItemList, int zipLevel = 3) 
    { 
     MemoryStream zipMemoryStream = new MemoryStream(); 
     ZipOutputStream zOutput = new ZipOutputStream(zipMemoryStream); 
     zOutput.SetLevel(zipLevel); 
     ICSharpCode.SharpZipLib.Checksums.Crc32 crc = new ICSharpCode.SharpZipLib.Checksums.Crc32(); 
     foreach (var file in fileItemList) 
     { 
      ZipEntry entry = new ZipEntry(file.Item2); 
      entry.DateTime = DateTime.Now; 
      zOutput.PutNextEntry(entry); 
      var memStreamCurrentfile = new MemoryStream(file.Item1); 
      StreamUtils.Copy(memStreamCurrentfile, zOutput, new byte[4096]); 
      zOutput.CloseEntry(); 
     } 
     zOutput.IsStreamOwner = false; 
     zOutput.Finish(); 
     zOutput.Close(); 
     zipMemoryStream.Position = 0; 

     byte[] zipedFile = zipMemoryStream.ToArray(); 
     return zipedFile; 
    } 
이 https://github.com/icsharpcode/SharpZipLib/wiki/Zip-Samples#anchorMemory의 예는`zipStream.CloseEntry()를 추가