package zip; 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.util.Enumeration; import java.util.zip.Deflater; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipFile; import org.apache.tools.zip.ZipOutputStream; public class Zip { public static void main(String[] args){ // new Zip().doZip("d:/報表程序","e:/報表程序.rar"); new Zip().unZip("e:/報表程序.rar","d:/報表程序"); } private ZipFile zipFile; private ZipOutputStream zipOut; //壓縮Zip private int bufSize; //size of bytes private byte[] buf; public Zip(){ //要構造函數中去初始化我們的緩沖區 this.bufSize = 1024*4; this.buf = new byte[this.bufSize]; } /** * 對傳入的目錄或者是文件進行壓縮 * @param srcFile 需要 壓縮的目錄或者文件 * @param destFile 壓縮文件的路徑 */ public void doZip(String srcFile, String destFile) {// zipDirectoryPath:需要壓縮的文件夾名 File zipFile = new File(srcFile); try { //生成ZipOutputStream,會把壓縮的內容全都通過這個輸出流輸出,最后寫到壓縮文件中去 this.zipOut = new ZipOutputStream(new BufferedOutputStream( new FileOutputStream(destFile))); //設置壓縮的注釋 zipOut.setComment("comment"); //設置壓縮的編碼,如果要壓縮的路徑中有中文,就用下面的編碼 zipOut.setEncoding("GBK"); //啟用壓縮 zipOut.setMethod(ZipOutputStream.DEFLATED); //壓縮級別為最強壓縮,但時間要花得多一點 zipOut.setLevel(Deflater.BEST_COMPRESSION); handleFile(zipFile, this.zipOut,""); //處理完成后關閉我們的輸出流 this.zipOut.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } /** * 由doZip調用,遞歸完成目錄文件讀取 * @param zipFile * @param zipOut * @param dirName 這個主要是用來記錄壓縮文件的一個目錄層次結構的 * @throws IOException */ private void handleFile(File zipFile, ZipOutputStream zipOut,String dirName) throws IOException { System.out.println("遍歷文件:"+zipFile.getName()); //如果是一個目錄,則遍歷 if(zipFile.isDirectory()){ File[] files = zipFile.listFiles(); if (files.length == 0) {// 如果目錄為空,則單獨創建之. //只是放入了空目錄的名字 this.zipOut.putNextEntry(new ZipEntry(dirName+zipFile.getName()+File.separator)); this.zipOut.closeEntry(); } else {// 如果目錄不為空,則進入遞歸,處理下一級文件 for (File file : files) { // 進入遞歸,處理下一級的文件 handleFile(file, zipOut, dirName+zipFile.getName()+File.separator); } } }else{//如果是文件,則直接壓縮 FileInputStream fileIn = new FileInputStream(zipFile); //放入一個ZipEntry this.zipOut.putNextEntry(new ZipEntry(dirName+zipFile.getName())); int length = 0; //放入壓縮文件的流 while ((length = fileIn.read(this.buf)) > 0) { this.zipOut.write(this.buf, 0, length); } //關閉ZipEntry,完成一個文件的壓縮 fileIn.close(); this.zipOut.closeEntry(); } } /** * 解壓指定zip文件 * @param unZipfile 壓縮文件的路徑 * @param destFile 解壓到的目錄 */ public void unZip(String unZipfile, String destFile) {// unZipfileName需要解壓的zip文件名 FileOutputStream fileOut; File file; InputStream inputStream; try { //生成一個zip的文件 this.zipFile = new ZipFile(unZipfile); //遍歷zipFile中所有的實體,並把他們解壓出來 for (@SuppressWarnings("unchecked") Enumeration<ZipEntry> entries = this.zipFile.getEntries(); entries .hasMoreElements();) { ZipEntry entry = entries.nextElement(); //生成他們解壓后的一個文件 file = new File(destFile+File.separator+entry.getName()); if (entry.isDirectory()) { file.mkdirs(); } else { // 如果指定文件的目錄不存在,則創建之. File parent = file.getParentFile(); if (!parent.exists()) { parent.mkdirs(); } //獲取出該壓縮實體的輸入流 inputStream = zipFile.getInputStream(entry); fileOut = new FileOutputStream(file); int length = 0; //將實體寫到本地文件中去 while ((length = inputStream.read(this.buf)) > 0) { fileOut.write(this.buf, 0, length); } fileOut.close(); inputStream.close(); } } this.zipFile.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } }