Inkfish original, do not reprint commercial nature, reproduced please indicate the source (http://blog.csdn.net/inkfish).
Compression is a common technique in programming, mostly for large file compression, data stream compression, and so on. In the Java class Library, the built-in jar, ZIP, GZIP, zlib, etc. support (see Java.util.zip, Java.util.jar package). In addition, the Org.apache.tools.tar, Org.apache.tools.zip, and org.apache.tools.bzip2 of Ant Ant.jar in the Apache project provide the support of tar, zip, and bzip2 respectively. The Apache Commons Compress project provides support for AR, bZIP, CPIO, Gzp, TAR, and zip. 7-zip provides compression in the LZMA format (public domain), QUICKLZ provides compression in QUICKLZ format (GPL), Oberhumer.com provides compression in LZO format (GPL), Hadoop-gpl-compression is used to package the C implementation of Lzo with JNI, providing more efficient lzo compression. (Source: Http://blog.csdn.net/inkfish)
Strictly speaking, TAR, AR, Cpio is not a compression software, but a packaging software, it can be a lot of files, folders packaged into a file for compression program compression. And we are familiar with the windows of the zip, RAR, is strictly a packaging and compression function of a format. (Source: Http://blog.csdn.net/inkfish)
Because I need to use a compression tool in the project to compress the data flow in the network, so the focus on the stream compression, and do not care about the compression of multiple files, which will also be reflected in the code. As a result, the code for this series is generally only applicable to compressing a stream or compressing a file. (Source: Http://blog.csdn.net/inkfish)
Lzma, Quicklz, Lzo because the provided class library does not support stream form compression or provide example too ugly to understand, so do not test. Here's another caveat, the Apache commons compress tar, zip, and bzip2, originally from Ant, but migrated and evolved across projects, with different APIs and performance. (Source: Http://blog.csdn.net/inkfish)
All formats offer compression and decompression in two ways, again reminding all of the code here is not suitable for compressing multiple files and folders. The following is an abstract compression, compression class: (Source: Http://blog.csdn.net/inkfish)
Package study.inkfish.compress; Import Java.io.File; Import java.io.IOException; Public abstract class Compress {public void Compress (file srcfile, file destfile) {destfile.getparentfile (). Mkdirs (); TR y {docompress (srcfile, destfile);} catch (IOException ex) {ex.printstacktrace ();}} public void decompress (file srcfile, file Destdir) {destdir.mkdirs (); try {dodecompress (srcfile, Destdir);} catch (Ioex Ception ex) {ex.printstacktrace ();}} protected int Bufferlen = 1024 * 1024;//buffer size:1mbyte protected abstract void docompress (file srcfile, file DestFile ) throws IOException; protected abstract void dodecompress (file srcfile, file destdir) throws IOException; }