Compression and decompression of files using the Zlib library

Source: Internet
Author: User

zlib Introduction

Zlib is a library of functions for data compression, developed by Jean-loup Gailly and Mark Adler, and published on May 1, 1995 in the first edition of version 0.9. Zlib uses the DEFLATE algorithm, originally written for the Libpng function library, and later widely used by many software. This library is free software and uses zlib authorization

Configuration

I downloaded the zlib DLL file directly from the Internet, the compressed package has several parts, respectively, the header file Zconf.h,zlib.h;lib file Zdll.lib;dll file Zlib1.dll.
The configuration process is simple, contains the header file directory or the introduction header file, the Configuration Vs Project property page, in the linker-general-Additional library directory, add the Lib file directory, while in the linker-input-Additional dependencies, Add Zdll.lib, and finally put the Zlib1.dll into the debug directory and EXE file together.

Example
#include <stdio.h> #include "zlib.h" int main () {/    * raw data */    unsigned char strsrc[] = "Hello world! AAAAA bbbbb CCCCC ddddd Chinese test yes ";    unsigned char buf[1024] = {0};    unsigned char strdst[1024] = {0};    unsigned long Srclen = sizeof (STRSRC);    unsigned long buflen = sizeof (BUF);    unsigned long Dstlen = sizeof (STRDST);    printf ("Src string:%s\nlength:%ld\n", STRSRC, Srclen);    /* Compression *    /compress (buf, &buflen, STRSRC, Srclen);    printf ("After compressed length:%ld\n", buflen);    /* Unzip *    /Uncompress (STRDST, &dstlen, buf, Buflen);    printf ("After uncompressed length:%ld\n", Dstlen);    printf ("Uncompressed string:%s\n", STRDST);    return 0;}


The output is:

Src string:hello world! aaaaa bbbbb ccccc ddddd 中文测试 yesLength:50After Compressed Length:49After UnCompressed Length:50UnCompressed String:hello world! aaaaa bbbbb ccccc ddddd 中文测试 yes

Here, Zlib just compresses a piece of memory, compresses it and puts it in another memory, which is far from the compressed file or even the folder's target.
To be exact, zlib may not be a library for zip files, it's just a library for gzip and deflate algorithms. It provides a method called Minizip (Contrib\minizip) to give an example of how to manipulate a ZIP file.

Minizip

Minizip is an additional instance of a zip and unzip file. Minizip.c and MINIUNZ.C respectively actual compression, decompression program, after removing the main program minizip.c and MINIUNZ.C, the rest of us can be seen as a zlib of the upper library, which encapsulates the zip file format related operations.

Specific operations for compression

Compression involves the following APIs:

  • zipopen64
  • zipClose
  • zipopennewfileinzip
  • zipclosefileinzip
  • zipwriteinfileinzip
  • Use ZipOpen64 to open/create a ZIP file, and then start traversing the files to be placed in the archive. For each file, call Zipopennewfileinzip once, then start reading the original file data and use Zipwriteinfileinzip to write to the ZIP file. The third parameter of Zipopennewfileinzip is a zip_fileinfo structure, which can be completely zeroed, where dosdate can be used to fill in a time (LastModificationTime). Its second parameter is the file name in the ZIP, which preserves the path in order to keep the directory structure.

    Unzip the specific operation

    Decompression involves the following APIs:

  • unzopen64
  • unzClose
  • unzgetglobalinfo64
  • unzgotonextfile
  • unzgetcurrentfileinfo64
  • unzopencurrentfile
  • unzclosecurrentfile
  • unzreadcurrentfile
  • After you open a ZIP file, you need to use UNZGETGLOBALINFO64 to get some information about how many files are included in the package, and so on. What we need now is the number of this file. It then starts traversing the files in the ZIP, initially automatically positioned in the first file, and then using Unzgotonextfile to jump to the next file after finishing one. For each internal file, unzGetCurrentFileInfo64 can be used to check the internal filename. This file name is the same as the second parameter of the Zipopennewfileinzip, so it is possible to include a path. It is also possible to end with the path delimiter (/), indicating that this is a directory entry (in fact, the compression operation can also be written to the directory for such internal files, which did not do). So the next step is to create a (multilevel) directory based on the situation. The third parameter of UNZGETCURRENTFILEINFO64 is the UNZ_FILE_INFO64 structure, which also contains a dosdate information that can restore the file time. For non-directory internal files, open with Unzopencurrentfile, then Unzreadcurrentfile read the contents of the file and write to the real file. Unzreadcurrentfile returns 0 indicating the end of the file read.

    Resources

    Minizip

    Reprint please indicate the author Jason Ding and its provenance
    GitHub home page (http://jasonding1354.github.io/)
    CSDN Blog (http://blog.csdn.net/jasonding1354)
    Jane Book homepage (http://www.jianshu.com/users/2bd9b48f6ea8/latest_articles)

    Compression and decompression of files using the Zlib library

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.