Apache in Commonscompress provides a well-known tar, cpio, zip and other compression algorithm implementation, the most abundant is the ZIP implementation!
Commons Compress has built a compression algorithm factory class Compressorstreamfactory for compression (gzip and BZIP2). This class makes it easy to build the input and output streams for gzip and BZIP2, with the keywords "gz" and "bzip2", respectively.
Gzip
Java code
Gzipcompressorinputstream
Compressorinputstream Gzipin = Newcompressorstreamfactory ()
. Createcompressorinputstream ("GZ", is);
Gzipcompressoroutputstream
Compressoroutputstream gzipout = Newcompressorstreamfactory ()
. Createcompressoroutputstream ("GZ", OS);
BZip2
Java code
Bzip2compressorinputstream
Compressorinputstream bzip2in =new compressorstreamfactory ()
. Createcompressorinputstream ("bzip2", is);
Bzip2compressoroutputstream
Compressoroutputstream bzip2out= New Compressorstreamfactory ()
. Createcompressoroutputstream ("bzip2", OS);
The following is a complete example of a simple implementation of bzip2 compression via the apachecommons compress.
The package that needs to be imported is: Commons-compress-1.9.jar
packagetest.ffm83.commons.compress;
Importjava.io.ByteArrayInputStream;
Importjava.io.ByteArrayOutputStream;
Importjava.io.File;
Importjava.io.FileInputStream;
Importjava.io.FileOutputStream;
Importjava.io.InputStream;
Importjava.io.OutputStream;
Importorg.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
Importorg.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
/* Simple implementation of BZIP2 compression via Apache Commons compress
* Part of the code from the Internet
* @author Fan Fangming
* */
Publicclass Bzip2utils {
public static final int BUFFER = 1024;
public static final Charsequence EXT = ". bz2";
/**
* Data compression
*
* @param data
* @return
* @throws Exception
*/
public static byte[] Compress (byte[] data) throws Exception {
Bytearrayinputstream Bais = newbytearrayinputstream (data);
Bytearrayoutputstream BAOs = Newbytearrayoutputstream ();
Compression
Compress (Bais, BAOs);
byte[] Output =baos.tobytearray ();
Baos.flush ();
Baos.close ();
Bais.close ();
return output;
}
/**
* File compression
*
* @param file
* @param Delete
* Whether to delete the original file
* @throws Exception
*/
public static void Compress (File file,boolean delete) throws Exception {
FileInputStream FIS = newfileinputstream (file);
FileOutputStream fos = Newfileoutputstream (File.getpath () + EXT);
Compress (FIS, FOS);
Fis.close ();
Fos.flush ();
Fos.close ();
if (delete) {
File.delete ();
}
}
/**
* Data compression
*
* @param is
* @param os
* @throws Exception
*/
public static void Compress (InputStream is,outputstream OS)
Throws Exception {
Bzip2compressoroutputstream Gos = newbzip2compressoroutputstream (OS);
int count;
byte data[] = new Byte[buffer];
while ((count = is.read (data, 0, BUFFER))!=-1) {
Gos.write (data, 0, count);
}
Gos.finish ();
Gos.flush ();
Gos.close ();
}
/**
* File compression
*
* @param path
* @param Delete
* Whether to delete the original file
* @throws Exception
*/
public static void Compress (String path,boolean delete) throws Exception {
File File = new file (path);
Compress (file, delete);
}
/**
* Data Decompression
*
* @param data
* @return
* @throws Exception
*/
public static byte[] Decompress (byte[]data) throws Exception {
Bytearrayinputstream Bais = newbytearrayinputstream (data);
Bytearrayoutputstream BAOs = Newbytearrayoutputstream ();
Unzip
Decompress (Bais, BAOs);
data = Baos.tobytearray ();
Baos.flush ();
Baos.close ();
Bais.close ();
return data;
}
/**
* File Decompression
*
* @param file
* @param Delete
* Whether to delete the original file
* @throws Exception
*/
public static void Decompress (File File,boolean delete) throws Exception {
FileInputStream FIS = newfileinputstream (file);
FileOutputStream fos = Newfileoutputstream (File.getpath (). replace (EXT,
""));
Decompress (FIS, FOS);
Fis.close ();
Fos.flush ();
Fos.close ();
if (delete) {
File.delete ();
}
}
/**
* Data Decompression
*
* @param is
* @param os
* @throws Exception
*/
public static void Decompress (Inputstreamis, OutputStream OS)
Throws Exception {
Bzip2compressorinputstream GIS = Newbzip2compressorinputstream (IS);
int count;
byte data[] = new Byte[buffer];
while ((count = Gis.read (data, 0,buffer))! =-1) {
Os.write (data, 0, count);
}
Gis.close ();
}
/**
* File Decompression
*
* @param path
* @param Delete
* Whether to delete the original file
* @throws Exception
*/
public static void Decompress (String Path,boolean delete) throws Exception {
File File = new file (path);
Decompress (file, delete);
}
public static void Main (string[] args) throws exception{
Stringinputstr = "http://blog.csdn.net/ffm83/article/details/42240513";
Byte[]input = Inputstr.getbytes ();
System.out.println ("Original: \ T" + inputstr);
System.out.println ("Length: \ T" + input.length);
byte[] data = compress (input);
System.err.println ("after compression: \ t");
System.err.println ("Length: \ T" + data.length);
byte[] Output = decompress (data);
String outputstr = newstring (output);
System.err.println ("after decompression: \ t" + outputstr);
System.err.println ("Length: \ T" + output.length);
}
}
The results of the operation are as follows:
Original: http://blog.csdn.net/ffm83/article/details/42240513
Length: 51
After compression:
Length: 86
After decompression: http://blog.csdn.net/ffm83/article/details/42240513
Length: 51
Apache in Commons compress introduction and simple application