Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using zlib to compress a directory

Tags:

c++

zip

zlib

I have a C++ application that requires using a standard compression format for a directory. I thought about using zip format. And therefore, zlib was obvious.

My problem is building the dictionary, i.e. compressing a directory containing some files with zlib to a standard zip file.

I have read that zlib does it but I don't understand how. I've checked minzip code. It seems to use gzip. I could build my own format but I want to be able to open these files using 7z for debugging (just in case).

What should I use in zlib to compress a directory?

[edit] In minzip I used minzip to compress 1 file into its gz version -> not zip but fine to me. (I will have that on various compilers in the world -- having a standard format is easier in case there is an issue on the client's platform)

Also, there is this code in main. It loops on a list of files and writes it to an output. But where is the information on the file location in the archive?

    do {
        if (uncompr) {
            if (copyout) {
                file = gzopen(*argv, "rb");
                if (file == NULL)
                    fprintf(stderr, "%s: can't gzopen %s\n", prog, *argv);
                else
                    gz_uncompress(file, stdout);
            } else {
                file_uncompress(*argv);
            }
        } else {
            if (copyout) {
                FILE * in = fopen(*argv, "rb");

                if (in == NULL) {
                    perror(*argv);
                } else {
                    file = gzdopen(fileno(stdout), outmode);
                    if (file == NULL) error("can't gzdopen stdout");

                    gz_compress(in, file);
                }

            } else {
                file_compress(*argv, outmode);
            }
        }
    } while (argv++, --argc);

Sorry if this is obvious.

like image 647
dzada Avatar asked Sep 10 '13 17:09

dzada


2 Answers

Look at minizip.c for an example of how to use minizip to create zip files. minizip is in the contrib directory of the zlib distribution, which means that it is third-party contributed code. It is not part of the zlib library.

Note that gzip is not zip. The zlib format is not zip. Raw deflate data is not zip. Those are all different formats. Raw deflate data is one of the compressed formats permitted within the zip format, and in fact is the most common. zlib is useful for processing zip files, but it does not do that by itself. zlib by itself provides only the compression and decompression engines and CRC calculation.

like image 172
Mark Adler Avatar answered Sep 18 '22 07:09

Mark Adler


Do not mix together zip - standard for archive, which can containe multiple files in it, and zlib - which is used in zip to compress the single file/data stream. To compress a directory you should use minizip/infozip or any other library which is compatible with ZIP archive format.

like image 31
Nickolay Olshevsky Avatar answered Sep 18 '22 07:09

Nickolay Olshevsky