C# 从不可搜索的流中读取大型 zip 存档
C# Read large zip archive from non-seekable stream
我在流中提供了一个不可搜索的 zip 文件。
zip 文件本身的大小大于 Int32.MaxValue
。我的目标是读取 zip 文件中包含的条目之一并将其输出到某个目标流。
类似这样:
using var cs = new System.Security.Cryptography.CryptoStream (...);
using var z = new System.IO.Compression.ZipArchive (cs, ZipArchiveMode.Read);
var entry = zip.GetEntry("1");
entry.Open().CopyTo(...);
因为流不支持搜索,System.IO.Compression.ZipArchive
class 尝试将流复制到支持 MemoryStream
中。这失败了,因为 MemoryStream
的大小限制为 2GB 并引发 IOException
:
Stream was too long.
我想避免在阅读条目之前将整个流下载到文件中。有没有办法做到这一点?我可以使用其他库。
可以读取流两次或三次。我希望这足以找到条目内容,以便可以对其进行解压缩。
这个库似乎是您(我们)需要的:
https://github.com/adamhathcock/sharpcompress
来自他们的readme.md:
The major feature is support for non-seekable streams so large files
can be processed on the fly (i.e. download stream).
请注意,另一种选择是使用管道:https://docs.microsoft.com/en-us/dotnet/standard/io/pipelines
我最终使用了标准的 deflate 库。我认为最好将管道与您选择的任一库一起使用,可能 zlibstream 与管道一起工作。
我在流中提供了一个不可搜索的 zip 文件。
zip 文件本身的大小大于 Int32.MaxValue
。我的目标是读取 zip 文件中包含的条目之一并将其输出到某个目标流。
类似这样:
using var cs = new System.Security.Cryptography.CryptoStream (...);
using var z = new System.IO.Compression.ZipArchive (cs, ZipArchiveMode.Read);
var entry = zip.GetEntry("1");
entry.Open().CopyTo(...);
因为流不支持搜索,System.IO.Compression.ZipArchive
class 尝试将流复制到支持 MemoryStream
中。这失败了,因为 MemoryStream
的大小限制为 2GB 并引发 IOException
:
Stream was too long.
我想避免在阅读条目之前将整个流下载到文件中。有没有办法做到这一点?我可以使用其他库。
可以读取流两次或三次。我希望这足以找到条目内容,以便可以对其进行解压缩。
这个库似乎是您(我们)需要的: https://github.com/adamhathcock/sharpcompress
来自他们的readme.md:
The major feature is support for non-seekable streams so large files can be processed on the fly (i.e. download stream).
请注意,另一种选择是使用管道:https://docs.microsoft.com/en-us/dotnet/standard/io/pipelines
我最终使用了标准的 deflate 库。我认为最好将管道与您选择的任一库一起使用,可能 zlibstream 与管道一起工作。