package com.cwqi.demo;/** * @author cwqi *2015-8-26 Morning 9:59:12 */import java.io.bufferedinputstream;import java.io.bufferedoutputstream;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.enumeration;import org.apache.tools.ant.project;import org.apache.tools.ant.taskdefs.zip;import org.apache.tools.ant.types.fileset;import org.apache.tools.zip.zipentry;import Org.apache.tools.zip.zipfile;import org.apache.tools.zip.zipoutputstream;public class zipdemo {/** * decompression * @param zipfilepath: Compressed file path */public static void Unzip (String zipfilepath) {unzipfile (zipfilepath, null);} Public static file buildfile (string filename, boolean isdirectory) {File target = new file (FileName);if (isdirectory) {target.mkdirs (); else {if (!target.getparentfile (). exists ()) {target.getparentfile (). Mkdirs ();} Return target;} /** * Unzip * @param zipfilepath zip file path * @param targetPath Unzip to the location, if null or an empty string then the default decompression to the same directory as the ZIP package with the same name as the ZIP package under the folder */public static void unzipfile ( String zipfilepath, string targetpath) {outputstream os = null;inputstream is = null; Zipfile zipfile = null;try {zipfile = new zipfile (ZipFilePath); string directorypath = "";if (null == targetpath | | ". Equals (TargetPath)) {directorypath = zipfilepath.substring (0,zipfilepath.lastindexof (" ."));} else {directorypath = targetpath;} @SuppressWarnings ("unchecked") Enumeration<zipentry> entryenum = zipfile.getentriES ();if (null != entryenum) {ZipEntry zipEntry = null;while ( Entryenum.hasmoreelements ()) {zipentry = entryenum.nextelement ();if (ZipEntry.isDirectory ( )) {new file (Directorypath + file.separator+ zipentry.getname ()). Mkdirs (); continue;} if (Zipentry.getsize () > 0) {// file file targetfile = Zipdemo.buildfile (Directorypath+ file.separator + zipentry.getname (), false); os = new bufferedoutputstream (New fileoutputstream (targetfile));is = Zipfile.getinputstream (zipentry); byte[] buffer = new byte[4096];int readlen = 0;while ((Readlen = is.read (buffer, 0, 4096)) >= 0) {os.write (Buffer, 0, readlen);} Os.flush (); Os.close ();} else {// Empty Directory Zipdemo.buildfile (Directorypath + file.separator+ zipentry.getname ( ),  , true);}}} catch (Ioexception ex) {ex.printstacktrace ();} finally {if (Null != zipfile) {zipfile = null;} if (Null != is) {try {is.close ();} catch (ioexception e) {e.printstacktrace ();}} if (Null != os) {try {os.close ();} catch (ioexception e) {e.printstacktrace ();}}} static final int buffer = 2048;/** * compression * @param sourcefile The file or file directory to compress * @param targetFile the path and file name for the zip file to be generated, such as "D:\\downloads.zip" */ Public static void zipfiles (String sourcedir, string targetfile) {File sourcefile = new file (SourceDir); File zfile = new file (targetfile); zipoutputstream zos = null;try {outputstream os;os = new FileOutputStream (Zfile); bufferedoutputstream bos = new bufferedoutputstream (OS); Zos = new zipoutputstream (BOS); string basepath = null;if (Sourcefile.isdirectory ()) {basePath = Sourcefile.getpath ();} else {basepath = sourcefile.getparent ();} ZipFile (Sourcefile, basepath, zos);} catch (ioexception e) {e.printstacktrace ();} finally {if (Zos != null) {try {zos.close ();} catch (ioexception e) {e.printstacktrace ();}}} Private static void zipfile (File sourcefile, string basepath,zipoutputstream zos) throws IOException {File[] files = null;if ( Sourcefile.isdirectory ()) {files = sourcefile.listfiles ();} else {files = new file[] { sourcefile };} inputstream is = null; bufferedinputstream bis = null; string pathname;byte[] buf = new byte[buffer];int length = 0;try {for (File file : files) {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); Bis = new bufferedinputstream (is); Zos.putnextentry (new zipentry (PathName));while ((Length = bis.read (BUF)) != -1) {zos.write (buf, 0, length);}}} finally {if (Bis != null) {bis.close ();} if (Is != null) {is.close ();}} /** * using ant compression * @param srcPathName * @param destPathName */ Public static void zipbyant (string srcpathname,string destPathName) {file srcdir = new file (srcpathname);if (!srcdir.exists ()) {throw New runtimeexception (srcpathname + "no exist!");} Project project = new project (); Zip zip = new zip (); Zip.setproject (project); File zipfile = new file (Destpathname); Zip.setdestfile (ZipFile); Fileset fileset = new fileset (); Fileset.setproject (project); Fileset.setdir (srcdir);// fileset.setincludes ("**/*.java");// fileset.setexcludes (*.txt); zip.addfileset (FileSet); Zip.execute ();} /** * Test * @param args */public static void main (string[] args) {unzipfile ("D:\\downloads.zip", "d:\\downloads");//zipfiles ("D:\\downloads", "D:\\ Downloads.zip ");//zipbyant (" D:\\downloads ", " D:\\downloads.zip ");}}
Java decompression file Simple instance