Using zlib to compress a directory

17,935

Solution 1

I was misunderstanding a bit ZIP and the compression.

Finally I decided to implement the ZIP archive format following wikipedia, this page. And I used extensively HxD tool on windows and 7z to test a lot.

At the end I found out that the format of the archive is simple, and I support only 1 disk, N files with directories, and compression based on zlib. I also use zlib for crc32 checksum.

so if someone has the problem in the future, see the zip format on wikipedia and use zlib for crc32, then if you want compress your chunks with zlib.

Thanks to all of you

Solution 2

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.

Solution 3

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.

Share:
17,935
dzada
Author by

dzada

Updated on June 25, 2022

Comments

  • dzada
    dzada almost 2 years

    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.

  • dzada
    dzada over 10 years
    so as I already tried with minzip how does it manage this? I've succeeded in compressing 1 file into a ".gz" file, that I can open with 7z. But I don't understand where are the info of the files info in the .gz file.
  • Admin
    Admin over 10 years
    There is none. gzip just compresses raw data - it's not an archive format; it doesn't have any concept of files.
  • dzada
    dzada over 10 years
    Ok, and is there a lib that I can use to build this dictionnary?
  • dzada
    dzada over 10 years
    I found libzip, but I don't know if this is reliable and it doesnt build without fixes on my OS (Mac and Win)
  • dzada
    dzada over 10 years
    thank you I've read the zip format standard (quickly) today, I got this, zip is the archive format. In fact I need more the archive format than the compression indeed. I want zip because it is built in in all OSs. I'm trying to see if it is easier to write myself the zip format packaging, and use zlib to compress. Best