Java Compression Class Library usage-1. General statement

Source: Internet
Author: User

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; }

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.