[英]Get byte array for ZipArchive in C#
I'm trying to write code to remove the _MAXOSX
folder from a zip archive if it exists (breaks validation further down the line if it exists).我正在尝试编写代码以从 zip 存档中删除
_MAXOSX
文件夹(如果存在,则进一步中断验证)。
The code must return a byte[]
and not the ZipArchive itself.代码必须返回一个
byte[]
而不是 ZipArchive 本身。 The code I currently have is:我目前拥有的代码是:
var fileBytes = uploadController.FileBytes;
if (stripMacOSXFolder && uploadController.FileName.EndsWith(".zip", StringComparison.CurrentCultureIgnoreCase))
{
try
{
using (var data = new MemoryStream(fileBytes))
using (var archive = new ZipArchive(data, ZipArchiveMode.Update))
{
var osx = archive.Entries.SingleOrDefault(c =>
c.FullName.Equals("__MACOSX/", StringComparison.CurrentCultureIgnoreCase));
if (osx != null)
{
osx.Delete();
// SET fileBytes to archive byte[]
}
}
}
catch (Exception)
{
return new ObjectReturnMethodResult<UploadedFileV2>("Uploaded zip appears to be invalid.");
}
}
It's not clear to me once I've deleted the entry how I set fileBytes
to the byte array of the updated ZipArchive.一旦我删除了如何将
fileBytes
设置为更新后的 ZipArchive 的字节数组的条目,我就fileBytes
了。
The docs for Update
mode state: Update
模式状态的文档:
When you set the mode to Update, the underlying file or stream must support reading, writing, and seeking.
当您将模式设置为更新时,底层文件或流必须支持读取、写入和查找。 The content of the entire archive is held in memory, and no data is written to the underlying file or stream until the archive is disposed.
整个存档的内容保存在内存中,在存档被释放之前,不会将数据写入底层文件或流。
It seems that you will need to obtain the updates bytes after the ZipArchive
has been disposed of:似乎您需要在处理
ZipArchive
后获取更新字节:
using (var data = new MemoryStream(fileBytes))
{
using (var archive = new ZipArchive(data, ZipArchiveMode.Update))
{
var osx = archive.Entries.SingleOrDefault(c =>
c.FullName.Equals("__MACOSX/", StringComparison.CurrentCultureIgnoreCase));
if (osx != null)
{
osx.Delete();
}
}
fileBytes = data.ToArray();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.