Introduction to Linux programming-Application of zlib

Source: Internet
Author: User
Introduction to Linux programming-Use of zlib-general Linux technology-Linux programming and kernel information. For details, see the following. Introduction to Linux programming-Application of zlib

The gzip (*. gz) file format is almost the standard format in Linux. Some people think that the compression ratio of bzip2

Gzip is high. This statement is generally correct, but according to my personal experience, half of it

For the above files, bzip2 does not have a higher compression ratio than gzip. 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 if it were an open file.

Easily, you can easily add gz support 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 file formats. In this case, gzread will directly read

.




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. However

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
Related Article

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.