我在数据库中有一个ziparchive字节数组。当我从数据库中检索数据并尝试转换回Ziparchive时,它会抛出一个错误。有没有办法从字节数组转换为zipArchive?
答案 0 :(得分:2)
从此answer我认为可以将您的流字节数组转换为zip存档:
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" };
}
在这里,使用行new FileContentResult(compressedFileStream.ToArray(), "application/zip") { FileDownloadName = "Filename.zip" };
,如果您已经转换为zip文件,那么您可以将您的流字节数组转换为zip存档,如下所示:
new FileContentResult(your_stream_byte_array, "application/zip") { FileDownloadName = "Filename.zip" };