[英]How do I add a byte array Image to a ziparchive?
My situation: I have a bunch of Jpeg images in the form of byte arrays. I have a list of these objects:我的情况:我有一堆字节 arrays 形式的 Jpeg 图像。我有这些对象的列表:
public class MyImage
{
public byte[] ImageData { get; set; }
}
My desired situation: I want to create a zip file in memory and put it in a variable.我想要的情况:我想在 memory 中创建一个 zip 的文件,并把它放在一个变量中。 I am creating these zip files in a REST service.我正在 REST 服务中创建这些 zip 文件。 Ideally, I would turn this ziparchive into a byte array so that I can send it to systems written in other languages.理想情况下,我会将此 ziparchive 转换为字节数组,以便我可以将其发送到用其他语言编写的系统。
I have code for creating a ziparchive, and adding my image byte arrays to it.我有创建 ziparchive 的代码,并将我的图像字节 arrays 添加到其中。 But how do I turn the ziparchive itself into a byte array?但是如何将 ziparchive 本身变成一个字节数组呢?
var images = new List<MyImage>();
//add data to images
using (var ms = new MemoryStream())
{
using (var zipArchive = new ZipArchive(ms, ZipArchiveMode.Create, true))
{
var i = 1;
foreach (var image in images)
{
var entry = zipArchive.CreateEntry("image" + i + ".jpg", CompressionLevel.Fastest);
using (var entryStream = entry.Open())
{
entryStream.Write(image.ImageData, 0, photo.ImageOriginal.Length);
}
i++;
}
//var zipBytes = magic method for turning my archive into a zipfile
}
}
My question: How do I get this to work?我的问题:我如何让它工作? Bonus question: is a ziparchive turned into a byte array compatible with other languages?奖励问题:是否将 ziparchive 转换为与其他语言兼容的字节数组?
Your ZipArchive is mapped onto a MemoryStream (variable ms
). 您的ZipArchive被映射到MemoryStream(变量ms
)。 You can get the contents of a MemoryStream as a byte array using MemoryStream.ToArray() . 您可以使用MemoryStream.ToArray()将MemoryStream的内容作为字节数组获取。
To convert memory stream array to a zip file, you can use a method such that:要将 memory stream 数组转换为 zip 文件,您可以使用如下方法:
var file = File(ms.ToArray(), "application/zip", "zipFileName.zip");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.