【发布时间】:2018-04-19 22:44:55
【问题描述】:
我正在尝试使用二进制阅读器读取 .gz 文件,首先使用 gzipstream 解压缩,然后使用 gzipstream 创建一个新的二进制阅读器。但是,当我尝试使用 BinaryReader 的 BaseStream.Position 和 BaseStream.Length 时(知道我何时处于文件末尾),我得到一个 NotSupportedException,检查 GZipStream 类中这些字段的文档显示:
长度
此属性不受支持,并且总是抛出 NotSupportedException。(覆盖 Stream.Length。)
位置
此属性不受支持,并且总是抛出 NotSupportedException。(覆盖 Stream.Position。)
所以我的问题是,当使用 BinaryReader 读取解压缩的 GZipStream 时,我如何知道我何时处于文件末尾?谢谢
这是我的代码:
Stream stream = new MemoryStream(textAsset.bytes);
GZipStream zippedStream = new GZipStream(stream, CompressionMode.Decompress);
using (BinaryReader reader = new BinaryReader(zippedStream))
while(reader.BaseStream.Position != reader.BaseStream.Length)
{
//do stuff with BinaryReader
}
以上抛出: NotSupportedException:不支持操作。 System.IO.Compression.DeflateStream.get_Position()
由于 while() 中的 BaseStream.Position 调用
【问题讨论】:
-
我还没有真正使用过这个,但你不能一直读下去直到它失败吗?
-
BinaryReader 是一个 .Net 类。压缩文件不必遵循其规范。所以直接使用 zippedStream.Read 直到它返回
-
@Eser 检查
标签: c# gzip binaryreader