Unzip the zip file using zlib

I have archive.zip that contains two encrypted .txt files. I would like to unzip the archive to get these 2 files.

Here is what I have done so far:

FILE *FileIn = fopen("./archive.zip", "rb");
if (FileIn)
    printf("file opened\n");
else
    printf("unable to open file\n");

fseek(FileIn, 0, SEEK_END);
unsigned long FileInSize = ftell(FileIn);
printf("size of input compressed file : %u\n", FileInSize);

void *CompDataBuff = malloc(FileInSize);
void *UnCompDataBuff = NULL;

int fd = open ("archive.zip", O_RDONLY);
CompDataBuff = mmap(NULL, FileInSize, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
printf("buffer read : %s\n", (char *)CompDataBuff);

uLongf UnCompSize = (FileInSize * 11/10 + 12);
UnCompDataBuff = malloc(UnCompSize);

int ret_uncp ;

ret_uncp = uncompress((Bytef*)UnCompDataBuff, &UnCompSize, (const Bytef*)CompDataBuff,FileInSize);
printf("size of uncompressed data : %u\n", UnCompSize);

if (ret_uncp == Z_OK){
    printf("uncompression ok\n");
    printf("uncompressed data : %s\n",(char *)UnCompDataBuff);
    }
if (ret_uncp == Z_MEM_ERROR)
    printf("uncompression memory error\n");
if (ret_uncp == Z_BUF_ERROR)
    printf("uncompression buffer error\n");
if (ret_uncp == Z_DATA_ERROR)
    printf("uncompression data error\n");

I always get a "compression error without compression" and I don't know why. And then I would like to know how to get 2 files with my data uncompressed.

+5
source share
3 answers

zip - , , . , zlib. zlib crc32, crc zip.

zlib , zip. , ( ), minizip contrib/minizip zlib distribution, , zip .

+12

Zlib ZIP . zlib gzip, , "", .zip.

(, libzip, ), .zip.

+4

, zlib , . , , , zip ( , rar, 7zip ..)

zip , zip, minizip - , .

minizip https://github.com/nmoinvaz/minizip , . , minizip.c miniunz.c , . (Minizip zlib )

I also finished creating a library that wraps minizip and adds a bunch of nice features to it and simplifies its use and focuses on objects. Allows you to do things like zip entire folders, streams, vectors, etc. As well as everything is completely in memory.

Repo with examples here: https://github.com/sebastiandev/zipper

Beta Preview: https://github.com/sebastiandev/zipper/releases/

The code looks something like this:

Zipper zipper("ziptest.zip");
zipper.add("somefile.txt");
zipper.add("myFolder");
zipper.close();
+2
source

All Articles