Implementation of the Java decompression Technology GZIP ZIP BZIP2 Series implementation
There's nothing to say, it's all file operations, direct code placement
Package Com.ljh.zip;import Java.io.bufferedinputstream;import Java.io.bufferedoutputstream;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.util.zip.CRC32; Import Java.util.zip.checkedinputstream;import Java.util.zip.checkedoutputstream;import java.util.zip.ZipEntry; Import Java.util.zip.zipinputstream;import java.util.zip.zipoutputstream;/** * @Desc: Zip compression tool (recommended for file compression, support for folder decompression) * Zip available for socket socket stream data compression (Implementation similar) * @author Ljh * @date 2015-4-14 a.m. 9:39:14 */public class Ziputils {private static final int B Uffer = 1024;private static final String EXT = ". zip";p rivate static final String base_dir = "";p rivate static final Strin G PATH = "/"; The symbol "/" is used as the identifier for the directory inside the zip compressed file [because the Zipentry.isdirectory () method internally is a '/' as the directory identifier of]/** * @Description: Zip data compression * @author (LJH) @ Date 2015-4-13 pm 6:00:52 * @param data * @return * @throws IOException * @return byte[] */publiC Static byte[] Compressdata (byte[] data) throws IOException {Bytearrayoutputstream BAOs = new Bytearrayoutputstream (); Zipoutputstream Zipos = new Zipoutputstream (BAOs); ZipEntry entry = new ZipEntry ("Zip"), entry.setsize (data.length); Zipos.putnextentry (entry); Zipos.write (data); Zipos.closeentry (); Zipos.close (); Baos.close (); return Baos.tobytearray ();} /** * @Description: ZIP Data uncompressed * @author (LJH) @date 2015-4-13 pm 6:00:42 * @param bytes * @return * @throws IOException * @ return byte[] */public static byte[] Uncompressdata (byte[] data) throws IOException {Bytearrayinputstream Bais = new Bytea Rrayinputstream (data); Zipinputstream Zipis = new Zipinputstream (Bais); byte[] bytes = Null;while (Zipis.getnextentry ()! = null) {byte[] buf = new Byte[buffer];int count; Bytearrayoutputstream BAOs = new Bytearrayoutputstream (BUFFER), while ((Count = Zipis.read (buf, 0, buf.length))! =-1) {Bao S.write (buf, 0, count);} bytes = Baos.tobytearray (); Baos.flush (); Baos.close ();} Zipis.close (); Bais.close (); return bytes;} /** * @Description: File compression * @author (LJH) @date 2015-4-14 Morning 9:32:52 * @param path * source file path * @throws IOException * @return void */public static void compress (String srcpath) throws IOException {compress (new File (Srcpath));} public static void Compress (File srcfile) throws IOException {compress (Srcfile, Srcfile.getpath () + EXT);} public static void Compress (string srcpath, String destpath) throws IOException {Compress (new file (Srcpath), new file (dest Path));} public static void Compress (File srcfile, String destpath) throws Ioexception{compress (Srcfile, New File (DestPath));} /** * @Description: Start file compression * @author (LJH) @date 2015-4-14 Morning 10:54:06 * @param srcfile * @param destfile * @throws ioexcep tion * @return void */public static void compress (file srcfile, file destfile) throws IOException {checkedoutputstream cos = new Checkedoutputstream (new FileOutputStream (DestFile), New CRC32 ());//CRC32 checksum for output file Zipoutputstream Zipos = new Zipou Tputstream (COS); Compress (Srcfile, Zipos, base_dIR); Zipos.flush (); Zipos.close (); Cos.close ();} /** * @Description: ZIP File/directory * @author (LJH) @date 2015-4-14 pm 12:41:33 * @param srcfile Source File/directory * @param zipos output stream * @pa Ram BasePath * @throws IOException * @return void */public static void compress (File srcfile, Zipoutputstream Zipos, STR ing BasePath) throws IOException {if (Srcfile.isdirectory ()) {Compressdir (Srcfile, Zipos, basepath);} else {Compressfile (Srcfile, Zipos, BasePath);}} /** * @Description: Zip file compression * @author (LJH) @date 2015-4-14 a.m. 10:36:56 * @param file * @param zipos * @throws IOException * @return void */private static void compressfile (file file, Zipoutputstream Zipos, String basepath) throws IOException { /** * Compressed package file name definition, if there is a multilevel directory, then you need to give the file name containing the directory, if you open the package with WinRAR, Chinese name will be displayed as garbled */zipentry entry = new ZipEntry (BasePath + file.getn Ame ()); Zipos.putnextentry (entry); Bufferedinputstream bis = new Bufferedinputstream (new FileInputStream (file)), int count;byte[] buf = new Byte[buffer]; while ((count = Bis.read (buf, 0, buf.length))!=-1) {zipos.write (buf, 0, count);} Bis.close (); Zipos.closeentry ();} /** * @Description: Directory compression * @author (LJH) @date 2015-4-14 pm 12:40:49 * @param dirfile * @param zipos * @param basepath * @ Throws IOException * @return void */private static void Compressdir (File dirfile, Zipoutputstream zipos, String basepath) Throws IOException {file[] files = dirfile.listfiles ();//Build Empty directory if (Files.length < 1) {//empty directory ZipEntry entry = new Zipen Try (BasePath + dirfile.getname () + PATH); Zipos.putnextentry (entry); Zipos.closeentry ();} for (file file:files) {compress (file, Zipos, BasePath + dirfile.getname () + PATH);//recursive compression}}//-------------------------- -------------------------------------/** * @Description: Zip file uncompressed * @author (LJH) @date 2015-4-14 pm 12:46:25 * @param src Path * @throws IOException * @return void */public static void uncompress (String srcpath) throws IOException {uncompress ( New File (Srcpath));} public static void Uncompress (File srcfile) throws IOException {uncompress (srcfile, srcfile.geTparent ());} public static void Uncompress (String srcpath, String destpath) throws IOException {uncompress (new File (Srcpath), DestPath );} public static void Uncompress (File srcfile, String destpath) throws IOException {uncompress (srcfile, New File (DestPath)); }/** * @Description: Start zip file extract * @author (ljh) @date 2015-4-14 pm 12:47:00 * @param srcfile * @param destfile * @throws I Oexception * @return void */public static void uncompress (file srcfile, file destfile) throws IOException {CHECKEDINPUTST Ream cis = new Checkedinputstream (new FileInputStream (Srcfile), New CRC32 ()); Zipinputstream Zipis = new Zipinputstream (CIS); Uncompress (Zipis, destfile); Zipis.close (); Cis.close ();} private static void Uncompress (Zipinputstream zipis, File destfile) throws IOException {ZipEntry entry = Null;while ((entr y = zipis.getnextentry ()) = null) {String dir = destfile.getpath () + File.separator + entry.getname (); File Dirfile = new file (dir); Fileprober (dirfile);//Recursive creation Dirfile parent directory if (Entry.isdirectory ()) {DIRFIle.mkdirs ();} else {uncompressfile (Zipis, dirfile);} Zipis.closeentry ();}} /** * @Description: Create directory When parent directory is not present * @author (LJH) @date 2015-4-14 pm 12:54:25 * @param dirfile * @return void */private s tatic void Fileprober (file dirfile) {File Parentfile = Dirfile.getparentfile (); if (!parentfile.exists ()) {Fileprober ( Parentfile);//recursively find the parent directory and recursively create Parentfile.mkdir ();}} /** * File uncompressed * @throws ioexception */private static void Uncompressfile (Zipinputstream zipis, File destfile) throws Ioexcep tion {bufferedoutputstream bos = new Bufferedoutputstream (new FileOutputStream (destfile)); int count;byte buf[] = new byte [Buffer];while ((count = Zipis.read (buf, 0, buf.length))! =-1) {bos.write (buf, 0, count);} Bos.close ();} // --------------------------------------------}
Test method:
/** * @Description: Compressed data is more obvious [test found that the Chinese byte byte is greater than 200 when the obvious compression, English 150 is obvious] * @author (LJH) @date 2015-4-14 Morning 11:33:13 * @thro WS IOException * @return void *///@org. junit.testprivate static void Testzip () throws IOException {byte[] bytes = "Ah, a simple generalization. Cheerleading as the price of the space off to see the g9w9eijgslkj is a Kentucky Fried chicken is the feeling is KFC preoccupied preoccupied preoccupied with the petty and petty preoccupied with the very close to the dead of the tight-and-go-la-la-la-Cheerleading. GetBytes ();/ Bytes Compression//verification of the effect of compression after the comparison System.out.println ("Pre-compression:"); System.out.println (bytes.length); for (byte b:bytes) {System.out.print (b + "");} System.out.println (); SYSTEM.OUT.PRINTLN ("compressed:"); byte[] bytes2 = ziputils.compressdata (bytes); System.out.println (bytes2.length); for (byte b:bytes2) {System.out.print (b + "");} System.out.println (); System.out.println ("After decompression:"); byte[] Byte22 = Ziputils.uncompressdata (bytes2); System.out.println (byte22.length); for (byte b:byte22) {System.out.print (b + "");} System.out.println ();//Ziputils.compress ("F:\\test\\test1");//ziputils.uncompress ("F:\\test\\test1.zip");// To compress folders, refer to Ziputils self-implementation}
Click I download the relevant source code
Java Decompression Technology (a) ZIP compression-Decompression