新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Java使用線程池遞歸壓縮文件夾下面的所有子文件
本文將介紹Java中利用線程池遞歸的方式壓縮文件夾下面的所有子文件,具體方法如下:

成都創(chuàng)新互聯(lián)制作網(wǎng)站網(wǎng)頁找三站合一網(wǎng)站制作公司,專注于網(wǎng)頁設(shè)計(jì),成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作,網(wǎng)站設(shè)計(jì),企業(yè)網(wǎng)站搭建,網(wǎng)站開發(fā),建網(wǎng)站業(yè)務(wù),680元做網(wǎng)站,已為上千家服務(wù),成都創(chuàng)新互聯(lián)網(wǎng)站建設(shè)將一如既往的為我們的客戶提供最優(yōu)質(zhì)的網(wǎng)站建設(shè)、網(wǎng)絡(luò)營銷推廣服務(wù)!
Gzip單個(gè)文件壓縮
對(duì)于單個(gè)文件使用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+"文件已經(jīng)存在,無法壓縮!");
- }
- }
- }
- }
創(chuàng)建線程池進(jìn)行遞歸壓縮
要實(shí)現(xiàn)對(duì)一個(gè)文件夾下面的所有的子文件進(jìn)行壓縮,首先遞歸找出所有子文件,然后調(diào)用壓縮線程進(jìn)行壓縮。
- 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已經(jīng)壓縮完成!");
- }
- }
- }
測試代碼
- 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();
- }
- }
網(wǎng)頁題目:Java使用線程池遞歸壓縮文件夾下面的所有子文件
網(wǎng)站URL:http://m.fisionsoft.com.cn/article/dhdhegh.html


咨詢
建站咨詢
