压缩包 解压包 保存包到网络 文件打包

//定义压缩包的目录 以及压缩包的名字
     String zipFilePath = filePath+patrr.getTaskNo()+".zip";
     /**保存网络文件*/
     QunarUtil.saveURLFile(fileUrl, zipFilePath);

   /**保存网络文件*/
  public static void saveURLFile(String destUrl, String fileName) throws IOException {
  InputStream in = new URL(destUrl).openStream();
  byte[] data = IOUtils.toByteArray(in);
  FileUtils.writeByteArrayToFile(new File(fileName), data);
  IOUtils.closeQuietly(in);
 }

     log.info(QunarUtil.getStringDate()+":去哪儿全量增加-->开始解压"+filePath+patrr.getTaskNo()+".zip");

     QunarUtil.unzip(filePath+patrr.getTaskNo()+".zip", filePath+patrr.getTaskNo()+"/");

 /**
   * 解压zip文件
      * @throws IOException
   */
  public static void unzip(String srcFile, String destPath) throws IOException {

   File outFile = new File(destPath);
   if (!outFile.exists()) {   
    outFile.mkdirs();  
   }  
   org.apache.commons.compress.archivers.zip.ZipFile zipFile = new org.apache.commons.compress.archivers.zip.ZipFile(srcFile);
   Enumeration<?> en = zipFile.getEntries();
   ZipArchiveEntry zipEntry = null; 
   while (en.hasMoreElements()) {  
    zipEntry = (ZipArchiveEntry) en.nextElement();
    if (zipEntry.isDirectory()) {  
     // mkdir directory  
     String dirName = zipEntry.getName();  
     dirName = dirName.substring(0, dirName.length() - 1);  
     File f = new File(outFile.getPath() + "/" + dirName); 
     f.mkdirs();   
    }
    else {   
     // unzip file  
     File f = new File(outFile.getPath() + "/" + zipEntry.getName());
     if(!f.getParentFile().exists()){
      f.getParentFile().mkdirs();
     }
     f.createNewFile();
     InputStream in = zipFile.getInputStream(zipEntry);  
     OutputStream out = new FileOutputStream(f);  
     IOUtils.copy(in, out);   
     out.close(); 
     in.close();   
     } 
   }
   zipFile.close(); 
  }

 


     //删掉ZIP
     File dbFile = new File(zipFilePath);
     dbFile.delete();
     

 

   /**
   * 压缩成zip文件
      * @throws IOException
   */
  public static void tozip(String srcFile, String destPath) throws IOException {
   
   File outFile = new File(destPath);
   if (!outFile.exists()) {   
    outFile.mkdirs();  
   }
   
   //定义要压缩的文件  也就是说在D盘里有个 demo.txt 的文件(必须要有,否者会有异常,实际应用中可判断);  
    File file = new File(srcFile);    
    //定义压缩文件的名称  
    File zipFile = new File(destPath + "qunar.zip");    
    //定义输入文件流  
    InputStream input = new FileInputStream(file);    
    //定义压缩输出流  
    ZipOutputStream zipOut = null;    
    //实例化压缩输出流,并制定压缩文件的输出路径  就是D盘下,名字叫 demo.zip  
    zipOut = new ZipOutputStream(new FileOutputStream(zipFile));    
    zipOut.putNextEntry(new ZipEntry(file.getName()));    
    //设置注释  zipOut.setComment("www.demo.com");  
    int temp = 0;  
    while((temp = input.read()) != -1) {   
     zipOut.write(temp);  
     }  
    input.close();  
    zipOut.close();
  }
  

/**
   * 文件打包成ZIP
   *
   * @param filePath
   * @param zipFilePath
   * @throws Exception
   */
  public static void zip(String filePath, String zipFilePath)
   throws Exception {
  FileOutputStream dest = null;
  ZipOutputStream out = null;
  BufferedInputStream origin = null;
  try {
   dest = new FileOutputStream(zipFilePath);
   out = new ZipOutputStream(new BufferedOutputStream(dest));
   byte data[] = new byte[BUFFER];
   File f = new File(filePath);
//   File files[] = f.listFiles();
//   for (int i = 0; i < files.length; i++) {
    FileInputStream fi = new FileInputStream(f);
    origin = new BufferedInputStream(fi, BUFFER);
    ZipEntry entry = new ZipEntry(f.getName());
    out.putNextEntry(entry);
    int count;
    while ((count = origin.read(data, 0, BUFFER)) != -1) {
     out.write(data, 0, count);
    }
//   }

  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   if (null != origin) {
    origin.close();
   }

   if (null != out) {
    out.close();
   }

   if (null != dest) {
    dest.close();
   }
  }

 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值