標籤:package import public java source
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 {/** * 功能:把 sourceDir 目錄下的所有檔案進行 zip 格式的壓縮,儲存為指定 zip 檔案 * @param sourceDir 如果是目錄,eg:D:\\MyEclipse\\first\\testFile,則壓縮目錄下所有檔案; * 如果是檔案,eg:D:\\MyEclipse\\first\\testFile\\aa.zip,則只壓縮本檔案 * @param zipFile 最後壓縮的檔案路徑和名稱,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 { // 建立寫出流操作 OutputStream os = new FileOutputStream(zipFile); BufferedOutputStream bos = new BufferedOutputStream(os); zos = new ZipOutputStream(bos); String basePath = null; // 擷取目錄 if(file.isDirectory()) { basePath = file.getPath(); }else { basePath = file.getParent(); } zipFile(file, basePath, zos); }finally { if(zos != null) { zos.closeEntry(); zos.close(); } } return zipFile; } /** * @param source 源檔案 * @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) {//迴圈壓縮檔夾下面的所有檔案,知道zos.close的時候才算壓縮完成 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(); } } } /** * 解壓到指定目錄 * @param zipPath 壓縮包目錄 * @param descDir 組建檔案儲存目錄 * */public static void unZipFiles(String zipPath,String descDir)throws IOException{unZipFiles(new File(zipPath), descDir);}/** * 解壓檔案到指定目錄 * @param zipFile * @param descDir * */@SuppressWarnings("rawtypes")public static void unZipFiles(File zipFile2,String descDir)throws IOException{ try { //根據ZIP檔案建立ZipFile對象 ZipFile zipFile = new ZipFile(zipFile2); ZipEntry entry = null; String entryName = null; String targetFileName = null; byte[] buffer = new byte[1024]; int bytes_read; //擷取ZIP檔案裡所有的entry Enumeration entrys = zipFile.entries(); //遍曆所有entry while (entrys.hasMoreElements()) { entry = (ZipEntry)entrys.nextElement(); //獲得entry的名字 entryName = entry.getName(); targetFileName = descDir+File.separator+entryName; if (entry.isDirectory()){ // 如果entry是一個目錄,則建立目錄 new File(targetFileName).mkdirs(); continue; } else { // 如果entry是一個檔案,則建立父目錄 new File(targetFileName).getParentFile().mkdirs(); } //否則建立檔案 File targetFile = new File(targetFileName); System.out.println("建立檔案:" + targetFile.getAbsolutePath()); //開啟檔案輸出資料流 FileOutputStream os = new FileOutputStream(targetFile); //從ZipFile對象中開啟entry的輸入資料流 InputStream is = zipFile.getInputStream(entry); while ((bytes_read = is.read(buffer)) != -1){ os.write(buffer, 0, bytes_read); } //關閉流 os.close( ); is.close( ); } System.out.println("解壓縮檔案成功!"); } catch (IOException err) { System.err.println("解壓縮檔案失敗: " + err); }}}
本文出自 “breezewindlw” 部落格,請務必保留此出處http://breezewindlw.blog.51cto.com/6504579/1629659
Java原始的壓縮和解壓