Java Decompression Technology (iii) BZIP2 compression-Decompression

Source: Internet
Author: User
Tags uncompress


Implementation of the Java decompression technology GZIP ZIP BZIP2


Unlike Gzip ZIP, BZIP2 is not implemented in Java, BZIP2 implementations are provided by Apache Commons-compress.jar.


About Commons Compress please visit: http://commons.apache.org/proper/commons-compress/


or directly on the code


< Span style= "font-size:18px" >

Package Com.ljh.bzip2;import Java.io.bytearrayinputstream;import Java.io.bytearrayoutputstream;import java.io.File ; Import Java.io.fileinputstream;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.inputstream;import Java.io.outputstream;import Org.apache.commons.compress.compressors.compressorexception;import Org.apache.commons.compress.compressors.bzip2.bzip2compressorinputstream;import org.apache.commons.compress.compressors.bzip2.bzip2compressoroutputstream;/** * @Desc: BZIP2 compression tool (test result: suitable for larger compression,  Usage similar to gzip) * @author LJH * @date 2015-4-14 a.m. 9:39:14 */public class Bzip2utils {private static final int BUFFER = 8;public Static final String EXT = ". bz2";/** * @Description: GZIP data compression * @author (LJH) @date 2015-4-13 pm 6:00:52 * @param data *  @return * @throws IOException * @return byte[] * @throws compressorexception */public static byte[] Compress (byte[) data) Throws IOException, compressorexception {bytearrayinputstream Bais = new Bytearrayinputstream (data); ByteArrayOutputStream BAOs = new Bytearrayoutputstream () compress (Bais, BAOs);//input stream compressed to output stream Baos.flush (); Baos.close (); Bais.close (); return Baos.tobytearray ();} /** * @Description: GZIP data compression * @author (LJH) @date 2015-4-14 a.m. 9:26:30 * @param is * @param os * @throws IOException * @r eturn void * @throws compressorexception */public static void Compress (InputStream is, OutputStream os) throws Ioexceptio N, compressorexception {bzip2compressoroutputstream bzip2os = new Bzip2compressoroutputstream (OS, 8);// Compressoroutputstream Bzip2os = new Compressorstreamfactory (). Createcompressoroutputstream ( COMPRESSORSTREAMFACTORY.BZIP2, OS); int count;byte data[] = new Byte[buffer];while ((count = is.read (data, 0, Data.length) )! =-1) {bzip2os.write (data, 0, count);} Bzip2os.finish (); Bzip2os.flush (); Bzip2os.close ();} /** * @Description: GZIP Data uncompressed * @author (LJH) @date 2015-4-13 pm 6:00:42 * @param data * @return * @throws IOException * @ return byte[] */public static byte[] uncompress (byte[] data) throws IOException{Bytearrayinputstream Bais = new Bytearrayinputstream (data); Bytearrayoutputstream BAOs = new Bytearrayoutputstream () uncompress (Bais, BAOs); Bais.close (); return Baos.tobytearray ();} /** * @Description: GZIP Data uncompressed * @author (LJH) @date 2015-4-14 a.m. 9:26:51 * @param is * @param os * @throws IOException * @ return void */private static void Uncompress (InputStream is, OutputStream os) throws IOException {Bzip2compressorinputstr EAM bzip2is = new Bzip2compressorinputstream (IS), int count;byte data[] = new Byte[buffer];while ((count = Bzip2is.read (dat A, 0, data.length))! =-1) {os.write (data, 0, count);} Os.flush (); Os.close (); Bzip2is.close ();} /** * @Description: File compression * @author (LJH) @date 2015-4-14 a.m. 9:28:46 * @param file * @throws Exception * @return void * @thr  oWS compressorexception */public static void compress (file file) throws IOException, compressorexception {compress (file, true);} /** * @Description: File compression * @author (LJH) @date 2015-4-14 a.m. 9:29:21 * @param file * @param Delete * YesNo Delete source file * @throws Exception * @return void * @throws IOException * @throws compressorexception */public static void Compre  SS (File file, Boolean delete) throws IOException, compressorexception {if (!file.exists ()) {//file does not exist}fileinputstream FIS = New FileInputStream (file); FileOutputStream fos = new FileOutputStream (File.getpath () + EXT) compress (FIS, FOS); Fis.close (); Fos.flush (); Fos.close (); if (delete) {file.delete ();}} /** * @Description: File compression * @author (LJH) @date 2015-4-14 Morning 9:32:52 * @param path * source file path * @throws Exception * @return void * @throws compressorexception */public static void compress (String path) throws IOException, Compressorexcep tion {Compress (path, true);}            /** * @Description: File compression * @author (LJH) @date 2015-4-14 Morning 9:33:18 * @param path * source file path * @param Delete * Delete source file * @throws Exception * @return void * @throws IOException * @throws compressorexception */public static Voi D Compress (String path, Boolean delete) throws IOException, Compressorexception {File File = new file (path); Compress (file, delete);} /** * @Description: File extract * @author (LJH) @date 2015-4-14 a.m. 9:35:03 * @param file * @throws Exception * @return void */pub Lic static void uncompress (file file) throws IOException {uncompress (file, true);} /** * @Description: File extract * @author (LJH) @date 2015-4-14 a.m. 9:35:44 * @param file * @param Delete * Delete source file * @ Throws Exception * @return void * @throws ioexception */public static void uncompress (file file, Boolean delete) throws IO Exception {if (!file.exists ()) {///file does not exist}fileinputstream FIS = new FileInputStream (file); FileOutputStream fos = new FileOutputStream (File.getpath (). Replace (EXT, "")); uncompress (FIS, FOS); Fis.close (); Fos.flush (); Fos.close (); if (delete) {file.delete ();}} /** * @Description: File extract * @author (LJH) @date 2015-4-14 a.m. 9:37:48 * @param Path * @throws Exception * @return void */pub Lic static void Uncompress (String path) throws IOException {uncompress (path, true);} /** * @Description: File decompression * @author (LJH) @date 2015-4-14 Morning 9:38:03 * @param path * @param Delete * Delete source files * @throws Exception * @retur n void */public static void uncompress (String path, Boolean delete) throws IOException {File File = new file (path); uncompr ESS (file, delete);} /** * Tips: * Commons compress not only supports BZIP2 algorithm implementation, but also supports GZIP algorithm implementation. * For the GZIP algorithm implementation, there is basically no difference from Java native implementations.  Its source code analysis, just to do a simple packaging. * However, it is necessary to mention that Commons compress has built a compression algorithm factory class Compressorstreamfactory for compression (gzip and BZIP2).  * This class makes it easy to build gzip and BZIP2 input and output streams, with the keywords "gz" and "bzip2" respectively. */}


Test code:


@org. Junit.testpublic final void TestBZIP2 () throws IOException, compressorexception {byte[] bytes = " Alex Everyone Karma card crash of a AH the card of the death of the crash of the feeling opened on the AO Trench Assad gas song of the third tick tick tick tick ticking ". GetBytes ();//bytes compression, Verify the compression effect comparison System.out.println ("before compression:"); System.out.println (bytes.length); for (byte b:bytes) {System.out.print (b + "");} System.out.println (); SYSTEM.OUT.PRINTLN ("compressed:"); byte[] bytes2 = bzip2utils.compress (bytes); System.out.println (bytes2.length); for (byte b:bytes2) {System.out.print (b + "");} System.out.println (); System.out.println ("After decompression:");        byte[] Byte22 = bzip2utils.uncompress (bytes2);        System.out.println (byte22.length);        for (byte b:byte22) {        System.out.print (b + "");}                Compression time is long, test compressed 12305KB files when 17.203s, compressed size 383kb//        bzip2utils.compress ("F:\\activity_win9.bmp", false);        Decompression Time 4.667s        bzip2utils.uncompress ("f:\\activity_win9.bmp.bz2", false);//Unzip the same as the source file        // To compress folders, refer to Ziputils self-implementation}



Commons Compress not only supports BZIP2 algorithm implementation, but also supports GZIP algorithm implementation. For the GZIP algorithm implementation, there is basically no difference from Java native implementations. Its source code analysis, just to do a simple packaging.
It is important to mention, however, that Commons compress has built a compression algorithm factory class for compression (gzip and BZIP2)compressorstreamfactory. This class makes it easy to build the input and output streams for gzip and BZIP2, with the keywords "gz" and "bzip2", respectively.
GZip

Gzipcompressorinputstream        Compressorinputstream gzipin = new Compressorstreamfactory (). Createcompressorinputstream ("GZ", is);  Gzipcompressoroutputstream  Compressoroutputstream gzipout = new Compressorstreamfactory (). Createcompressoroutputstream ("GZ", OS);

BZip2

Bzip2compressorinputstream  Compressorinputstream bzip2in = new Compressorstreamfactory (). Createcompressorinputstream ("bzip2", is);    Bzip2compressoroutputstream  


gzip and BZIP2 in the algorithm implementation steps basically no difference, if necessary unified, can be implemented according to the above code!  



Click I download the relevant source code



Java Decompression Technology (iii) BZIP2 compression-Decompression

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.