1

I am looking for a way to read a part of a binary zip file (starting position and number of bytes to read). Currently I'm investigating this on Windows, but optimally it would be platform independent. For a normal binary file (unzipped), this can be achieved in the following way:

//Open the file
std::ifstream file (path, std::ios::in | std::ios::binary | std::ios::ate);

//Move to the position to start reading
file.seekg(64); 

//Read 128 bytes of the file
std::vector<unsigned char> mDataBuffer;
mDataBuffer.resize( 128 ) ;
file.read( (char*)( &mDataBuffer[0]), 128 ) ;

//Read as string
std::string s_data( mDataBuffer.begin(), mDataBuffer.end());

file.close()

This example is a slightly modified version of this one.

There are also many unzip packages available (e.g. zlib or minizip). Each covering functions to unzip a file. I could simply unzip my zipped file, save it on the disk and read it using the method above.

Unfortunately, I didn't find an example to read only a part of a binary zip file (if that is even possible), straight from the zipped file. Because my file is quite large, I don't want to unzip it completely onto the hard drive. Furthermore, the part that I want to read is quite small, so it would be a waste of cpu time to completely unzip the file. For the same reasons, I also don't want to decompress the complete file into my memory. I am looking for a genuine way to read only a part of a zipped file.

How could this be accomplished in c++?

Community
  • 1
  • 1
takje
  • 2,630
  • 28
  • 47

1 Answers1

2

Apparently there is no general way to seek in zip files. This was according to:

  • A comment of @πάντα ῥεῖ.
  • A general thread on searching in zipped files here.
  • A similar question here (although the question itself is about Python).
Community
  • 1
  • 1
takje
  • 2,630
  • 28
  • 47