2016-03-03 65 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文件已损坏。 –