Use the zlib library to read a single compressed document

Source: Internet
Author: User
Introduction to Linux programming-Application of zlib

Published on and read 1104 Times [Font: large, medium, and small]

The gzip (*. GZ) file format is almost the standard format in Linux. Some people think that Bzip2 has a higher compression ratio than gzip. This statement is generally correct, but according to my personal experience, Bzip2 is not more compressed than gzip in more than half of the files. In a few cases, the gzip compression ratio is reversed.
It is higher than Bzip2.
Zlib is a function library that supports GZIP file formats. It makes access to the GZ file as easy as opening or closing the file. You can easily add support for the GZ file for your program.

Example: showgz. c

# Include
# Include
# Include

Void main (INT argc, char ** argv)
{
Gzfile zip;
Int C;

If (argc <2) return;

Zip = gzopen (argv [1], "rb ");
While (C = gzgetc (ZIP ))! = EOF) putchar (C );
Gzclose (ZIP );
}

Compile

Gcc-O showgz. C-LZ

Inspection

Gzip-9 <showgz. c> showgz.c.gz
./Showgz showgz.c.gz

The program content will be displayed. The function of showgz can be said to be gzip-DC.

Function Declaration

Gzfile gzopen (const char * path, const char * mode );
Open a gzip (*. GZ) file.
Mode parameters can be "rb" or "WB ".
It can also contain the compression degree such as "wb9 ".
Use 'F' to filter data, such as "wb6f ".
Use 'H' to specify the Huffman only compression, such as "wb1h"
Gzopen can also be used to read non-compressed gzip files. In this case, gzread directly reads the files without decompression.

Int gzread (gzfile file, voidp Buf, unsigned Len );
It is used in the same way as read.

Int gzwrite (gzfile file, const voidp Buf, unsigned Len );
It is used in the same way as write.

Int gzprintf (gzfile file, const char * format ,...);
Same as fprintf.

Char * gzgets (gzfile file, char * Buf, int Len );
Same as fgets.

Int gzputc (gzfile file, int C );
Same as fputc.

Int gzgetc (gzfile file );
The usage is the same as that of fgetc.

Int gzflush (gzfile file, int flush );
Same as fflush.

Z_off_t gzseek (gzfile file, z_off_t offset, int whence );
Whence does not support seek_end
If the file is enabled as "read", seek_set and seek_cur are supported both forward and backward, but it is very slow.
If the file is enabled as "write", only forward seek is supported.

Int gzrewind (gzfile file );
It works the same as gzseek (file, 0l, seek_set) and is only valid for reading.

Z_off_t gztell (gzfile file );
Returned value: current file location (extracted location)

Int gzeof (gzfile file );
Return Value: 1-EOF, 0-not EOF

Int gzclose (gzfile file );
Close an archive
Returned value: zlib error number

OK station, webmaster, Brian Lin

--
In the rivers and lakes, as long as you pick up the knife, it is a dream without failure.

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.