1. 程式人生 > >Java Zip壓縮 解壓縮

Java 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.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class ZipUtil {
	/*
	 * 壓縮檔案或資料夾中的所有檔案
	 * @param filepath 檔案所在目錄
	 * @param zippath 壓縮後zip路徑
	 * @param zipname 壓縮後zip檔名
	 * @param dirFlag zip檔案中第一層是否包含一級目錄
	*/
	public static void zip2File(String filepath, String zippath, String zipname, boolean dirFlag) throws Exception {
		File file = new File(filepath);
        File zipPath = new File(zippath);
        if (!zipPath.exists())  
        {
        	zipPath.mkdirs();
        }
        ZipOutputStream zipOut = null;
        try {
            File zipFile = new File(zippath + File.separator + zipname);
            zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
	        if(file.isDirectory()){
	        	File[] files = file.listFiles();
	            for(File fileSec:files){
	            	if(dirFlag){
	            		_recursionZip(zipOut, fileSec, file.getName() + File.separator);
	                }else{
	                	_recursionZip(zipOut, fileSec, "");
	                }
	            }
	        }else{
	        	_recursionZip(zipOut, file, "");
	        }
	    } catch (Exception e) {
	    	e.printStackTrace();
	    }finally{
	    	if(zipOut != null){
                zipOut.close();
            }
	    }
	}
    //web用這個
	public static void zip2Stream(String filepath, OutputStream out, boolean dirFlag) throws Exception{
		File file = new File(filepath);
        if (!file.exists())  
        {
            return;
        }  
        ZipOutputStream zipOut = null;
        try {
            zipOut = new ZipOutputStream(out);
	        if(file.isDirectory()){
	        	File[] files = file.listFiles();
	            for(File fileSec:files){
	            	if(dirFlag){
	            		_recursionZip(zipOut, fileSec, file.getName() + File.separator);
	                }else{
	                	_recursionZip(zipOut, fileSec, "");
	                }
	            }
	        }else{
	        	_recursionZip(zipOut, file, "");
	        }
	    } catch (Exception e) {
	    	e.printStackTrace();
	    }finally{
            if(zipOut != null){
            	zipOut.close();
            }
        }
	}
	/*
	 * 迭代生成壓縮檔案
	 * @param zipOut zip輸出流
	 * @param file 檔案/目錄
	 * @param baseDir 上級目錄
	*/
	private static void _recursionZip(ZipOutputStream zipOut, File file, String baseDir) throws Exception{
		if(file.isDirectory()){
			File[] files = file.listFiles();
			for(File fileSec:files){
				_recursionZip(zipOut, fileSec, baseDir + file.getName() + File.separator);
	        }
	    }else{
	        byte[] buf = new byte[1024];
	        InputStream input = new FileInputStream(file);
	        zipOut.putNextEntry(new ZipEntry(baseDir + file.getName()));
	        int len;
	        while((len = input.read(buf)) != -1){
	        	zipOut.write(buf, 0, len);
	        }
	        zipOut.closeEntry();
	        input.close();
	    }
	}
	
	/*
	 * 解壓一個zip檔案(檔案)
	 * @param zippath zip檔案目錄名(包括目錄和名稱)
	 * @param filepath 解壓路徑
	*/
	public static void unzipFromFile(String zippath, String filepath) throws Exception{
		File zip = new File(zippath);
        File filePath = new File(filepath);
        if (!filePath.exists())  
        {
        	filePath.mkdirs();
        }
        ZipInputStream zipInput = null;
        try {
        	zipInput = new ZipInputStream(new FileInputStream(zip));
    		_loopUnzip(zipInput, filepath);
	    } catch (Exception e) {
	    	e.printStackTrace();
	    }finally{
            if(zipInput != null){
                zipInput.close();
            }
        }
	}
	/*
	 * 迴圈解壓縮Zip檔案
	 * 這裡為什麼要加上loop,只是為了跟壓縮函式中的recursion保持一致而已
	 * @param zipInput zip輸入流
	 * @param filepath 解壓路徑
	*/
	private static void _loopUnzip(ZipInputStream zipInput, String filepath) throws Exception{
		ZipEntry entry = null;
		while((entry = zipInput.getNextEntry()) != null){
			//不會解壓空資料夾
        	if(entry.isDirectory()){
            	continue;
            }
            String entryFilePath = filepath + File.separator + entry.getName();
            File entryFile = new File(entryFilePath);
            if(!entryFile.getParentFile().exists()){
            	entryFile.getParentFile().mkdirs();
            }
            OutputStream out = new FileOutputStream(entryFile);
			int temp = 0 ;
			while((temp=zipInput.read()) != -1){
				out.write(temp);
			}
			out.close();
			zipInput.closeEntry();
		}
	}
}

參考文獻: