Using zlib to compress a directory

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 10:40:13

问题


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.


回答1:


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.




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/18725424/using-zlib-to-compress-a-directory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!