解压缩工具类

public class ZipUtils {
 private static final Logger logger = Logger.getLogger(ZipUtils.class);

 /**
  * 下载zip文件流
  *
  * @param filePath
  * @param response
  * @throws Exception
  */
 public static void downloadFileZip(String filePath,HttpServletResponse response) throws Exception{
  ServletOutputStream outputStream = null;
  FileInputStream zipfile = null;
  try {
   //下载zip文件
      String fileSimpleName = filePath.substring(filePath.lastIndexOf("//")+1).split("//.")[0];
   outputStream = response.getOutputStream();
   response.setContentType("application/zip");
   response.setCharacterEncoding("UTF-8");
   response.setHeader("Content-Disposition", "attachment;filename=/""
   + URLEncoder.encode(fileSimpleName, "UTF-8") + ".zip" + "/"");
   byte[] content = new byte[1024];
   zipfile = new FileInputStream(filePath);
   while (zipfile.read(content) != -1) {
    outputStream.write(content);
   }
   outputStream.flush();
   outputStream.close();
   zipfile.close();
  }catch(Exception e) {
   if(outputStream != null) outputStream.close();
   if(zipfile != null) zipfile.close();
   e.printStackTrace();
   logger.error("文件压缩下载失败");
  }
 }
 
 /**
  * 对directory目录下的文件压缩,保存为指定的文件zipFile
  *
  * @param directory
  * @param zipFile
  */
 public static void zip(String directory, String zipFile) {
  ZipOutputStream zos = null;
  try {
   zos = new ZipOutputStream(new FileOutputStream(zipFile));
   fileZip(zos, new File(directory));
  } catch (Exception e) {
   e.printStackTrace();
   logger.error("压缩文件出错");
  } finally {
   try {
    if (zos != null) {
     zos.close();
    }
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
 }
 
 /**
  * 把指定文件解压缩到指定目录,支持多级目录解压
  * @param zipFile    压缩文件
  * @param directory  目标目录
  * @throws Exception
  */
 public static void unzip(String zipFile, String directory) {
  if (null == directory || "".equals(directory)) { // 处理传进来的目标目录为空
   directory = zipFile.substring(0, zipFile.lastIndexOf("."));
  }
  ZipInputStream zis = null;
  try {
   zis = new ZipInputStream(new FileInputStream(zipFile));
   File f = new File(directory);
   f.mkdirs();
   fileUnZip(zis, f);
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   if (zis != null) {
    try {
     zis.close();
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  }
 }

 
 /**
  * 解压缩文件
  *
  * @param zis    压缩文件输入流
  * @param file   解压后保存的文件目录
  * @throws Exception
  */
 private static void fileUnZip(ZipInputStream zis, File file)
   throws Exception {
  ZipEntry zip = zis.getNextEntry();
  if (zip == null)
   return;
  String name = zip.getName(); // 得到压缩文件中文件名称
  File f = new File(file.getAbsolutePath() + File.separator + name);
  if (zip.isDirectory()) {
   f.mkdirs();
   fileUnZip(zis, file);
  } else {
   f.createNewFile();
   BufferedOutputStream bfos = null;
   try {
    bfos = new BufferedOutputStream(new FileOutputStream(f));
    byte b[] = new byte[2048];
    int aa = 0;
    while ((aa = zis.read(b)) != -1) {
     bfos.write(b, 0, aa);
    }
   } catch (Exception e) {
    e.printStackTrace();  
   } finally {
    try {
     bfos.close();
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
   fileUnZip(zis, file); // 解压下一个条目
  }
 }

 /**
  * 压缩文件
  *
  * @param zos
  * @param file
  * @throws Exception
  */
 private static void fileZip(ZipOutputStream zos, File file) throws Exception {
  if (file.isFile()) {
   zos.putNextEntry(new ZipEntry(file.getName()));
   FileInputStream fis = null;
   try {
    fis = new FileInputStream(file);
    byte[] bb = new byte[2048];
    int aa = 0;
    while ((aa = fis.read(bb)) != -1) {
     zos.write(bb, 0, aa);
    }
   } catch (Exception e) {
    throw e;
   } finally {
    if (fis != null) {
     try {
      fis.close();
     } catch (Exception e) {
      e.printStackTrace();
     }
    }
   }
  } else {
   directoryZip(zos, file, "");
  }
 }

 /**
  * 压缩目录文件
  * @param out
  * @param f
  * @param base
  * @throws Exception
  */
 private static void directoryZip(ZipOutputStream out, File f, String base)
   throws Exception {
  // 如果传入的是目录
  if (f.isDirectory()) {
   File[] fl = f.listFiles();
   // 创建压缩的子目录
   out.putNextEntry(new ZipEntry(base + "/"));
   if (base.length() == 0) {
    base = "";
   } else {
    base = base + "/";
   }
   for (int i = 0; i < fl.length; i++) {
    directoryZip(out, fl[i], base + fl[i].getName());
   }
  } else {
   // 把压缩文件加入zip中
   out.putNextEntry(new ZipEntry(base));
   FileInputStream in = null;
   try {
    in = new FileInputStream(f);
    byte[] bb = new byte[2048];
    int aa = 0;
    while ((aa = in.read(bb)) != -1) {
     out.write(bb, 0, aa);
    }
   } catch (Exception e) {
    throw e;
   } finally {
    if (in != null) {
     try {
      in.close();
     } catch (Exception e) {
      e.printStackTrace();
     }
    }
   }
  }
 }
 
 public static void main(){
  
 }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值