ZipHelper压缩工具类

public final class ZipHelper {
 private static final Logger logger = LoggerFactory.getLogger(ZipHelper.class);
 private ZipHelper() {}
 
 /**
  * 方法说明:<br>
  * 解压<tt>tar.gz</tt>类型文件并写入<tt>outputDir</tt>对应的目录
  *
  * @param zipFileName tar.gz格式的压缩文件
  * @param outputDir 输入文件目录
  * @throws Exception
  */
 public static void unZipTargz(String zipFileName, String outputDir) throws Exception {
  ArchiveInputStream in = null;
  BufferedInputStream bis = null;
  BufferedOutputStream bos = null;
  byte[] b = null;
  File file = null;
  char last = outputDir.charAt(outputDir.length()-1);
  String dir = last == '/' || last == '\\' ? outputDir : outputDir+'/';
  try {
   GZIPInputStream gis = new GZIPInputStream(new BufferedInputStream(new FileInputStream(zipFileName)));
   in = new ArchiveStreamFactory().createArchiveInputStream("tar", gis);
   TarArchiveEntry entry = null;
   while (null != (entry = (TarArchiveEntry) in.getNextEntry())) {
    String name = entry.getName();
    file = new File(dir+name);
    if(name.charAt(name.length()-1) == '/') {
     if (!file.exists()) {
      try{
       file.mkdirs();
      }catch(SecurityException e) {
       throw new IOException("[ It doesn't own the create directory rights. ]");
      }
     }
    } else {
     try{
      file.createNewFile();
     }catch(SecurityException e) {
      throw new IOException("[ It doesn't own the create directory rights. ]");
     }
     try {
      bis = new BufferedInputStream(in);
      bos = new BufferedOutputStream(new FileOutputStream(file));
      b = new byte[bis.available()];
      bis.read(b);
      bos.write(b);
      bos.flush();
     } finally {
      try {
       if(null != bos) { bos.close(); }
      } catch (IOException e) {
      }
     }
    }
   }
  } finally {
   try {
    if(null != bis) { bis.close(); }
    if(null != in) { in.close(); }
   } catch (IOException e) {
   }
  }
 }
 
 /**
  * 方法说明:<br>
  * 解压<tt>zip</tt>类型文件并写入<tt>outputDir</tt>对应的目录
  *
  * @param zipFileName zip格式的压缩文件
  * @param outputDir 输入文件目录
  * @throws Exception
  */
 public static void unZip(String zipFileName, String outputDir) throws Exception {
  File zipFile = new File(zipFileName);  
  File pathFile = new File(outputDir); 
  if(!pathFile.exists()){ 
   try{
       pathFile.mkdirs();
   }catch(SecurityException e) {
    throw new IOException("[ It doesn't own the create directory rights. ]");
   }
  } 
  char last = outputDir.charAt(outputDir.length()-1);
  String dir = last == '/' || last == '\\' ? outputDir : outputDir+'/';
  
  InputStream in = null;
  OutputStream out = null;
  ZipFile zip =  null;
  try{
   zip = new ZipFile(zipFile); 
   for(Enumeration<ZipArchiveEntry> entries = zip.getEntries();entries.hasMoreElements();){ 
    ZipArchiveEntry entry = (ZipArchiveEntry)entries.nextElement(); 
       String zipEntryName = entry.getName(); 
       in = zip.getInputStream(entry); 
       String outPath = dir+zipEntryName; 
    //判断路径是否存在,不存在则创建文件路径 
    File file = new File(outPath.substring(0, outPath.lastIndexOf('/'))); 
    if(!file.exists()){ 
     try{
         file.mkdirs(); 
     } catch (SecurityException e) {
      throw new IOException("[ It doesn't own the create directory rights. ]");
     }
    } 
    //判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压 
    if(new File(outPath).isDirectory()){ 
        continue; 
    } 
     
    out = new FileOutputStream(outPath); 
    byte[] buf1 = new byte[1024]; 
    int len; 
    while((len=in.read(buf1))>0){ 
        out.write(buf1,0,len); 
    } 
    
   } 
  }finally{
   try {
    if(null != in) { in.close(); }
    if(null != out) { out.close(); }
    if(null != zip){zip.close();}
   }catch(Exception ex){
    logger.info("正常释放资源");
   }
  }
 }   
       
    public static boolean isPics(String filename)   
    {   
        boolean flag = false;   
           
        if(filename.endsWith(".jpg") || filename.endsWith(".gif")  || filename.endsWith(".bmp") || filename.endsWith(".png"))   
            flag = true;   
           
        return flag;  
 }
 
 
 public static void main(String[] args){
  String filePath = "D:\\work\\acquirer\\recon\\20130904\\b2c\\UPOP.zip";
  String outputDir = "D:\\work\\acquirer\\recon\\20130904\\b2c\\UPOP";//解压到文件
  try {
   ZipHelper.unZip(filePath, outputDir);
  } catch (Exception e) {
  }
 }
}

转载于:https://my.oschina.net/u/2345354/blog/476680

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值