public class UnZipper {
/**
* 解壓縮檔案到目前目錄功能相當於右鍵選擇解壓縮
* @param zipFile
* @param
* @author gabriel
*/
@SuppressWarnings("rawtypes")
public static void unZipFiles(File zipFile)throws IOException{
//得到壓縮檔案所在目錄
String path=zipFile.getAbsolutePath();
path=path.substring(0,path.lastIndexOf("//"));
// System.out.println("path "+path);
ZipFile zip = new ZipFile(zipFile);
for(Enumeration entries =zip.entries();
entries.hasMoreElements();){
ZipEntry entry = (ZipEntry)entries.nextElement();
String zipEntryName = entry.getName();
InputStream in = zip.getInputStream(entry);
//outPath輸出目錄
String outPath = (path+"//"+zipEntryName).replaceAll("//*", "/");;
//System.out.println("outPath "+outPath);
//判斷路徑是否存在,不存在則建立檔案路徑
File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
if(!file.exists()){
file.mkdirs();
}
//判斷檔案全路徑是否為資料夾,如果是上面已經上傳,不需要解壓縮
if(new File(outPath).isDirectory()){
continue;
}
//輸出檔案路徑資訊
System.out.println(outPath);
OutputStream out = new FileOutputStream(outPath);
byte[] buf1 = new byte[1024];
int len;
while((len=in.read(buf1))>0){
out.write(buf1,0,len);
}
in.close();
out.close();
}
System.out.println("******************解壓縮完畢********************");
}
public static void main(String[] args) {
try {
unZipFiles(new File("D://all//zip//Default.adiumemoticonset.zip"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}