创建一个从字节zip文件[] [英] Create zip file from byte[]

查看:141
本文介绍了创建一个从字节zip文件[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个Zip从一系列字节数组的文件中.NET 4.5(System.IO.Com pression)。作为一个例子,从我使用一个API我结束了一个列表<附件> ,每个附件有物业名为机身这是一个字节[] 。我怎么可以遍历该列表,并创建一个包含每个附件的zip文件?

现在我的IM pression,我将不得不写每个附件到磁盘,从创建zip文件下。

  //这是伟大的,如果我对磁盘上的文件
ZipFile.CreateFromDirectory(startPath,zipPath);
//如何从一系列的字节数组的创造呢?


解决方案

在多一点玩耍和阅读我能想出解决办法。这里是你可以创建多个文件的zip文件(存档),而无需编写任何临时数据到磁盘:

 使用(VAR COM pressedFileStream =新的MemoryStream()){
    //创建一个归档和数据流存储在内存中。
    使用(VAR zipArchive =新的ZipArchive(COM pressedFileStream,ZipArchiveMode.Update,FALSE)){
        的foreach(在caseAttachmentModels VAR caseAttachmentModel){
            //创建为每个附件的ZIP条目
            VAR的ZipEntry = zipArchive.CreateEntry(caseAttachmentModel.Name);            //获取附件的流
            使用(VAR originalFileStream =新的MemoryStream(caseAttachmentModel.Body)){
                使用(VAR zipEntryStream = zipEntry.Open()){
                    //附件流复制到ZIP入口流
                    originalFileStream.CopyTo(zipEntryStream);
                }
            }
        }    }    返回新FileContentResult(COM pressedFileStream.ToArray(),应用程序/压缩){FileDownloadName =Filename.zip};
}

I am trying to create a Zip file in .NET 4.5 (System.IO.Compression) from a series of byte arrays. As an example, from an API I am using I end up with a List<Attachment> and each Attachment has a property called Body which is a byte[]. How can I iterate over that list and create a zip file that contains each attachment?

Right now I am under the impression that I would have to write each attachment to disk and create the zip file from that.

//This is great if I had the files on disk
ZipFile.CreateFromDirectory(startPath, zipPath);
//How can I create it from a series of byte arrays?

解决方案

After a little more playing around and reading I was able to figure this out. Here is how you can create a zip file (archive) with multiple files without writing any temporary data to disk:

using (var compressedFileStream = new MemoryStream()) {
    //Create an archive and store the stream in memory.
    using (var zipArchive = new ZipArchive(compressedFileStream, ZipArchiveMode.Update, false)) {
        foreach (var caseAttachmentModel in caseAttachmentModels) {
            //Create a zip entry for each attachment
            var zipEntry = zipArchive.CreateEntry(caseAttachmentModel.Name);

            //Get the stream of the attachment
            using (var originalFileStream = new MemoryStream(caseAttachmentModel.Body)) {
                using (var zipEntryStream = zipEntry.Open()) {
                    //Copy the attachment stream to the zip entry stream
                    originalFileStream.CopyTo(zipEntryStream);
                }
            }
        }

    }

    return new FileContentResult(compressedFileStream.ToArray(), "application/zip") { FileDownloadName = "Filename.zip" };
}

这篇关于创建一个从字节zip文件[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆