1. 程式人生 > 實用技巧 >解壓MAC壓縮的zip檔案

解壓MAC壓縮的zip檔案

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Enumeration;
import java.util.List;


/** * 解壓zip, * * @param zipFile 裡面可以有多個psd檔案,支援多級目錄 * @param targetPath 儲存目錄 * @throws Exception */ public static void unzipPSD(String zipPath, String targetPath) throws Exception { ZipFile zipFile = new ZipFile(zipPath); Enumeration emu = zipFile.getEntries();
      //Enumeration emu = zipFile.entries(); int i = 0; while (emu.hasMoreElements()) { ZipEntry entry = (ZipEntry) emu.nextElement(); String fileName=entry.getName().toLowerCase(); if(!fileName.startsWith("__macosx/")&&fileName.endsWith("psd")) { //如果檔名沒有以__macosx/開頭,且以psd結尾,就是psd檔案,解壓,在mac下壓縮的檔案,會自動加上__macosx目錄,但其實是沒用的 BufferedInputStream bis = new BufferedInputStream( zipFile.getInputStream(entry)); File file = new File(targetPath + System.currentTimeMillis()+".psd"); //一次讀40K int BUFFER=40960; FileOutputStream fos = new FileOutputStream(file); BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER); int count; byte data[] = new byte[BUFFER]; while ((count = bis.read(data, 0, BUFFER)) != -1) { bos.write(data, 0, count); } bos.flush(); bos.close(); bis.close(); } } zipFile.close(); }