java压缩文件

Java代码 复制代码  收藏代码
  1. /**  
  2.      * 压缩文件  
  3.      *   
  4.      * @method zip_file  
  5.      * @param file_path 需压缩的文件路径  
  6.      */  
  7.     private void zip_file(String file_path){   
  8.         File file = new File(file_path);   
  9.         FileInputStream fileInputStream = null;   
  10.         BufferedInputStream bufferedInputStream = null;   
  11.         ZipOutputStream zipOutputStream = null;   
  12.         try {   
  13.             fileInputStream = new FileInputStream(file);   
  14.             bufferedInputStream = new BufferedInputStream(fileInputStream);   
  15.             byte[] buf = new byte[1024];   
  16.             int len;   
  17.             FileOutputStream fileOutputStream = new FileOutputStream(GlobalVar.serverConfig.getProperty("vasserver.adcftp.srcdir")+file.getName()+".ZIP");   
  18.             BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);   
  19.             zipOutputStream = new ZipOutputStream(bos);//压缩包   
  20.             ZipEntry zipEntry = new ZipEntry(file.getName());//压缩包里的文件名称   
  21.             zipOutputStream.putNextEntry(zipEntry);//写入新的 ZIP 文件条目并将流定位到条目数据的开始处   
  22.             while(-1 != (len=bufferedInputStream.read(buf))){   
  23.                 zipOutputStream.write(buf, 0, len);   
  24.                 zipOutputStream.flush();   
  25.             }   
  26.                
  27.         } catch (FileNotFoundException e) {   
  28.             log.error("未找到需压缩的文件!-->" + e.getMessage());   
  29.             e.printStackTrace();   
  30.             System.out.println("未找到需压缩的文件!-->" + e.getMessage());   
  31.         } catch (IOException e) {   
  32.             log.error("文件压缩异常!-->" + e.getMessage());   
  33.             e.printStackTrace();   
  34.             System.out.println("文件压缩异常!-->" + e.getMessage());   
  35.         } finally {   
  36.             try {   
  37.                 if(null != zipOutputStream)   
  38.                     zipOutputStream.close();   
  39.                 if(null != bufferedInputStream)   
  40.                     bufferedInputStream.close();   
  41.             } catch (IOException e) {   
  42.                 e.printStackTrace();   
  43.             }   
  44.                
  45.         }   
  46.     }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值