Java使用线程池递归压缩文件夹下面的所有子文件
2023-03-09 22:17:39 时间
本文将介绍Java中利用线程池递归的方式压缩文件夹下面的所有子文件,具体方法如下:
Gzip单个文件压缩
对于单个文件使用GZip压缩。
- package date0805.demo1;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.zip.GZIPOutputStream;
- public class GZipRunnable implements Runnable {
- private final File file;
- public GZipRunnable(File file) {
- this.file=file;
- }
- @Override
- public void run() {
- if(!file.getName().endsWith(".gz")) {
- File outputFile=new File(file.getParent(),file.getName()+".gz");
- if(!outputFile.exists()) {
- CountTime countTime = new CountTime(file);
- Thread t=new Thread(countTime);
- t.start();
- try(
- InputStream in =new BufferedInputStream(new FileInputStream(file));
- OutputStream out=new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(outputFile)));
- ) {
- int b;
- while((b=in.read())!=-1)
- out.write(b);
- out.flush();
- } catch(Exception ex) {
- ex.printStackTrace();
- }
- t.interrupt();
- } else {
- System.out.println(outputFile+"文件已经存在,无法压缩!");
- }
- }
- }
- }
创建线程池进行递归压缩
要实现对一个文件夹下面的所有的子文件进行压缩,首先递归找出所有子文件,然后调用压缩线程进行压缩。
- package date0805.demo1;
- import java.io.File;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- public class GZipFiles {
- private final static int THREAD_COUNT=4;
- private static ExecutorService service=Executors.newFixedThreadPool(THREAD_COUNT);
- public static void GZip(File fileArgs) {
- if(!fileArgs.isDirectory()) {
- GZipRunnable gZipRunnable = new GZipRunnable(fileArgs);
- service.submit(gZipRunnable);
- }
- else {
- File[] files = fileArgs.listFiles();
- for(File f:files) {
- GZip(f);
- }
- }
- }
- public static void shutdown() {
- service.shutdown();
- }
- }
压缩提示
- package date0805.demo1;
- import java.io.File;
- public class CountTime implements Runnable {
- private File file;
- public CountTime(File file) {
- super();
- this.file = file;
- }
- @Override
- public void run() {
- System.out.println("正在压缩"+file.getAbsolutePath());
- try {
- while(true) {
- System.out.print(".");
- Thread.sleep(1000);
- }
- }catch (InterruptedException e) {
- System.out.println();
- System.out.println(file.getAbsolutePath()+".gz已经压缩完成!");
- }
- }
- }
测试代码
- package date0805.demo1;
- import java.io.File;
- public class Test {
- public static void main(String[] args) {
- File file=new File("D://新建文件夹");
- GZipFiles.GZip(file);
- GZipFiles.shutdown();
- }
- }
相关文章
- Java要抛弃祖宗的基业,Java程序员危险了!
- 十大 Java 语言特性
- JVM 三色标记算法,原来是这么回事!
- 聊聊 Spring 事务控制策略以及 @Transactional 失效问题避坑
- 写给 Java 程序员的前端 Promise 教程
- 写给 Java 程序员的前端 Promise 教程,你学会了吗?
- Java 中为什么不全部使用 Static 方法?
- Java 池化技术你了解多少?
- Java 服务 Docker 容器化优秀实践
- Spring Boot + EasyExcel导入导出,简直太好用了!
- 我们一起聊聊 Java 内存泄漏
- CentOS 下安装 Docker 极简教程
- JDK 19 功能集冻结:Java 19 只有七个新特性
- 关于 CMS 垃圾回收器,你真的懂了吗?
- 为什么会有这么多编程语言?
- 改善Java代码的八个建议
- 接口流量突增,如何做好性能优化?
- Java 以编程方式创建JAR文件
- POJO、Java Bean是如何定义的
- Spring 的 Bean 明明设置了 Scope 为 Prototype,为什么还是只能获取到单例对象?