把一个文件夹下所有文件夹压缩成一个zip压缩包

本文提供了一个使用Java实现文件夹压缩的示例代码。通过递归方式将指定路径下的文件压缩成ZIP格式,并支持同时压缩多个路径。文章还包含了一个简单的测试案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

说明:现在对D://test//0000目录进行压缩

一、压缩程序:

package dirtozip;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;


public class DirToZip {
    //zip 为zip压缩包的名称路径,pathName为待压缩的文件路径,可以传多个路径,把多个路径下的文件压缩到一个压缩包里面
       public void compress(String zipFile,String... pathName) {   
            ZipOutputStream zos = null;     
            try {    
                FileOutputStream fileOutputStream = new FileOutputStream(zipFile);     
                CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream,     
                        new CRC32());     
                zos = new ZipOutputStream(cos);     
                String basedir = "";
                //循环读取待压缩路径
                for (int i=0;i<pathName.length;i++){  
                    //对每一个待压缩文件夹进行压缩
                    compress(new File(pathName[i]), zos, basedir);     
                }  
                zos.close();    
            } catch (Exception e) {     
                throw new RuntimeException(e);     
            }   
        }
    
    
      public void compress(String srcPathName) { 
          
            File file = new File(srcPathName);     
            if (!file.exists())     
                throw new RuntimeException(srcPathName + "不存在!");     
            try {   
                //zip包路径
                FileOutputStream fileOutputStream = new FileOutputStream("zipFile");     
                CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream,     
                        new CRC32());     
                ZipOutputStream zos = new ZipOutputStream(cos);     
                String basedir = "";     
                compress(file, zos, basedir);     
                zos.close();     
            } catch (Exception e) {     
                throw new RuntimeException(e);     
            }     
        } 
    
      private void compress(File file, ZipOutputStream zos, String basedir) {     
            /* 判断是目录还是文件 */    
            if (file.isDirectory()) {     
                System.out.println("压缩:" + basedir + file.getName());     
                this.compressDirectory(file, zos, basedir);     
            } else {     
                System.out.println("压缩:" + basedir + file.getName());     
                this.compressFile(file, zos, basedir);     
            }     
        }  
    
    /** 压缩一个目录 */    
    private void compressDirectory(File dir, ZipOutputStream zos, String basedir) {     
        if (!dir.exists())     
            return;     
    
        File[] files = dir.listFiles();     
        for (int i = 0; i < files.length; i++) {     
            /* 递归 */    
            compress(files[i], zos, basedir + dir.getName() + "/");     
        }     
    } 
    
    /** 压缩一个文件 */    
    private void compressFile(File file, ZipOutputStream zos, String basedir) {     
        if (!file.exists()) {     
            return;     
        }     
        try {     
            BufferedInputStream bis = new BufferedInputStream(     
                    new FileInputStream(file));   
            //entry 待压缩文件的名称
            ZipEntry entry = new ZipEntry(basedir + file.getName());     
            zos.putNextEntry(entry);     
            int count;     
            byte data[] = new byte[1024*10];     
            while ((count = bis.read(data, 0, 1024*10)) != -1) {     
                zos.write(data, 0, count);     
            }     
            bis.close();     
        } catch (Exception e) {     
            throw new RuntimeException(e);     
        }     
    } 
        
}
 

二、测试:

package dirtozip;

public class TestZipMain {
    public static void main(String[] args) throws Exception {
        DirToZip dirToZkip = new DirToZip();
        String sourceFilePath1="D://test//0000";
        String sourceFilePath2="D://test//1111";
        String zipFilePath="D://test//zipRes//002.zip";
        dirToZkip.compress(zipFilePath, sourceFilePath1,sourceFilePath2);
        System.out.println("压缩完成...");
    }

}
 

测试结果:

测试数据所在目录位置:

230813_Gfan_3197158.png

230900_MGwD_3197158.png

压缩后的结果:

231058_edoc_3197158.png

转载于:https://my.oschina.net/u/3197158/blog/910948

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值