0

This question is similar to How to read data from a zip file without having to unzip the entire file excepted I'd like to go a bit further and read only a part of a file, i.e. get the file stream, and seek to a position for which I know the offset in bytes. I don't know if the zip format allows that in the first place.

I've tried to seek in the stream returned by ZipArchive.Entries[...].Open(), but it throws, saying the operation is not supported.

I can of course just read (and discard) the contents up to the point I'm interested in, but this is slow for big files.

EDIT: An example to make clear what I want to do:

Let's say I have a file archive.zip containing several files, one of them is bigfile.bin. I already know how to decompress bigfile.bin without decompressing the other files of archive.zip, no problem there. My question is: can I skip 10 000 000 bytes of bigfile.bin and start reading what remains? Those 10 000 000 bytes would be measured in the decompressed stream of course.

using (var archive = new ZipArchive("archive.zip"))
{
    using (var data = archive.Entries.Single(e => e.Name == "bigfile.bin").Open())
    {
        data.Seek(10000000, SeekOrigin.Begin); // this is what I want to do but it doesn't work
        data.Read(/*etc*/);
    }
}
youen
  • 1,952
  • 1
  • 23
  • 32
  • A zip file has a file system just like a hard drive. So you just can't randomly go to an offset. You must go by section (directory) to section to extract data. Also if files are appended to zip at different times the new files are put at end so they don't always go where the actually belong in the directory structure. – jdweng Jun 08 '17 at 10:40
  • Not sure I understand you, you say it's like a file system on a hard drive, but I don't see the point, I can seek as I want in files on my hard drive, this doesn't tell me if I can do the same in files inside a zip archive. I'll edit the question to make clear what I want to do. – youen Jun 08 '17 at 12:19
  • Yes a file you can seek, not a folder. Your zip contains folders and files. Even if you have only one file in the zip there is a header on the zip file which contains a points to the start of the first file. – jdweng Jun 08 '17 at 12:47

0 Answers0