Zip is used to archive and compress files. gzip does not archive files, but only compresses a single file. Therefore, on UNIX platforms, the tar command is usually used to create an archive file, then run gzip to compress the file.
The Java I/O class library also contains classes that can read and write compressed format streams. To provide the compression function, you just need to package them out of the existing I/O class. These classes are not reader and writer, but subclasses of inputstream and outstreamput. This is because the compression algorithm is for bytes rather than characters.
Related Classes and interfaces:
ChecksumInterface: The interface implemented by class adler32 and CRC32
Adler32: Use the alder32 algorithm to calculate the number of checksum
CRC32: Use the CRC32 algorithm to calculate the number of checksum
Checkedinputstream: Inputstream derived class. The checksum of the input stream is obtained to verify data integrity.
Checkedoutputstream: Outputstream derived class. The checksum of the output stream is obtained to verify data integrity.
Deflateroutputstream: The base class of the compression class.
Zipoutputstream: A subclass of deflateroutputstream that compresses data into a zip file.
Gzipoutputstream: A subclass of deflateroutputstream that compresses data into the GZIP file format.
Inflaterinputstream: Base class of the extract class
Zipinputstream: A subclass of inflaterinputstream that can decompress ZIP data.
Gzipinputstream: A subclass of inflaterinputstream that can decompress ZIP data.
ZipentryClass: Indicates ZIP file entries
ZipfileClass: this class is used to read entries from a zip file.
Use gzip to compress a single file
The gzip interface is relatively simple, so you can use it if you only need to compress a stream. Of course, it can compress the bytes stream and compress the byte stream. Below is a compressed text file in GBK encoding format.
The usage of the compression class is very simple. You only need to use gzipoutputstream or zipoutputstream to package the output stream, and then use gzipinputstream or zipinputstream to package the input stream. The rest are common I/O operations.
Package com.apache.gzip; import Java. io. bufferedinputstream; import Java. io. bufferedoutputstream; import Java. io. file; import Java. io. fileinputstream; import Java. io. filenotfoundexception; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. util. enumeration; import java.util.zip. CRC32; import java.util.zip. checkedinputstream; import java.util.zip. checkedoutputstream; import java.util.zip. d Eflater; import java.util.zip. zipexception; import java.util.zip. zipinputstream; import org.apache.tools.zip. zipentry; import org.apache.tools.zip. zipfile; import org.apache.tools.zip. zipoutputstream;/** use ant. jar, which can be used to compress a single file and directory, and whether to create a compressed source directory and a Chinese path * @ title: * @ description: zipcompress * @ version 1.2 */public class zipcompress {Private Static Boolean iscreatesrcdir = true; // whether to create the source directory/*** @ Param ARGs * @ throws ioexception */public static void main (string [] ARGs) throws ioexception {string src = "F: \ Chinese package"; // specify the compression source, it can be a directory or file string decompressdir = "F: \ depress"; // extract the path string archive = "F: \ Chinese compressed file .zip "; // compressed package path string comment = "Java zip test. "; // compressed package comments // ---- compressed file or directory writebyapachezipoutputstream (SRC, archive, comment);/** read and comment out the compressed file, because the Apache compression class is used, an error occurs when * decompression class is used in the Java class library. */readbyzipinputs cannot be run here. Tream (archive, decompressdir); // ---- use apace zipfile to read the compressed file readbyapachezipfile (archive, decompressdir );} /** Compress folders or files ** @ time 2012-3-9 09:32:35 create * @ Param SRC * @ Param archive * @ Param comment * @ throws filenotfoundexception * @ throws ioexception * @ author jiangzhenming */public static void writebyapachezipoutputstream (string SRC, string archive, string comment) throws filenotfoundex Ception, ioexception {// ---- compressed file: fileoutputstream F = new fileoutputstream (archive); // use the specified checksum to create the output stream checkedoutputstream csum = new checkedoutputstream (F, new CRC32 (); zipoutputstream ZOS = new zipoutputstream (csum); // supports Chinese ZOS. setencoding ("GBK"); bufferedoutputstream out = new bufferedoutputstream (ZOS); // set the compressed package comment ZOS. setcomment (comment); // enable ZOS compression. setmethod (zipoutputstream. deflated); // The compression level is the strongest Compression, but it takes more time to ZOS. setlevel (Deflater. best_compression); file srcfile = new file (SRC); If (! Srcfile. exists () | (srcfile. isdirectory () & srcfile. list (). length = 0) {Throw new filenotfoundexception ("file must exist and zip file must have at least one entry. ");} // obtain the parent directory of the compression source src = SRC. replaceall ("\\\\", "/"); string prefixdir = NULL; If (srcfile. isfile () {prefixdir = SRC. substring (0, SRC. lastindexof ("/") + 1);} else {prefixdir = (SRC. replaceall ("/$", "") + "/");} // if it is not the root directory if (PR Efixdir. indexof ("/")! = (Prefixdir. length ()-1) & iscreatesrcdir) {prefixdir = prefixdir. replaceall ("[^/] +/$", "");} // start to compress writerecursive (ZOS, out, srcfile, prefixdir); out. close (); // Note: The Checksum must be prepared after the stream is closed. Be sure to use system after the stream is closed. out. println ("checksum:" + csum. getchecksum (). getvalue (); bufferedinputstream Bi;}/*** use org.apache.tools.zip. zipfile: Decompress the file. It is similar to * java.util.zip in the Java class library. zipfile is used in a new way, except for the * interface that sets the encoding method. ** Note: Apache does not provide the zipinputstream class, so only zipfile * provided by Apache can be used to read compressed files. * @ Param archive path * @ Param decompressdir extract path * @ throws ioexception * @ throws filenotfoundexception * @ throws zipexception */public static void destroy (string archive, string decompressdir) throws ioexception, filenotfoundexception, zipexception {bufferedinputstream Bi; zipfile ZF = new zipfile (archive, "GBK"); // supports Chinese enumeration E = ZF. getentries (); While (E. hasmoreelements ()){ Zipentry ze2 = (zipentry) E. nextelement (); string entryname = ze2.getname (); string Path = decompressdir + "/" + entryname; If (ze2.isdirectory () {system. out. println ("creating extract directory-" + entryname); file decompressdirfile = new file (PATH); If (! Decompressdirfile. exists () {decompressdirfile. mkdirs () ;}} else {system. out. println ("creating extract file-" + entryname); string filedir = path. substring (0, path. lastindexof ("/"); file filedirfile = new file (filedir); If (! Filedirfile. exists () {filedirfile. mkdirs ();} bufferedoutputstream Bos = new bufferedoutputstream (New fileoutputstream (decompressdir + "/" + entryname); Bi = new bufferedinputstream (ZF. getinputstream (ze2); byte [] readcontent = new byte [1024]; int readcount = Bi. read (readcontent); While (readcount! =-1) {Bos. write (readcontent, 0, readcount); readcount = Bi. read (readcontent);} Bos. close () ;}} ZF. close ();}/*** decompress the file using the zipinputstream class in Java API. However, if the file is compressed using * org.apache.tools.zip. * java.util.zip. this method cannot be used when zipoutputstream is used, because the encoding method * is inconsistent, and the following exception is thrown during runtime: * Java. lang. illegalargumentexception * at java.util.zip. zipinputstream. getutf8string (zipinputstream. java: 290 )** Of course, if the compressed package uses java.util.zip of the Java library. zipoutputstream * compression is not a problem, however, it does not support Chinese ** @ Param archive compressed package path * @ Param decompressdir decompression path * @ throws filenotfoundexception * @ throws ioexception */public static void readbyzipinputstream (string archive, string decompressdir) throws filenotfoundexception, ioexception {bufferedinputstream Bi; // ---- decompress the file (the decompressing of the ZIP file is actually reading data from the input stream): system. out. println ("start to read compressed files"); File Inputstream Fi = new fileinputstream (archive); checkedinputstream csumi = new checkedinputstream (FI, new CRC32 (); zipinputstream in2 = new zipinputstream (csumi); Bi = new bytes (in2 ); java.util.zip. zipentry ze; // ZIP file entry // traverse the file entry while in the compressed package (ze = in2.getnextentry ())! = NULL) {string entryname = Ze. getname (); If (Ze. isdirectory () {system. out. println ("creating extract directory-" + entryname); file decompressdirfile = new file (decompressdir + "/" + entryname); If (! Decompressdirfile. exists () {decompressdirfile. mkdirs () ;}} else {system. out. println ("Creating an decompression file-" + entryname); bufferedoutputstream Bos = new bufferedoutputstream (New fileoutputstream (decompressdir + "/" + entryname )); byte [] buffer = new byte [1024]; int readcount = Bi. read (buffer); While (readcount! =-1) {Bos. write (buffer, 0, readcount); readcount = Bi. read (buffer);} Bos. close () ;}} bi. close (); system. out. println ("checksum:" + csumi. getchecksum (). getvalue ();}/*** recursive compression ** use org.apache.tools.zip. zipoutputstream class for compression. The advantage of zipoutputstream is that it supports Chinese paths. * Java class library java.util.zip. when zipoutputstream compresses the Chinese file name, garbled characters may occur in the compressed package. * The usage of this class in Apache and Java class libraries is new, but the encoding method can be set. ** @ Param ZOS * @ Param Bo * @ Param srcfile * @ Param prefixdir * @ throws ioexception * @ throws filenotfoundexception */Private Static void writerecursive (zipoutputstream ZOS, Baibo, file srcfile, string prefixdir) throws ioexception, filenotfoundexception {zipentry; string filepath = srcfile. getabsolutepath (). replaceall ("\\\\","/"). replaceall ("//", "/"); If (srcfile. Isdirectory () {filepath = filepath. replaceall ("/$", "") + "/";} string entryname = filepath. replace (prefixdir ,""). replaceall ("/$", ""); If (srcfile. isdirectory () {If (! "". Equals (entryname) {system. out. println ("creating directory-" + srcfile. getabsolutepath () + "entryname =" + entryname); // if it is a directory, add/zipentry = new zipentry (entryname + "/") after the write directory; ZOS. putnextentry (zipentry);} file srcfiles [] = srcfile. listfiles (); For (INT I = 0; I <srcfiles. length; I ++) {writerecursive (ZOS, Bo, srcfiles [I], prefixdir) ;}} else {system. out. println ("Writing files-" + srcfile. getabsolute PATH () + "entryname =" + entryname); bufferedinputstream Bi = new bufferedinputstream (New fileinputstream (srcfile )); // start writing new zip file entries and locate the stream to the beginning of the entry data zipentry = new zipentry (entryname); ZOS. putnextentry (zipentry); byte [] buffer = new byte [1024]; int readcount = Bi. read (buffer); While (readcount! =-1) {Bo. write (buffer, 0, readcount); readcount = Bi. read (buffer) ;}// Note: when using a buffer stream to write a compressed file, you must refresh it after a condition is complete, no // some contents may be saved to the following entries. flush (); // close Bi after the file is read. close ();}}}