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

时间:2017-05-09 16:08:23

标签: c# .net zipfile bin binaryreader

我想从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?

1 个答案:

答案 0 :(得分:4)

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

ms.Seek(0, SeekOrigin.Begin);