1. 程式人生 > >java實現zip的壓縮和解壓

java實現zip的壓縮和解壓

package cn.tzz.zip;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class ZipUtils {

	/**
	 * 檔案壓縮(支援一次壓縮多個檔案,檔案存放至一個資料夾中)
	 * 中文名亂碼,採用Apache的zip包可解決
	 */
    public static void ZipFile(String filepath ,String zippath) {
		try {
	        File file = new File(filepath);// 要被壓縮的資料夾
	        File zipFile = new File(zippath);
	        InputStream input = null;
	        ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
	        if(file.isDirectory()){//壓縮資料夾
	            File[] files = file.listFiles();
	            for(int i = 0; i < files.length; ++i){
	                input = new FileInputStream(files[i]);
	                zipOut.putNextEntry(new ZipEntry(file.getName() + File.separator + files[i].getName()));
	                int temp = 0;
	                while((temp = input.read()) != -1){
	                    zipOut.write(temp);
	                }
	                input.close();
	            }
	            zipOut.close();
	        }else{//壓縮單個檔案
	        	input = new FileInputStream(file);
	        	String fileName = new String(file.getName().getBytes(), "UTF-8");
                zipOut.putNextEntry(new ZipEntry(fileName));
                int temp = 0;
                while((temp = input.read()) != -1){
                    zipOut.write(temp);
                }
                input.close();
                zipOut.close();
	        }
	        zipOut.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
    
    /**  解壓縮(解壓縮單個檔案)*/
    public static void ZipContraFile(String zippath ,String outfilepath ,String filename) {
		try {
	        File file = new File(zippath);//壓縮檔案路徑和檔名
	        File outFile = new File(outfilepath);//解壓後路徑和檔名
	        ZipFile zipFile = new ZipFile(file);
	        ZipEntry entry = zipFile.getEntry(filename);//所解壓的檔名
	        InputStream input = zipFile.getInputStream(entry);
	        OutputStream output = new FileOutputStream(outFile);
	        int temp = 0;
	        while((temp = input.read()) != -1){
	            output.write(temp);
	        }
	        input.close();
	        output.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
    
    /**  解壓縮(壓縮檔案中包含多個檔案)可代替上面的方法使用。
     * ZipInputStream類
     * 當我們需要解壓縮多個檔案的時候,ZipEntry就無法使用了,
     * 如果想操作更加複雜的壓縮檔案,我們就必須使用ZipInputStream類
     * */
    public static void ZipContraMultiFile(String zippath ,String outzippath){
    	try {
            File file = new File(zippath);
            File outFile = null;
            ZipFile zipFile = new ZipFile(file);
            ZipInputStream zipInput = new ZipInputStream(new FileInputStream(file));
            ZipEntry entry = null;
            InputStream input = null;
            OutputStream output = null;
            while((entry = zipInput.getNextEntry()) != null){
                System.out.println("解壓縮" + entry.getName() + "檔案");
                outFile = new File(outzippath + File.separator + entry.getName());
                if(!outFile.getParentFile().exists()){
                    outFile.getParentFile().mkdir();
                }
                if(!outFile.exists()){
                    outFile.createNewFile();
                }
                input = zipFile.getInputStream(entry);
                output = new FileOutputStream(outFile);
                int temp = 0;
                while((temp = input.read()) != -1){
                    output.write(temp);
                }
                input.close();
                output.close();
            }
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    
    public static void main(String[] args) {
		ZipFile("E:/zip/中文名.txt", "E:/zip/壓縮單個檔案.zip");
		ZipFile("E:/zip", "E:/壓縮多個檔案.zip");
		ZipContraFile("E:/zip/壓縮單個檔案.zip","E:/zip/解壓檔案.txt", "1.txt");
		ZipContraMultiFile("E:/壓縮多個檔案.zip","E:/zip2");
		System.out.println("********執行成功**********");
	}
}