从 C# 的 zip 文件中读取二进制文件而不解压 [英] Read binary file from a zip file from C# without unzipping it

查看:25
本文介绍了从 C# 的 zip 文件中读取二进制文件而不解压的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个 zip 文件中读取一个二进制文件而不解压它.

I would like to read a binary file from a zip file without unzipping it .

zip 文件结构:

zipFolderName/subFolder/BinFile

在 BinFile 中,我有:

In the BinFile, I have:

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 ?

I got error: Unable to read beyond the end of the stream. It seems that BinaryReader cannnot read MemoryStream ?

推荐答案

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

After binFileName.Extract(ms); try the following:

ms.Seek(0, SeekOrigin.Begin);

这篇关于从 C# 的 zip 文件中读取二进制文件而不解压的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆