2016-03-03 40 views
0

我想創建一個zip文件,其中包含zip文件。我正在使用ICSharpCode.SharpZipLib(由於項目限制,必須使用它)。這工作正常,如果我只有1個字節[]數組..但它不適用於字節[]的列表。創建多字節[]陣列/文件的zip文件

foreach (byte[] internalZipFile in zipFiles) 
{ 
    // Source : internal zip file 
    MemoryStream inputMemoryStream = new  MemoryStream(internalZipFile); 

    ZipEntry newZipEntry = new ZipEntry("AdManifest-" + i.ToString() + ".zip"); 
    newZipEntry.DateTime = DateTime.Now; 

    zipStream.PutNextEntry(newZipEntry); 

    StreamUtils.Copy(inputMemoryStream, zipStream, new byte[1024]); 

    zipStream.CloseEntry(); 

    zipStream.IsStreamOwner = false; // to stop the close and underlying stream 
    zipStream.Close(); 

    outputMemoryStream.Position = 0; 

    zipByteArray = outputMemoryStream.ToArray(); 

    i++; 
} 
using (FileStream fileStream = new FileStream(@"c:\manifest.zip", FileMode.Create)) 
{ 
    fileStream.Write(zipByteArray, 0, zipByteArray.Length); 
} 

有人能幫忙嗎?我錯過了什麼?

+0

請仔細閱讀[提問]和闡述「不工作」。 – CodeCaster

回答

0

我想通了。 這裏我們一個爲我工作:

byte[] zipByteArray = null; 
     int i = 0; 

     if (zipFiles != null && zipFiles.Count > 0) 
     { 
      MemoryStream outputMemoryStream = new MemoryStream(); 
      ZipOutputStream zipStream = new ZipOutputStream(outputMemoryStream); 
      zipStream.SetLevel(3); 

      foreach (byte[] internalZipFile in zipFiles) 
      { 
       MemoryStream inputMemoryStream = new MemoryStream(internalZipFile); 

        ZipEntry newZipEntry = new ZipEntry("AdManifest-" + i.ToString() + ".zip"); 
        newZipEntry.DateTime = DateTime.Now; 
        newZipEntry.Size = internalZipFile.Length; 

        zipStream.PutNextEntry(newZipEntry); 

        StreamUtils.Copy(inputMemoryStream, zipStream, new byte[1024]); 
        zipStream.CloseEntry(); 
       i++; 
      } 

      zipStream.IsStreamOwner = false; // to stop the close and underlying stream 
      zipStream.Close(); 

      outputMemoryStream.Position = 0; 
      zipByteArray = outputMemoryStream.ToArray(); 

      using (FileStream fileStream = new FileStream(@"c:\manifest.zip", FileMode.Create)) 
      { 
       fileStream.Write(zipByteArray, 0, zipByteArray.Length); 
      } 
     } 
0

我不能嘗試,但我覺得比你在迭代體

加上我已經刪除outpustmemorystream和只使用zipStream需要更少的代碼。

foreach (byte[] internalZipFile in zipFiles) 
{ 
    // Source : internal zip file 
    MemoryStream inputMemoryStream = new MemoryStream(internalZipFile); 

    ZipEntry newZipEntry = new ZipEntry("AdManifest-" + i.ToString() + ".zip"); 
    newZipEntry.DateTime = DateTime.Now; 

    zipStream.PutNextEntry(newZipEntry); 

    StreamUtils.Copy(inputMemoryStream, zipStream, new byte[1024]); 

    zipStream.CloseEntry(); 

    i++; 
} 
zipStream.IsStreamOwner = false; // to stop the close and underlying stream 

zipStream.Position = 0; 

zipByteArray = zipStream.ToArray(); 

zipStream.Close(); 

using (FileStream fileStream = new FileStream(@"c:\manifest.zip", FileMode.Create)) 
{ 
    fileStream.Write(zipByteArray, 0, zipByteArray.Length); 
} 
+0

更改了此項。但仍然無法正常工作。另外,當我嘗試打開存檔時,它說內部zip文件已損壞。 –