java文件打包zip_JAVA文件打包ZIP

package com.amdox.busi.media.util;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.List;import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;import org.slf4j.Logger;import org.slf4j.LoggerFactory;/** * 文件打包工具类 * @author julong * @date 2016年5月9日 上午11:09:14 */public class ZipUtil {/** * 日志 * @author julong * @date 2016年5月9日 上午11:10:26 */private static final Logger logger = LoggerFactory.getLogger(ZipUtil.class);/** * 文件上传路径 * @author julong * @date 2016年5月9日 下午3:23:01 */public static final String uploadPath = "C://";/** * zip包存放地址 * @author julong * @date 2016年5月9日 下午3:23:41 */public static final String zipPath = "C://";/** * 文件打包的方法 * @param files 文件信息集合(文件的存放完整路径名称) * @param savePath 存储打包文件的路径 * @return true文件打包成功 false 文件打包失败 * @author julong * @date 2016年5月9日 上午11:12:43 */public static boolean createFilesToZip(List files,String savePath){logger.info("文件开始打包....");boolean result = false;//定义字节流byte[] buffer = new byte[1024];//定义zip流ZipOutputStream out = null;try {//定义打包文件名和存放的路径logger.info("当前保存文件的名称和存储路径是:"+savePath);out = new ZipOutputStream(new FileOutputStream(savePath)); if(null != files && !files.isEmpty()){logger.info("此次打包文件一共:"+files.size());for(int i=0;i0) { out.write(buffer,0,len); } out.closeEntry(); fis.close(); }} }out.close(); logger.info("文件打包完毕....");result = true;} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}finally{if(null != out){try {out.close();} catch (IOException e) {}}}return result;}/** * 测试方法 * @param args * @author julong * @date 2016年5月9日 上午11:10:34 */public static void main(String[] args) {// TODO Auto-generated method stub// TODO Auto-generated method stubbyte[] buffer = new byte[1024]; //生成的ZIP文件名为Demo.zip String strZipName = "C:/zip23.zip"; try {ZipOutputStream out = new ZipOutputStream(new FileOutputStream(strZipName)); //需要同时下载的两个文件result.txt ,source.txt File[] file1 = {new File("c:/a.txt"),new File("c:/b.txt"),new File("c:/c.txt"),new File("c:/d.txt")}; for(int i=0;i0) { out.write(buffer,0,len); } out.closeEntry(); fis.close(); } out.close(); } catch (Exception e) {// TODO: handle exception}}}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值