本文将介绍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();
    }

}



原文连接:http://www.codeceo.com/article/java-compass-directory.html

欢迎留言