First, brief
Decompression technology and compression technology just the opposite, the decompression technology to use the class: by the Zipinputstream through the Read method to extract data, and the need to set up redundancy check code through Checkedinputstream, such as:
New Checkedinputstream ( new fileinputstream(new CRC32 ()); New Zipinputstream (CIS);
It is important to note that when building an extract file, you need to consider the automatic creation of the directory, where the parent directory is created recursively by layer, as follows:
Create a directory when the parent directory does not exist!
Privatestaticvoid fileprober (File dirfile) { = dirfile.getparentfile (); if (! parentfile.exists ()) { // recursive search for parent directory fileprober (parentfile); Parentfile.mkdir (); } }
When compressing, we add a file as a compressed add-on (zipentry) to the compressed package, extracting a single compressed item from the compressed package, as follows:
Private Static voidDecompress (File destfile, Zipinputstream zis) throws Exception {ZipEntry entry=NULL; while((Entry = Zis.getnextentry ())! =NULL) { //fileString dir = destfile.getpath () + File.separator +Entry.getname (); File Dirfile=NewFile (dir); //file CheckFileprober (Dirfile); if(Entry.isdirectory ()) {dirfile.mkdirs (); } Else{decompressfile (dirfile, ZiS); } zis.closeentry (); } }
The most core of the decompression implementation, in fact, is very similar to the compression implementation, the code is as follows:
/** * File Uncompressed * * @param destfile * target file * @param zis * zipinputstream * @throws Exception */ Private Static voidDecompressfile (File destfile, Zipinputstream zis) throws Exception {Bufferedoutputstream Bos=NewBufferedoutputstream (NewFileOutputStream (destfile)); intcount; byteData[] =New byte[BUFFER]; while(count = zis.read (data,0, BUFFER))! =-1) {bos.write (data,0, Count); } bos.close (); }
A complete example:
Package Com.joyplus.test;import Java.io.BufferedOutputStream; Import Java.io.File; Import Java.io.FileInputStream; Import Java.io.FileOutputStream; Import java.util.zip.CRC32; Import Java.util.zip.CheckedInputStream; Import Java.util.zip.ZipEntry; Import Java.util.zip.ZipInputStream; /** * * @author * */public class ZipFiletest2 {public static final String EXT = ". zip"; private static final String Base_dir = ""; private static final String PATH = File.separator; private static final int BUFFER = 1024; /** * File Uncompressed * * @param srcpath * source file path * * @throws Exception */Publ IC static void Decompress (String Srcpath) throws Exception {file Srcfile = new File (Srcpath); Decompress (srcfile); }/** * Unzip * * @param srcfile * @throws Exception * * public static void decompress ( File srcfile) throws Exception {String BasePath = srcfile.getparent (); Decompress (Srcfile, basepath); */** * Unzip * * @param srcfile * @param destfile * @throws Exception */Public static void decompress (file srcfile, file destfile) throws Exception {checkedinputstream cis = new Checkedinpu TStream (New FileInputStream (Srcfile), New CRC32 ()); Zipinputstream ZiS = new Zipinputstream (CIS); Decompress (DestFile, ZiS); Zis.close (); */** * Unzip * * @param srcfile * @param destpath * @throws Exception */Public static void Decompress (File srcfile, String destpath) throws Exception {Decompress (srcfile, new Fil E (destpath)); /** * File Extract * * @param srcpath * source file path * @param destpath * Destination file path * @throws Exception */public static void decompress (string srcpath, string destpath) t HroWS Exception {file Srcfile = new File (Srcpath); Decompress (Srcfile, destpath); /** * File Extract * * @param destfile * target file * @param zis * Zipin Putstream * @throws Exception */private static void decompress (File destfile, Zipinputstream zis) Throws Exception {ZipEntry entry = null; while ((entry = Zis.getnextentry ()) = null) {//file String dir = destfile.getpath () + file.se Parator + entry.getname (); File Dirfile = new file (dir); Document Inspection Fileprober (dirfile); if (Entry.isdirectory ()) {dirfile.mkdirs (); } else {decompressfile (dirfile, ZiS); } zis.closeentry (); }}/** * file Probe * * * * When the parent directory does not exist, create a directory! * * * @param dirfile */Private Static void Fileprober (file dirfile) {File Parentfile = Dirfile.getparentfile (); if (!parentfile.exists ()) {//recursive search for parent directory Fileprober (Parentfile); Parentfile.mkdir (); }}/** * File uncompressed * * @param destfile * target file * @param zis * Zipinputstream * @throws Exception */private static void Decompressfile (File destfile, Zipinputstream Z IS) throws Exception {bufferedoutputstream bos = new Bufferedoutputstream (New F Ileoutputstream (DestFile)); int count; byte data[] = new Byte[buffer]; while ((count = zis.read (data, 0, BUFFER))! =-1) {bos.write (data, 0, count); } bos.close (); } public static void Main (string[] args) {//extract to the specified directory try {zipfiletest2.decompress ("D:\\sumzip\\co.zip", "D: \\log ");} catch (Exception e) {//TODO Auto-generated catch Blocke.printstacktrace ();} }}
Reference article: http://snowolf.iteye.com/blog/642492
Java.util.zip Compressed Packaging File Summary Two: Zip decompression technology