I try to read a binary file which is part of a zip archive. Therefore I opened the zip archive with Poco::Zip. Then I read the binary file chunkwise
struct Header
{
uint64_t tag;
uint32_t payloadLength;
} __attribute__((packed));
std::ifstream inp("my.zip", std::ios::binary);
Poco::Zip::ZipArchive arch(inp);
Poco::Zip::ZipArchive::FileHeaders::const_iterator it = arch.findHeader("file.dat");
if (it != arch.headerEnd()) {
Poco::Zip::ZipInputStream inStream(inp, it ->second);
Header hdr;
inStream.read(reinterpret_cast<char*>(&hdr), sizeof(hdr));
char buffer[hdr.payloadLength];
inStream.read(reinterpret_cast<char*>(buffer), sizeof(hdr.payloadLength));
...
}
I got garbage data when reading from the ZipInputStream.
When I use
std::string str((std::istreambuf_iterator<char>(inStream)), std::istreambuf_iterator<char>());
it works, but I want to avoid this solution because it buffers the complete file in one possible large buffer. What is goning wrong here with read()
?