从C#中读取zip文件中的二进制文件,而无需解压缩

3
我希望能够在不解压缩的情况下读取zip文件中的二进制文件。
zip文件结构如下:
zipFolderName/subFolder/BinFile

在BinFile中,我有:

Id1, id2, value1 // id1, id2 are string, value1 is int

在C#中:
 ZipEntry binFileName = …; // it has been got from zipFile entries
 MemoryStream ms  = new MemoryStream();
 binFileName.Extract(ms);

using (BinaryReader reader = new BinaryReader(ms))
{
    string id1 = reader.ReadString(); // error popped here
    string id2 = reader.ReadString();
    int value1 = reader.ReadInt32();
}

我遇到了错误: 无法读取流的末尾。 看起来 BinaryReader 无法读取 MemoryStream?

你尝试过在读取之前将内存流定位到开头吗? - Rowland Shaw
请注意,BinaryReader.ReadString期望在特定的数据格式中,字符串长度会作为前缀出现在流中。如果没有这个字符串长度数据,该方法将会错误地解释流中的字节数据并表现出奇怪的行为... - user2819245
内存流只有一个位置(不像Unix有读和写两个位置)。因此,当您向内存流写入时,位置会留在流的末尾。因此,在读取之前,必须将位置设置回零。 - jdweng
1个回答

4

binFileName.Extract(ms);之后,请尝试以下操作:

ms.Seek(0, SeekOrigin.Begin);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接