gpt4 book ai didi

c# - 将存档文件中的 Stream 转换为 Byte[]

转载 作者:行者123 更新时间:2023-12-02 03:25:32 33 4
gpt4 key购买 nike

在 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# - 将存档文件中的 Stream 转换为 Byte[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53545108/

33 4 0