JAVA多线程进行压缩文件(线程池)(CountDownLatch)详解

先说说我要做的事情吧,上一遍我已经写出了这么把文件夹进行压缩成一个zip包,所以在这个文章的基础上面就可以实现,但是问题是我这边要弄一个批量的操作,就是一次下载很多文件然后放到目录中,在进行压缩,并且文件还是线上的文件,所以我的进行下载文件到本地,这里是很需要时间的,所以在下载线上的文件这里我想着用多线程去进行下载,然后一次去进行压缩文件,好了看看代码吧

       String realPath = new File(request.getSession().getServletContext().getRealPath("/")).getParent(); //获取项目路径
        String mrker = realPath+"\\"+"合同文件"; //要创建的目录名字
        //线程池
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(10,10,1L,
                TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(3), Executors.defaultThreadFactory(),new ThreadPoolExecutor.DiscardOldestPolicy());
        for(int i=0;i<batchSignOrderList.size();i++){ //循环 因为有很多文件夹要进行下载
            Map<String, Object> map = batchSignOrderList.get(i); //获取map 对象 然后在拿值
            String contractName = map.get("contract_name").toString(); //文件名字 我们的文件是 本地保存过名字 然后拼接域名的直接就可以访问 比如 121212121212.pdf
            String url = "http://xxxxxxxxx.cn/temp/"+contractName; //这样就可以访问文件 然后下载
            String filePath = toUtf8String(url); //给访问路劲进行 编码 防止中文 访问
            threadPool.execute(new CarMergePdfFilesCallable(filePath, map.get("batchNo").toString(), mrker)); //执行下载文件方法
        }
        //创建文件 zip
        String newzip = ZipFileUtils.createNewzip(realPath + "\\" + "待签约合同文件" + ".zip");
        //文件加入  FileOutputStream
        FileOutputStream fos1 = new FileOutputStream(new File(newzip));
        ZipFileUtils.toZip(mrker, fos1,true); //进行生成 zip文件夹
        download(response,newzip); //这里是文件
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Java多线程压缩文件相关的类来实现多线程压缩文件。以下是一个简单的实现方法: 1. 首先,创建一个实现Runnable接口的类,用于执行压缩文件的任务。在这个类中,我们可以使用Java的ZipOutputStream类来压缩文件。 2. 在主程序中,创建一个线程池,用于执行多个任务。然后,将每个任务添加到线程池中。 3. 在添加任务之前,我们需要先将要压缩的文件列表传递给每个任务。这可以通过构造函数或setter方法来完成。 4. 在每个任务中,我们可以使用JavaCountDownLatch类来等待所有线程完成任务。这可以确保所有文件都已经被压缩。 5. 最后,我们可以将所有压缩后的文件合并成一个文件。这可以使用Java的ZipFileUtils类来完成。 下面是一个简单的示例代码: ``` 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; public class ZipTask implements Runnable { private List<File> fileList; private String outputZipFile; private CountDownLatch latch; public ZipTask(List<File> fileList, String outputZipFile, CountDownLatch latch) { this.fileList = fileList; this.outputZipFile = outputZipFile; this.latch = latch; } @Override public void run() { try { ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(outputZipFile)); for (File file : fileList) { addFileToZip(file, zos); } zos.close(); } catch (IOException e) { e.printStackTrace(); } finally { latch.countDown(); } } private void addFileToZip(File file, ZipOutputStream zos) throws IOException { FileInputStream fis = new FileInputStream(file); ZipEntry zipEntry = new ZipEntry(file.getName()); zos.putNextEntry(zipEntry); byte[] bytes = new byte[1024]; int length; while ((length = fis.read(bytes)) >= 0) { zos.write(bytes, 0, length); } zos.closeEntry(); fis.close(); } } public class Main { public static void main(String[] args) throws InterruptedException { List<File> fileList = ...; // 获取要压缩的文件列表 int numThreads = ...; // 指定线程数 String outputZipFile = ...; // 指定输出文件名 ExecutorService executor = Executors.newFixedThreadPool(numThreads); CountDownLatch latch = new CountDownLatch(numThreads); int numFilesPerThread = fileList.size() / numThreads; int startIndex = 0; int endIndex = numFilesPerThread; for (int i = 0; i < numThreads; i++) { if (i == numThreads - 1) { endIndex = fileList.size(); } List<File> subList = fileList.subList(startIndex, endIndex); executor.execute(new ZipTask(subList, "output" + i + ".zip", latch)); startIndex = endIndex; endIndex += numFilesPerThread; } latch.await(); // 合并所有压缩文件 ZipFileUtils.mergeZipFiles(outputZipFile, "output*.zip"); executor.shutdown(); } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值