Java for byte array decompression (ZIP,GZIP,BZIP2,JZLIB)

Source: Internet
Author: User
Tags file copy

1. First import Bzip2.jar in the project; (Download address: http://download.csdn.net/detail/majian_1987/5997697)

2. Download Jzlib source code Zip package, extract, and then copy all com.jcraft.jzlib.* Java files to the project (even with a Java file copy), download address: Http://download.csdn.net/detail /majian_1987/5997737

3. Then write the following code:

Import Java.io.ByteArrayInputStream;
Import Java.io.ByteArrayOutputStream;
Import Java.io.DataOutputStream;
Import java.io.IOException;
Import Java.util.zip.GZIPInputStream;
Import Java.util.zip.GZIPOutputStream;
Import Java.util.zip.ZipEntry;
Import Java.util.zip.ZipInputStream;
Import Java.util.zip.ZipOutputStream;
Import Org.apache.tools.bzip2.CBZip2InputStream;
Import Org.apache.tools.bzip2.CBZip2OutputStream;
Import Com.jcraft.jzlib.JZlib;
Import Com.jcraft.jzlib.ZInputStream;
Import Com.jcraft.jzlib.ZOutputStream;
public class Test {/*** * Compressed GZIP * * @param data * @return/public static byte[] GZip (byte[] data) {byte[] b = null;
try {bytearrayoutputstream bos = new Bytearrayoutputstream ();
Gzipoutputstream gzip = new Gzipoutputstream (BOS);
Gzip.write (data);
Gzip.finish ();
Gzip.close ();
b = Bos.tobytearray ();
Bos.close ();
catch (Exception ex) {ex.printstacktrace ();} return B; /*** * Extract gzip * * @param data * @return/public static byte[] Ungzip (byte[] data) {byte[]b = null;
try {Bytearrayinputstream bis = new Bytearrayinputstream (data);
Gzipinputstream gzip = new Gzipinputstream (bis);
byte[] buf = new byte[1024];
int num =-1;
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
while (num = gzip.read (buf, 0, Buf.length))!=-1) {baos.write (buf, 0, num);} B = Baos.tobytearray ();
Baos.flush ();
Baos.close ();
Gzip.close ();
Bis.close ();
catch (Exception ex) {ex.printstacktrace ();} return B; /*** * Compressed Zip * * @param data * @return/public static byte[] Zip (byte[] data) {byte[] b = null; try {BYTEARRAYOUTPU
Tstream BOS = new Bytearrayoutputstream ();
Zipoutputstream zip = new Zipoutputstream (BOS);
ZipEntry entry = new ZipEntry ("Zip");
Entry.setsize (data.length);
Zip.putnextentry (entry);
Zip.write (data);
Zip.closeentry ();
Zip.close ();
b = Bos.tobytearray ();
Bos.close ();
catch (Exception ex) {ex.printstacktrace ();} return B; /*** * Extract Zip * * @param data * @return/public static byte[] UnZip (byte[] data) {byte[] b = null; try {
Bytearrayinputstream bis = new Bytearrayinputstream (data);
Zipinputstream zip = new Zipinputstream (bis);
while (zip.getnextentry ()!= null) {byte[] buf = new byte[1024]; int num =-1;
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
while (num = zip.read (buf, 0, Buf.length))!=-1) {baos.write (buf, 0, num);} B = Baos.tobytearray ();
Baos.flush ();
Baos.close ();
} zip.close ();
Bis.close ();
catch (Exception ex) {ex.printstacktrace ();} return B; /*** * Compressed BZIP2 * * @param data * @return/public static byte[] BZIP2 (byte[] data) {byte[] b = null; try {Bytearrayo
Utputstream BOS = new Bytearrayoutputstream ();
Cbzip2outputstream bzip2 = new Cbzip2outputstream (BOS);
Bzip2.write (data);
Bzip2.flush ();
Bzip2.close ();
b = Bos.tobytearray ();
Bos.close ();
catch (Exception ex) {ex.printstacktrace ();} return B; /*** * Decompression BZIP2 * * @param data * @return/public static byte[] UNBZIP2 (byte[] data) {byte[] b = null; try {Bytearra Yinputstream bis = new Bytearrayinputstream (daTA);
Cbzip2inputstream bzip2 = new Cbzip2inputstream (bis);
byte[] buf = new byte[1024];
int num =-1;
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
while (num = bzip2.read (buf, 0, Buf.length))!=-1) {baos.write (buf, 0, num);} B = Baos.tobytearray ();
Baos.flush ();
Baos.close ();
Bzip2.close ();
Bis.close ();
catch (Exception ex) {ex.printstacktrace ();} return B; /** * Converts a byte array to a 16-character string * * @param barray * @return/public static string Bytestohexstring (byte[] barray) {Stringbuffe
R sb = new StringBuffer (barray.length);
String stemp; 
for (int i = 0; i < barray.length i++) {stemp = integer.tohexstring (0xFF & Barray[i]); if (Stemp.length () < 2)
Sb.append (0);
Sb.append (Stemp.touppercase ());
return sb.tostring ();  /** * Compressed Data * * @param object * @return * @throws ioexception/public static byte[] Jzlib (byte[] object) {byte[] Data
= NULL;
try {bytearrayoutputstream out = new Bytearrayoutputstream (); Zoutputstream zout = new Zoutputstream (out, Jzlib.z_defaUlt_compression);
DataOutputStream objout = new DataOutputStream (zout);
Objout.write (object);
Objout.flush ();
Zout.close ();
data = Out.tobytearray ();
Out.close ();
catch (IOException e) {e.printstacktrace ();} return data; /** * Extract Compressed data * * @param object * @return * @throws ioexception/public static byte[] Unjzlib (byte[] object) {byte[
] data = null;
try {bytearrayinputstream in = new Bytearrayinputstream (object);
Zinputstream zIn = new Zinputstream (in);
byte[] buf = new byte[1024];
int num =-1;
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
while (num = zin.read (buf, 0, Buf.length))!=-1) {baos.write (buf, 0, num);} data = Baos.tobytearray ();
Baos.flush ();
Baos.close ();
Zin.close ();

In.close ();
catch (IOException e) {e.printstacktrace ();} return data;

public static void Main (string[] args) {String s = ' This is a test ';
Byte[] B1 = Zip (S.getbytes ());
System.out.println ("Zip:" + bytestohexstring (B1));
byte[] B2 = UnZip (B1); System.out.println ("UnZip:" + New String (B2));
byte[] B3 = bZip2 (S.getbytes ());
System.out.println ("BZIP2:" + bytestohexstring (b3));
byte[] B4 = unBZip2 (b3);
System.out.println ("UNBZIP2:" + new String (B4));
Byte[] B5 = GZip (S.getbytes ());
System.out.println ("BZIP2:" + bytestohexstring (B5));
Byte[] B6 = Ungzip (B5);
System.out.println ("UNBZIP2:" + new String (B6));
byte[] B7 = Jzlib (S.getbytes ());
System.out.println ("Jzlib:" + bytestohexstring (B7));
byte[] B8 = Unjzlib (b7); System.out.println ("Unjzlib:" + new String (B8));}

Output results

Zip : 504b03041400080008008b82cc3a000000000000000000000000030000007a69702bc9c82c5600a2448592d4e21200504b0708eae71e0d0e0000000e 000000504b010214001400080008008b82cc3aeae71e0d0e0000000e0000000300000000000000000000000000000000007a6970504b0506000000000 1000100310000003f0000000000
Unzip:this is a test
Bzip2:6839314159265359f81d428d0000061180400022600c00200021a34cd42180a9b16d11d0f177245385090f81d428d0
Unbzip2:this is a test
bzip2:1f8b08000000000000002bc9c82c5600a2448592d4e21200eae71e0d0e000000
Unbzip2:this is a test
jzlib:789c2bc9c82c5600a2448592d4e2120026330516
Unjzlib:this is a test

Related Article

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.