The three concepts zlib, Gzip, and zip are indeed confusing:
1. Gzip
Gzip is a data format in UNIX.
Gzip is built on zlib and includes a layer. Some additional information is added at the header and end.
Gzip-tar.gz.
Gzip is a File compression tool (or the compressed file format generated by the compression tool) designed to process a single
. Zlib is used when gzip compresses data in the file. To save information related to file properties, GZ
The IP address needs to save more header information in the compressed file (*. GZ), and zlib does not need to consider this. However, Gzip only applies
It is used for a single file, so we often see that the extension of the compressed package on Unix/Linux is * .tar.gz or *. tgz.
Is to use tar to package multiple files into a single file, and then use gzip to compress the results.
2. zlib
Zlib is an open-source library that provides functions for compressing and decompressing data in the memory.
Zlib is designed to process pure data (regardless of the data source ).
3. Zip
Zip is a data structure similar to RAR.
Zip is applicable to compressing multiple files (corresponding tools include PKZIP and WinZip). Therefore
It further contains information about the file directory structure, which is more information than the gzip header. However, you must note that multiple zip formats are available.
Compression Algorithm. Most of our common ZIP files are not compressed by zlib. The format of the compressed data and the size of Gzip
Different.
Java SDK provides support for the above three compression technologies: Inflater class and Deflater class directly use zlib library to compress data/
Decompress, The gzipinputstream class and the gzipoutputstream class provide support for the GZIP format, zipfile, Zi
Pinputstream and zipoutputstream are used to process ZIP files.
Therefore, you should select different compression technologies based on your specific needs: If you only need to compress/decompress data
You can use zlib directly. To generate a GZIP file or decompress the compression results of other tools, you must
It is processed using gzip, zip, and other related classes.
The following is my program, which first packs the files in a folder into a tar file and then compresses them with gzip.
/** <Br/> * the compressed file is in GZIP format. in Linux, <br/> * the compressed folder is used to generate the suffix ". <br/> * @ Param folderpath, path of the folder to be compressed <br/> * @ Param zipfilepath, path of the compressed file <br/> * @ Param zipfilename, name of the compressed file <br/> * @ throws bizexception <br/> **/<br/> Public static void compressedfiles_gzip (string folderpath, string targzipfilepath, string targzipfilename) <br/>{< br/> file srcpath = new file (folderpath); <br/> int length = srcpath. l Istfiles (). length; <br/> byte [] Buf = new byte [1024]; // you can specify the size of the read buffer. <br/> file [] files = srcpath. listfiles (); <br/> try <br/>{< br/> // create a compressed file output stream <br/> fileoutputstream fout = new fileoutputstream (targzipfilepath ); <br/> // create a tar compressed output stream <br/> taroutputstream tout = new taroutputstream (fout); <br/> for (INT I = 0; I <length; I ++) <br/>{< br/> string filename = srcpath. getpath () + file. separator + files [I]. getname (); <B R // open the file to be compressed as the file input stream <br/> fileinputstream fin = new fileinputstream (filename ); // filename is the full file path <br/> tarentry taren = new tarentry (files [I]); // new tarentry (File file) must be used here ); <br/> taren. setname (files [I]. getname (); // you need to reset the name here. The default value is full path. Otherwise, the package will contain full path. <br/> tout. putnextentry (taren); <br/> int num; <br/> while (num = fin. read (BUF ))! =-1) <br/>{< br/> tout. write (BUF, 0, num); <br/>}< br/> tout. closeentry (); <br/> fin. close (); <br/>}< br/> tout. close (); <br/> fout. close (); </P> <p> // create a compressed file output stream <br/> fileoutputstream gzfile = new fileoutputstream (targzipfilepath + ". GZ "); <br/> // create a gzip compressed output stream <br/> gzipoutputstream gzout = new gzipoutputstream (gzfile ); <br/> // open the file to be compressed as the file input stream <br/> fileinputstream Tarin = new fileinputstream (targzipfilepa Th); // targzipfilepath is the full file path <br/> int Len; <br/> while (LEN = Tarin. Read (BUF ))! =-1) <br/>{< br/> gzout. write (BUF, 0, Len); <br/>}< br/> gzout. close (); <br/> gzfile. close (); <br/> Tarin. close (); <br/>}catch (filenotfoundexception e) <br/>{< br/> system. out. println (E); <br/>} catch (ioexception e) <br/>{< br/> system. out. println (E); <br/>}< br/>