Reputation: 602
I am trying to read a test.zip file using the zlib library and extract the contents. My requirement is to read each file and output content (unzipped) first to memory and then to a file.
I tried the below code:
#include <iostream>
#include <zlib.h>
using namespace std;
#define LINE 1024
int main()
{
const char *filename = "hello.zip";
gzFile inFileZ = gzopen(filename, "rb");
if (inFileZ == NULL) {
printf("Error: Failed to gzopen %s\n", filename);
return -1;
}
unsigned char unzipBuffer[8192];
unsigned int unzippedBytes;
std::vector<unsigned char> unzippedData;
while (true) {
unzippedBytes = gzread(inFileZ, unzipBuffer, 8192);
if (unzippedBytes > 0) {
cout << unzippedBytes << endl;
for (int i = 0; i < unzippedBytes; i++) {
cout << unzipBuffer[i];
}
} else {
break;
}
}
gzclose(inFileZ);
return 0;
}
I got the above example from the web. It was mentioned that gzread
inflates the contents and fills the buffer (here unzipBuffer
). However when I tried to print it, it was printing the same contents of the .zip file (example PK.....
with non readable code).
I believe I am not correctly using the APIs of zlib. However I could not find an example related to this. Can someone help me here?
Upvotes: 0
Views: 403
Reputation: 20130
THere is known ZIP reimplementation in C, from http://www.info-zip.org/. They have Windows DLL which I believe could be used to do what you want
UPDATE
some example is here http://www.vbaccelerator.com/home/VB/Code/Libraries/Compression/Unzipping_Files/article.asp
Upvotes: 0
Reputation: 112627
The zlib library does not directly process the zip format. zlib provides the raw deflate compression and decompression that you would need to process deflated zip entries, as well as a CRC-32 calculation facility. However you would need to write your own code or use another library to parse and process the zip format.
gzip is not zip. They are two completely different formats that use, or may use, the same raw compression format within.
Upvotes: 1