Java Decompression Technology (II) GZIP compression-Unzip (

Source: Internet
Author: User
Tags uncompress


Implementation of the Java decompression technology GZIP ZIP BZIP2


There's nothing to say. File IO operations


Package Com.ljh.gzip;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 Java.util.zip.gzipinputstream;import java.util.zip.gzipoutputstream;/** * @Desc: GZIP compression tool (data compression recommended, support single file compression) * To compress the folder please refer to Ziputils class * Tips:gzip can also be implemented using Apache open source Commons-compress.jar * @author ljh * @date 2015-4-14 morning 9:39:14 */public class Gziputils {privat e static final int BUFFER = 1024;public static final String EXT = ". Gz";/** * @Description: GZIP data compression * @author (LJH) @dat E 2015-4-13 pm 6:00:52 * @param data * @return * @throws IOException * @return byte[] */public static byte[] Compress (byte[ ] data) throws IOException {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 */public static void compress (InputStream is, OutputStream os) throws IOException {Gzipoutputstream Gzipos = NE W gzipoutputstream (OS, 1024x768), int count;byte data[] = new Byte[buffer];while ((count = is.read (data, 0, data.length))! =-1 {gzipos.write (data, 0, count);} Gzipos.finish (); Gzipos.flush (); Gzipos.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 ByteArray InputStream (data); Bytearrayoutputstream BAOs = new Bytearrayoutputstream (BUFFER); Uncompress (Bais, BAOs); Baos.flush (); Baos.close (); 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 {Gzipinputstream Gzipis = new GZIPINP Utstream (IS); int count;byte data[] = new Byte[buffer];while ((count = gzipis.read (data, 0, data.length))! =-1) {Os.write ( Data, 0, count);} Gzipis.close ();} /** * @Description: File compression * @author (LJH) @date 2015-4-14 a.m. 9:28:46 * @param file * @throws Exception * @return void */publ IC static void Compress (file file) throws IOException {compress (file, true);} /** * @Description: File compression * @author (LJH) @date 2015-4-14 a.m. 9:29:21 * @param file * @param Delete * Delete source file * @t Hrows Exception * @return void * @throws ioexception */public static void compress (file file, Boolean delete) throws Ioexc eption {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 a.m. 9:32:52 * @param Path * source file path * @throws Exception * @return void */public static void compress (S Tring path) throws IOException {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 */public static void compress (String path, Boolean Delete) throws IOException {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 extract * @author (LJH) @date 2015-4-14 Morning 9:38:03 * @param path * @param Delete * Delete source file * @  Throws Exception * @return void */public static void uncompress (String path, Boolean delete) throws IOException {file file = new file (path); uncompress (File, delete);}}


Test code:


/** * @Description: Chinese 150 byte compression is obvious, English 50 byte compression is obvious * @author (LJH) @date 2015-4-14 Morning 11:37:37 * @throws IOException * @retur n void *///@org. junit.testpublic void Testgzip () throws IOException {byte[] bytes = " Alex Everyone Karma card crash of a AH the card of the death of the crash of the feeling open on the AO trench Assad gas The 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 = gziputils.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 = gziputils.uncompress (bytes2);        System.out.println (byte22.length);        for (byte b:byte22) {System.out.print (b + "");} Gzip Tool//Gziputils.compress ("F:\\activity_win9.bmp", false);//Compressed file is a 12M BMP file, compressed after the size of 1.2m//Gziputils.unc Ompress ("f:\\activity_win9.bmp.gz", false);//Unzip the same as the source file}


Click I download the relevant source code



Java Decompression Technology (II) GZIP compression-Unzip (

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.