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

本文关键字:文件 zip 数组 字节 创建 | 更新日期: 2023-09-27 18:37:13

我正在尝试创建一个包含zip文件的zip文件。我正在使用ICSharpCode.SharpZipLib(由于项目限制,必须使用它)。如果我只有 1 字节 [] 数组,这工作正常。但它不适用于字节列表[]。

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

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

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

我想

通了。在这里,我们为我工作:

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

我不能尝试,但我认为迭代体中需要的代码更少

另外,我已经删除了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);
}