標籤:
package com.cwqi.demo;/** *@author Cwqi *2015-8-26上午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 {/** * 解壓 * @param zipFilePath:壓縮檔路徑 */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;}/** * 解壓 * @param zipFilePath zip檔案路徑 * @param targetPath 解壓縮到的位置,如果為null或Null 字元串則預設解壓縮到跟zip包同目錄跟zip包同名的檔案夾下 */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 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 {// 空目錄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;/** * 壓縮 * @param sourceFile 為要壓縮的檔案或檔案目錄 * @param targetFile 為要產生zip檔案的路徑和檔案名稱,如:"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();}}}/** * 使用ant壓縮 * @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();}/** * 測試 * @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解壓縮檔案簡單一實例