Accessing files within a zip archive
Sometimes you may need to access files within a zip archive, like writing an installation program, or packing game contents. You may want to dig into zip file's format (http://www.pkware.com/documents/casestudies/APPNOTE.TXT), and write your own code. However, writing a zip reader from scratch is not an easy job.
For an easier way, you can look into zlib's contrib folder, where you can find a minizip folder. Guess what, we already have working code to create and read zip file! Thanks to the contributors, you saved my time!
The minizip is very easy to use. You can use it to read files within a zip archive very easily. Here is a code snippet.
#include <stdio.h>
#include <stdlib.h>
#include "unzip.h"
void ReadTest(unzFile unz)
{
char str[100] = {0};
int readsize = 100;
/* read some bytes */
readsize = unzReadCurrentFile(unz, str, readsize);
/* and print it */
str[99] = 0;
printf("Read some bytes from zip: %s\n", str);
}
int main(void)
{
unzFile unz;
/* open the zip file */
unz = unzOpen("test.zip");
if (unz)
{
/* find the file we want to access, and open it */
if (unzLocateFile(unz, "data.txt", 0) == Z_OK &&
unzOpenCurrentFile(unz) == Z_OK)
{
/* try some basic infos */
unz_file_info info;
unzGetCurrentFileInfo(unz, &info, NULL, 0, NULL, 0, NULL, 0);
printf("File info:\n");
printf("Compressed size: %d\n", info.compressed_size);
printf("Uncompressed size: %d\n", info.uncompressed_size);
/* And some read test */
ReadTest(unz);
}
unzClose(unz);
}
return 0;
}
Love it? Why not try it now!

浙公网安备 33010602011771号