?? unzip.java
字號:
import java.util.zip.*;
import java.io.*;
public class Unzip
{
/*
* 檢查目標文件是否存在
* @param name 目標路徑
* @return 文件路徑
*/
public String checkDirectory(String name)
{
File f=new File(name);
if(f.exists())
{
return f.getAbsolutePath();
}
else
{
f.mkdir();
return f.getAbsolutePath();
}
}
/*
* 解壓縮ZIP文件函數
* @param inputZipFIle 要解壓的ZIP文件
* @param outDirectory 解壓縮目錄
*/
public boolean unZip(String inputZipFile,String outDirectory){
boolean b = false;
try{
File f1=new File(inputZipFile);
String out=checkDirectory(outDirectory+f1.getName().subSequence(0, f1.getName().indexOf(".")));
FileInputStream fin = new FileInputStream(inputZipFile);
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry z;
while((z=zin.getNextEntry())!= null){
if(z.isDirectory()){
String name = z.getName();
//System.out.println(name);
name = name.substring(0,name.length()-1);
File f = new File(out+File.separator+name);
f.mkdir(); //創建解壓目錄
}else{
System.out.println(out+File.separator+z.getName());
File f = new File(out+File.separator+z.getName());
f.createNewFile(); //創建解壓文件
FileOutputStream fout = new FileOutputStream(f);
int buf;
while((buf = zin.read())!= -1){
fout.write(buf);
}
fout.close();
}
}
fin.close();
zin.close();
f1.delete();
f1.exists();
b = true;
}catch(Exception e){
e.printStackTrace();
}
return b;
}
/*
* 測試函數
*/
public static void main(String[] args)
{
Unzip zip=new Unzip();
boolean flag=zip.unZip("E:\\Facade.zip","");
System.out.println(flag);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -