java中压缩文件:一次性压缩多个文件到zip中

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.springframework.util.StringUtils;
/**
     * @Title: 压缩文件
     * @Description: 一次性压缩多个文件到zip中
     * @param filePaths 需要压缩的文件地址列表(绝对路径)
     * @param zipFilePath 需要压缩到哪个zip文件(无需创建这样一个zip,只需要指定一个全路径)
     * @param keepDirStructure 压缩后目录是否保持原目录结构
     * @throws IOException    
     * @return int   压缩成功的文件个数
     */
     public static int compress(List<String> filePaths, String zipFilePath,Boolean keepDirStructure) throws IOException{
         byte[] buf = new byte[1024];
         File zipFile = new File(zipFilePath);
         //zip文件不存在,则创建文件,用于压缩
         if(!zipFile.exists())
             zipFile.createNewFile();
         int fileCount = 0;//记录压缩了几个文件?
         try {
             ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));
             for(int i = 0; i < filePaths.size(); i++){
                 String relativePath = filePaths.get(i);
                 if(StringUtils.isEmpty(relativePath)){
                     continue;
                 }
                 File sourceFile = new File(relativePath);//绝对路径找到file
                 if(sourceFile == null || !sourceFile.exists()){
                     continue;
                 }
                 
                 FileInputStream fis = new FileInputStream(sourceFile);
                 if(keepDirStructure!=null && keepDirStructure){
                     //保持目录结构
                     zos.putNextEntry(new ZipEntry(relativePath));
                 }else{
                     //直接放到压缩包的根目录
                     zos.putNextEntry(new ZipEntry(sourceFile.getName()));
                 }
                 //System.out.println("压缩当前文件:"+sourceFile.getName());
                 int len;
                 while((len = fis.read(buf)) > 0){
                     zos.write(buf, 0, len);
                 }
                 zos.closeEntry();
                 fis.close();
                 fileCount++;
             }
             zos.close();
             //System.out.println("压缩完成");
         } catch (Exception e) {
             e.printStackTrace();
         }
         return fileCount;
     }

测试

 public static void main(String[] args) throws IOException {
         List<String> sourceFilePaths = new ArrayList<String>();
         sourceFilePaths.add("d:/test/1.ofd");
         sourceFilePaths.add("d:/test/新建文件夹/1.pdf");
         sourceFilePaths.add("d:/test/2.jpg");//试一个找不到的文件
         //指定打包到哪个zip(绝对路径)
         String zipTempFilePath = "D:/test/test.zip";
         //调用压缩
         int s = compress(sourceFilePaths, zipTempFilePath,false);
         System.out.println("成功压缩"+s+"个文件");
    }

测试结果
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值