查看: 759|回复: 0

[Java代码] Java使用线程池递归压缩文件夹下面的所有子文件

发表于 2017-1-8 10:44:47
Gzip单个文件压缩

对于单个文件使用GZip压缩。


  1. package date0805.demo1;
  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedOutputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileOutputStream;
  7. import java.io.InputStream;
  8. import java.io.OutputStream;
  9. import java.util.zip.GZIPOutputStream;
  10. public class GZipRunnable implements Runnable {
  11. private final File file;
  12. public GZipRunnable(File file) {
  13. this.file=file;
  14. }
  15. @Override
  16. public void run() {
  17. if(!file.getName().endsWith(".gz")) {
  18. File outputFile=new File(file.getParent(),file.getName()+".gz");
  19. if(!outputFile.exists()) {
  20. CountTime countTime = new CountTime(file);
  21. Thread t=new Thread(countTime);
  22. t.start();
  23. try(
  24. InputStream in =new BufferedInputStream(new FileInputStream(file));
  25. OutputStream out=new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(outputFile)));
  26. ) {
  27. int b;
  28. while((b=in.read())!=-1)
  29. out.write(b);
  30. out.flush();
  31. } catch(Exception ex) {
  32. ex.printStackTrace();
  33. }
  34. t.interrupt();
  35. } else {
  36. System.out.println(outputFile+"文件已经存在,无法压缩!");
  37. }
  38. }
  39. }
  40. }
复制代码



创建线程池进行递归压缩

要实现对一个文件夹下面的所有的子文件进行压缩,首先递归找出所有子文件,然后调用压缩线程进行压缩。


  1. package date0805.demo1;
  2. import java.io.File;
  3. import java.util.concurrent.ExecutorService;
  4. import java.util.concurrent.Executors;
  5. public class GZipFiles {
  6. private final static int THREAD_COUNT=4;
  7. private static ExecutorService service=Executors.newFixedThreadPool(THREAD_COUNT);
  8. public static void GZip(File fileArgs) {
  9. if(!fileArgs.isDirectory()) {
  10. GZipRunnable gZipRunnable = new GZipRunnable(fileArgs);
  11. service.submit(gZipRunnable);
  12. }
  13. else {
  14. File[] files = fileArgs.listFiles();
  15. for(File f:files) {
  16. GZip(f);
  17. }
  18. }
  19. }
  20. public static void shutdown() {
  21. service.shutdown();
  22. }
  23. }
复制代码



压缩提示


  1. package date0805.demo1;
  2. import java.io.File;
  3. public class CountTime implements Runnable {
  4. private File file;
  5. public CountTime(File file) {
  6. super();
  7. this.file = file;
  8. }
  9. @Override
  10. public void run() {
  11. System.out.println("正在压缩"+file.getAbsolutePath());
  12. try {
  13. while(true) {
  14. System.out.print(".");
  15. Thread.sleep(1000);
  16. }
  17. }catch (InterruptedException e) {
  18. System.out.println();
  19. System.out.println(file.getAbsolutePath()+".gz已经压缩完成!");
  20. }
  21. }
  22. }
复制代码



测试代码



  1. package date0805.demo1;
  2. import java.io.File;
  3. public class Test {
  4. public static void main(String[] args) {
  5. File file=new File("D:\\新建文件夹");
  6. GZipFiles.GZip(file);
  7. GZipFiles.shutdown();
  8. }
  9. }
复制代码



来自:http://my.oschina.net/zzw922cn/blog/488382



回复

使用道具 举报