Java Compression Technology

Source: Internet
Author: User
Tags deflater

Package zip;

Import Java.io.ByteArrayOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Java.util.zip.Deflater;
Import Java.util.zip.DeflaterOutputStream;
Import Java.util.zip.Inflater;
Import Java.util.zip.InflaterInputStream;

public class Test {


public static void Main (string[] args) {

String inStr = "@UEsDBBQACAgIAFZ2dUkAAAAAAAAAAAAAAAABAAAAMO1dXXPTxhr +k7npcjvasneaghu7fbwk05io"
+ "MZAQBJKJBDL2/IXTJISRLIGSHBKSC06MFEHPOQPLAMMCM6UQIIQFU8T2RVGLXWKL7BTKLNIJNEJX"
+ "Owpw7vvsh1abxt3p+64t5auruwzxwljuthfyxx6sp5codsxzaigrzqe+phth/knu3knfeavelgsa"
+ "Kbhji5kzejndkid50wzwmceiog0ssynqt0udjtgoeqexpvqh311uivsiqco50ddpzspnl6yankfu"
+ "Eiqqyt3dskz/dunsuym2q41k0mmfztscxavaq6hwhiaucp7isor+kswkzayjjzumz5qi/vskjbu4"
+ "KVIY2KBLOPG07TZOKSXYWGDOLMSMDZM6YK00RZQ1WJ1IFMZT5NQWEQEXPWZ+H6IQS2F6T3ZZNS1G"
+ "Zaxtmzizspkbo+w1zqntxls6mjogqwofexl2qjtai5sizzngcmtqy81rzxlqwp/xc5eykxwj5yh8"
+ "5ghmyrycs5voy8xqyoyj3itnm6ymmgwysqmmdlvoq2jp1ty4bgzcqgqnmmvurytzrzbtrisrtgya"
+ "kxgkx06et3hjb4umhlstsgk9r9j4zjxdfijbwdygcunaqsrmiw2riubcnlssr5eeyzoznqcjab9k"
+ "IG9USZNWYXLXH8DLSTRSYMMOLIBNIRWFT7XFX9YNJ+TPNTUWXJDEVXUKPP2CGDKYEL6FW2R8FKNF"
+ "VL9/+R7+4L7T7UV1U6+I+V/MAEQFNV20OQPF/L6XVKLFNKWT1B68OK2ZARW7T2GFTADRHHAGPVLD"
+ "tt+uv1+i5t+tptimj0+ru/r8i0+r09w3t/ufbjzu/wfv+ppn+m8p9fk7tam2gtq6pvejvvxw/d5n"
+ "2sf6z4+r79/ufl2nf6bp/wtcgo90xhn5pjvk6bp6/rsbb1wfn2lm/b/6yzy1aiz96xwwpo+c3otz"
+ "63P94RMTSJUIT0WHOI/9PC8+098SU4VR9EJK49GTXUS/BMSDDVU0I+RKHX32FU2CPMLFXHOE+1D9"
+ "4RFZMCZOU/DOXWZND+5U1P89WYRSNTCF//ZX5PUU66G/W6B9RZ/9SFBYUEPJFGPTP/RQPP77BXPN"
+ "Dhromzqe7/jjwrl+8mv1zzi+29ryn7xf59uph41h3l9bo66+v8pk6i9e0oqg7rq++311zbq68ry2"
+ "Oevnpzy3r996zz7fnxpn1zvf2mb1zbb6bkr/58p6/a/0zpwftizzgzzc+foycaxzym061py4dhio"
+ "Kaff83yfeulzsdode5hijpuv9cvaeswxloq2so69deqxhwsfwpnwmyq0meryx0dpmcc6dqzjfhni"
+ "G+QGGK6R48GQQGZKOKIKIAQQHNAA2I/SSAFOEICAGGGASQMTPQAYLNQZBKHAQOBIMYQKKAXARSHP"
+ "giwajetkwcklobw5c5acqplcbecbuhcqakckwm0xhqms/z2heibnsi55ttfzzymspgeoz3ogzm8z"
+ "Kj1nsmizygitirpu5zlb3kvo9a6wkpceuzyqzzilror056odtijahance4frnpvjrjz7+w3svmpz"
+ "ZLHSEIDUGYXM5FAEQ0QCQ0KXLOSQ5OI0DQVCUIPDWY5OMENPF5TMUMYFZTG0IGTVPRGOZEUKY5GC"
+ "Ioq4kherpeck8oevul3ouruih67akle4vqttjw4vq3uz2yuuliocgpxbd94ot4+5qtc8mjq6qrxd"
+ "QEXGYQGDCKQXKM5XYGP50DXQSMSAOKHJJY3RQDY5LSXA7QOMZSHAGVALUVY6BCHLCTH8HI4JFB6J"
+ "gwiprsazqrt0qmh0bngzvxpi9mlw9i58jo6pdwtmpxcedoodadil3qfjay3ttehxbmleqj/9fj0h"
+ "W0kip5qdrcsqbgwcbifpda+sqdg+h2iclqqqcrwvhb9kqmpnrpzp49ggzsimw0rntgiaemvm/auz"
+ "0hgad2rzeojyycq7gdvfvdzo/32l3/jiudnj/bzp38ddn1uevvjh0t2ob+q9fipvxd3eoroendbg"
+ "Exrkvyiufimtlq8+pzucqgm3c45bkxti9rvcjtlqkyegnkfhor7d/pg5mk/w/fzuf9sczbeyd0d8"
+ "Xdudak+4dgdnfvcoylriutkhvwud256qig8ocfvxnqc6clyjoktccchxbqbsvt4opkxccchzxruc"
+ "actvh9nwf3dvawl69xyg3ma7eanvs0+irft3/072b4fuucogkaa5i6aqc0daiszeqcgwu128n68f"
+ "SIUH7SSND2XY09OVEBAUD4K2FR6FK2P7+HA/QJ4UGM472DO9PJ6UDGRFHSIT0PKTFCZFJM+0SS2H"
+ "R4SLUSOFTYRJ8B08CIOYCGUQYICUQSYNMGHLEMQGXCWQYQCATXDKOOZKENYLM1NBSIYOGIDSTAAC"
+ "LZQWQLCSQIAGGGLISOZYAMM+NF/1VD9RB/EDILF2AZAGFYB9L8RNIJ9FHY8JZ6+QSGFTXMDOBODM"
+ "Udjzbofmd5lz5sgs+vtnjfla/tq/6otp30rzq7kvfxlchvw5//tffo5/gg1w/anikjr+aba7+nuc"
+ "L6CNZBCMU+S+1ZVZFE1UFWMT8EEL48FBBABG4E2D8HGPWHHAGOIBYKFTL6BKPXRU3ACF5C9X6JD9"
+ "Agtx53mrqrcocke4qvbywu6ep3bqqidh2noxs2fytacyqmuvxguvhgdbhpyw2qloapgomgdqrtge"
+ "kgklu0dljrdmqiszvijoqjhqmr24eulfhiqpqwkkhwlcg1wolykslfbglyohq3blcffglznr7o2m"
+ "b6s6aolcswk7z37cja33k8yi+99miqu9maerztmqlwerjmmb+0lmtx4hjrbfcoyo/p7jcwgwx26f"
+ "Nje5+xnvrs+tdw3keswywazlmke/k2vm94a7moecdepvontxcjedswqsllake/dnj5ijvzwrt7ha"
+ "kvlantvvjxnve6mfcxtzhqsucyylfdmgc2ywpypnqjph5ynpm9rsoqvgiqmkeegg7hxcovourf7g"
+ "5b9rkt60i0yn7aeykd2wzrmjbsgbwsobmocmysbts3c2btyel3sq4wuycav4bghaldjzgwg35rgs"
+ "Eslmjdla/qq4ttxedsmg8a9xccorlhxadmvls0oqin+ic/8qf+7fadq/2+yr8pupcbgedtuvxijn"
+ "Kvbiifmx+r752stymi2zgmxzj0huknu6zbk7yp4s21bbvmjti+wzt85eiez7radq7ukvq2drsyit"
+ "yyxvbrw52sxrmdvt1uzwou0wg7xtwdvulxc4s1rbktzq2r0narfponx4xdhsznpgngh0dp2n777l"
+ "Vfu/v/ro8yuqwq0dxlukavb64vkm+eftbmsbiqco/fclxgbz+ou6kopizvyl7yjvxom/w+f0nnbo"
+ "ZAXA8XIGKHOP2AXF8RQLEYBRGCSELSSQ1WB7HT0J7ZN4/GORAANCCTJBJ6W8PYUOQVBTDBKMAQSV"
+ "TJQQHACQUVLZAZRUQZWTLFQHMIYUVG2TVHGYGDVU2E4ME0GPUCDTSKHIFTIZQMLJZGDHK8NEJ5PB"
+ "y8ktyao/bnffnzjsdu8mh/gjciez7zz5cqczdddjzg8yiyetzsqy2gnxmpklxcfb+gfcg9zpqi3w";
System.err.println ("byte compression/decompression test");
System.err.println ("input string: \ t" + inStr);
byte[] input = Instr.getbytes ();
SYSTEM.ERR.PRINTLN ("input byte length: \ t" + input.length);

byte[] data = compress (input);
System.err.println ("Byte length after compression: \ t" + data.length);

byte[] Output = decompress (data);
SYSTEM.ERR.PRINTLN ("uncompressed byte length: \ t" + output.length);
String outputstr = new string (output);
System.err.println ("output string: \ t" + outputstr);


}


/**
* Compression
*
* @param data
* Data to be compressed
* @return byte[] Compressed data
*/
public static byte[] Compress (byte[] data) {
byte[] output = new byte[0];

Deflater Compresser = new Deflater ();

Compresser.reset ();
Compresser.setinput (data);
Compresser.finish ();
Bytearrayoutputstream BOS = new Bytearrayoutputstream (data.length);
try {
byte[] buf = new byte[1024];
while (!compresser.finished ()) {
int i = compresser.deflate (BUF);
Bos.write (buf, 0, I);
}
Output = Bos.tobytearray ();
} catch (Exception e) {
output = data;
E.printstacktrace ();
} finally {
try {
Bos.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
Compresser.end ();
return output;
}

/**
* Compression
*
* @param data
* Data to be compressed
*
* @param os
* Output stream
*/
public static void Compress (byte[] data, OutputStream OS) {
Deflateroutputstream dos = new Deflateroutputstream (OS);

try {
Dos.write (data, 0, data.length);

Dos.finish ();

Dos.flush ();
} catch (IOException e) {
E.printstacktrace ();
}
}

/**
* Unzip
*
* @param data
* Data to be compressed
* @return byte[] decompressed data
*/
public static byte[] Decompress (byte[] data) {
byte[] output = new byte[0];

Inflater decompresser = new Inflater ();
Decompresser.reset ();
Decompresser.setinput (data);

Bytearrayoutputstream o = new Bytearrayoutputstream (data.length);
try {
byte[] buf = new byte[1024];
while (!decompresser.finished ()) {
int i = decompresser.inflate (BUF);
O.write (buf, 0, I);
}
Output = O.tobytearray ();
} catch (Exception e) {
output = data;
E.printstacktrace ();
} finally {
try {
O.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}

Decompresser.end ();
return output;
}

/**
* Unzip
*
* @param is
* Input Stream
* @return byte[] decompressed data
*/
public static byte[] Decompress (InputStream is) {
Inflaterinputstream IIS = new Inflaterinputstream (IS);
Bytearrayoutputstream o = new Bytearrayoutputstream (1024);
try {
int i = 1024;
byte[] buf = new Byte[i];

while ((i = iis.read (buf, 0, i)) > 0) {
O.write (buf, 0, I);
}

} catch (IOException e) {
E.printstacktrace ();
}
return O.tobytearray ();
}
}

Test results:

Java Compression Technology

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.