创建多个byte[]数组/文件的zip文件

3
我正在尝试创建一个包含多个zip文件的zip文件。由于项目限制,我必须使用ICSharpCode.SharpZipLib。如果只有一个byte[]数组,这个方法可以正常工作,但是对于byte[]数组列表则无法正常工作。请帮助我解决这个问题。
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);
}

有人可以帮忙吗?我错过了什么吗?


请阅读并详细说明“不起作用”的问题。 - undefined
2个回答

4
我已经想到了解决方法。 这是对我有效的一个:
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);
            }
        }

请查看这里:https://dev59.com/bmQm5IYBdhLWcg3w8iuD - undefined

0

我无法尝试,但我认为你需要在迭代体中减少代码量。

另外,我已经移除了OutputMemoryStream,并只使用了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);
}

修改了这个,但还是不起作用。而且,当我尝试打开存档时,它显示内部压缩文件已损坏。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接