Java自動解壓文件實例代碼
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
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);
//判斷路徑是否存在,不存在則創(chuàng)建文件路徑
File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
if(!file.exists()){
file.mkdirs();
}
//判斷文件全路徑是否為文件夾,如果是上面已經(jīng)上傳,不需要解壓
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();
}
}
}
相關(guān)文章
JavaWeb實現(xiàn)學(xué)生信息管理系統(tǒng)(3)
這篇文章主要為大家詳細介紹了JavaWeb實現(xiàn)學(xué)生信息管理系統(tǒng)第三篇,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08java日期格式化YYYY-MM-dd遇坑指南小結(jié)
本文主要介紹了java日期格式化YYYY-MM-dd遇坑指南小結(jié),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08