java將文件或是文件夾打包壓縮成zip格式
import java.io.BufferedInputStream; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipException; import java.util.zip.ZipFile; import java.util.zip.ZipOutputStream; import org.slf4j.Logger; import org.slf4j.LoggerFactory;/* 將文件或是文件夾打包壓縮成zip格式
@author ysc */ public class ZipUtils { private static final Logger log = LoggerFactory.getLogger(ZipUtils.class);
private ZipUtils(){};
/**
- APDPlat中的重要打包機制
- 將jar文件中的某個文件夾里面的內容復制到某個文件夾
- @param jar 包含靜態資源的jar包
- @param subDir jar中包含待復制靜態資源的文件夾名稱
- @param loc 靜態資源復制到的目標文件夾
@param force 目標靜態資源存在的時候是否強制覆蓋 */ public static void unZip(String jar, String subDir, String loc, boolean force){ try {
File base=new File(loc); if(!base.exists()){ base.mkdirs(); } ZipFile zip=new ZipFile(new File(jar)); Enumeration<? extends ZipEntry> entrys = zip.entries(); while(entrys.hasMoreElements()){ ZipEntry entry = entrys.nextElement(); String name=entry.getName(); if(!name.startsWith(subDir)){ continue; } //去掉subDir name=name.replace(subDir,"").trim(); if(name.length()<2){ log.debug(name+" 長度 < 2"); continue; } if(entry.isDirectory()){ File dir=new File(base,name); if(!dir.exists()){ dir.mkdirs(); log.debug("創建目錄"); }else{ log.debug("目錄已經存在"); } log.debug(name+" 是目錄"); }else{ File file=new File(base,name); if(file.exists() && force){ file.delete(); } if(!file.exists()){ InputStream in=zip.getInputStream(entry); FileUtils.copyFile(in,file); log.debug("創建文件"); }else{ log.debug("文件已經存在"); } log.debug(name+" 不是目錄"); } }
} catch (ZipException ex) {
log.error("文件解壓失敗",ex);
} catch (IOException ex) {
log.error("文件操作失敗",ex);
} }
/**
- 創建ZIP文件
- @param sourcePath 文件或文件夾路徑
@param zipPath 生成的zip文件存在路徑(包括文件名) */ public static void createZip(String sourcePath, String zipPath) { FileOutputStream fos = null; ZipOutputStream zos = null; try {
fos = new FileOutputStream(zipPath); zos = new ZipOutputStream(fos); writeZip(new File(sourcePath), "", zos);
} catch (FileNotFoundException e) {
log.error("創建ZIP文件失敗",e);
} finally {
try { if (zos != null) { zos.close(); } } catch (IOException e) { log.error("創建ZIP文件失敗",e); }
} }
private static void writeZip(File file, String parentPath, ZipOutputStream zos) { if(file.exists()){
if(file.isDirectory()){//處理文件夾 parentPath+=file.getName()+File.separator; File [] files=file.listFiles(); for(File f:files){ writeZip(f, parentPath, zos); } }else{ FileInputStream fis=null; try { fis=new FileInputStream(file); ZipEntry ze = new ZipEntry(parentPath + file.getName()); zos.putNextEntry(ze); byte [] content=new byte[1024]; int len; while((len=fis.read(content))!=-1){ zos.write(content,0,len); zos.flush(); } } catch (FileNotFoundException e) { log.error("創建ZIP文件失敗",e); } catch (IOException e) { log.error("創建ZIP文件失敗",e); }finally{ try { if(fis!=null){ fis.close(); } }catch(IOException e){ log.error("創建ZIP文件失敗",e); } } }
} }
public static void main(String[] args) { ZipUtils.createZip("D:\workspaces\netbeans\APDPlat\APDPlat_Web\target\APDPlat_Web-2.2\platform\temp\backup", "D:\workspaces\netbeans\APDPlat\APDPlat_Web\target\APDPlat_Web-2.2\platform\temp\backup.zip"); ZipUtils.createZip("D:\workspaces\netbeans\APDPlat\APDPlat_Web\target\APDPlat_Web-2.2\platform\index.jsp", "D:\workspaces\netbeans\APDPlat\APDPlat_Web\target\APDPlat_Web-2.2\platform\index.zip");} } </pre>