c# - 将存档文件中的 Stream 转换为 Byte[]
问题描述
在 Net Core 2.1 上,我试图从 ZIP 存档中读取文件。
我需要将每个文件内容转换为 Byte[] 所以我有:
using (ZipArchive archive = ZipFile.OpenRead("Archive.zip")) {
foreach (ZipArchiveEntry entry in archive.Entries) {
using (Stream stream = entry.Open()) {
Byte[] file = new Byte[stream.Length];
stream.Read(file, 0, (Int32)stream.Length);
}
}
}
当我运行它时,我收到错误:
Exception has occurred: CLR/System.NotSupportedException
An exception of type 'System.NotSupportedException' occurred in System.IO.Compression.dll but was not handled in user code:
'This operation is not supported.' at System.IO.Compression.DeflateStream.get_Length()
如何将内容放入每个文件的 Byte[] 中?
解决方案
尝试做这样的事情:
using (ZipArchive archive = ZipFile.OpenRead("archieve.zip"))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
using (Stream stream = entry.Open())
{
byte[] bytes;
using (var ms = new MemoryStream())
{
stream.CopyTo(ms);
bytes = ms.ToArray();
}
}
}
}
推荐阅读
- c# - dot net core(pomelo mysql provider)中的mysql - 无法访问已处置的对象
- python - sys.stdin.readline 的问题
- c# - myItem.Id,这是跨系统唯一的吗
- android - 使用同一行中最大长度的文本更新 SQLite 列
- html - babylonjs 场景边框的小问题
- mysql - 数据仓库逻辑模型
- python - 不能使用带下标的赋值表达式
- python - 如何在 txt 文件中使用 pandas.DataFrame.to_csv 保存二维数组
- python - 在 Python 中操作列表中的一些值
- autohotkey - 为什么我的代码在最后打印双“k”?自动热键 (ahk)