package com.ahzc.test;import java.io.bufferedinputstream;import java.io.bufferedoutputstream; Import java.io.file;import java.io.fileinputstream;import java.io.filenotfoundexception;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.outputstream;import java.util.enumeration;import java.util.zip.zipentry;import java.util.zip.zipfile;import java.util.zip.zipinputstream;import java.util.zip.zipoutputstream; public class compress {/** * function: zip All files under the sourceDir directory format compression, save as specified zip file * @param sourceDir If it is a directory, eg:d:\\myeclipse\\first\ \testfile, all files under the compressed directory; * if it is a file, eg:d:\\myeclipse\\first\\testfile\ \aa.zip, only compress this file * @param zipFile last compressed file path and name, eg:d:\\myeclipse\\first\\testfile\\ Aa.zip */ public static file dozip (String sourcedir, string zipfilepath) throws Ioexception { file file = new file (SourceDir); File zipfile = new file (Zipfilepath); zipoutputstream zos = null; try { // Create write-out flow operation outputstream os = new fileoutputstream (ZipFile); bufferedoutputstream bos = new Bufferedoutputstream (OS); zos = new zipoutputstream (BOS); String basePath = null; // Get catalog if (File.isdirectory ()) { basepath = file.getpath (); }else { basepath = file.getparent (); } &nbsP;zipfile (File, basepath, zos); }finally { if (zos != null ) { zos.closeentry (); zos.close (); } } return zipFile; } /** * @param source Source files * @param basePath * @param zos */ private static void zipfile (file source, string basepath, Zipoutputstream zos) throws IOException { File[] files = null; if (Source.isdirectory ()) { files = source.listfiles (); } else { files = new file[1]; files[0] = source; } InputStream is = null; String pathname; byte[] buf = new byte[1024]; int length = 0; try{ for (file file : files) {//compress all the files underneath the folder, knowing Zos.close is the time to compress complete if (File.isdirectory ()) { pathname = file.getpath (). substring (basepath.length () + 1) + "/"; zos.putnextentry ( New zipentry (PathName)); zipfile (File, basepath, zos); }else { pathname = file.getpath (). SUBSTRING ( Basepath.length () + 1); is = new fileinputstream (file); bufferedinputstream bis = new bufferedinputstream (IS); zos.putnextentry (New zipentry (PathName)); while (length = bis.reAD (BUF)) > 0) { zos.write (buf, 0, length); } } } }finally { if (Is != null) { is.close (); } } } /** * extract to the specified directory * @param zipPath Zip package directory * @param descDir generate File Save directory * */public static void unzipfiles (String zippath,string descdir) Throws ioexception{unzipfiles (New file (ZipPath), descDir);} /** * extract files to the specified directory * @param zipFile * @param descdir * */ @SuppressWarnings ("Rawtypes") Public static void unzipfiles (file zipfile2,string Descdir) throws ioexception{ try { // Create a ZipFile object based on a zip file zipfile zipfile = new zipfile (ZipFile2); ZipEntry entry = null; String entryName = null; String targetFileName = null; byte[] buffer = new byte[1024]; int bytes_read; //get all the entry in the zip file Enumeration entrys = Zipfile.entries (); //Traverse all entry while (Entrys.hasmoreelements ()) { entry = (ZipEntry) entrys.nextelement (); //gets entry's name entryname = entry.getname (); targetfilename = descdir+file.separator+ entryname; if ( Entry.isdirectory ()) { // if entry is a directory, create a directory new file (TargetFileName). Mkdirs (); continue; } else { // if entry is a file, create a parent directory new file (TargetFileName). GetParentFile ( ). Mkdirs (); } //otherwise create file file targetfile = new file (TargetFileName); system.out.println ("Create File:" + targetfile.getabsolutepath ()); //Open File output stream fileoutputstream os = new fileOutputStream (targetfile); // Open the entry input stream from the ZipFile object Inputstream is = zipfile.getinputstream (Entry); while ((bytes_read = is.read (buffer)) != -1) { os.write (Buffer, 0, bytes_read); } //Close Flow os.close ( ); is.close ( ); } &nbsP; system.out.println ("Unzip the file successfully!") "); } catch (Ioexception err) {           SYSTEM.ERR.PRINTLN ("Uncompressed file failed: " + ERR); }}}
This article is from the "BREEZEWINDLW" blog, make sure to keep this source http://breezewindlw.blog.51cto.com/6504579/1629659
Java RAW compression and decompression