Read binary file from a zip file from C# without unzipping it

拈花ヽ惹草 提交于 2019-12-12 09:49:07

问题


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

The zip file structure:

zipFolderName/subFolder/BinFile

In the BinFile, I have:

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

In 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();
}

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


回答1:


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

ms.Seek(0, SeekOrigin.Begin);


来源:https://stackoverflow.com/questions/43874834/read-binary-file-from-a-zip-file-from-c-sharp-without-unzipping-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!