JAVA中压缩文件成zip

这是一个Java实现的文件压缩工具类,能够将指定的文件或目录压缩为ZIP格式。主要方法包括`compress`用于压缩文件,`compressPro`分别处理文件和目录的压缩,以及`compressDirectory`和`compressFile`进行递归压缩。代码中包含了详细的注释和使用示例。
摘要由CSDN通过智能技术生成
package com.wxz;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * TODO
 *
 * @author wxz
 * @date 2021/9/30 15:21
 */
public class Test {
    /**
    * @Description: 
    * @param srcPath 要压缩的文件路径
    * @param dstPath 压缩后的zip路径
    * @return void
    * @author wxz
    * @date 2021/11/16 11:41
    */
    public static void compress(String srcPath , String dstPath) throws IOException{
        File srcFile = new File(srcPath);
        File dstFile = new File(dstPath);
        if (!srcFile.exists()) {
            throw new FileNotFoundException(srcPath + "不存在!");
        }
        ZipOutputStream zipOut = null;
        try {
            zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(dstFile)));
            String baseDir = "";
            compressPro(srcFile, zipOut, baseDir);
        }
        finally {
            if(null != zipOut){
                zipOut.close();
            }
        }
    }

    private static void compressPro(File srcFile, ZipOutputStream zipOut,String baseDir) throws IOException{
        if (srcFile.isDirectory()) {
            compressDirectory(srcFile,zipOut,baseDir);
        } else {
            compressFile(srcFile,zipOut,baseDir);
        }
    }

    /** 压缩一个目录 */
    private static void compressDirectory(File dir, ZipOutputStream zipOut,String baseDir) throws IOException{
        File[] files = dir.listFiles();
        for (int i = 0; i < files.length; i++) {
            compressPro(files[i], zipOut, baseDir + dir.getName() + "/");
        }
    }
    /** 压缩一个文件 */
    private static void compressFile(File file, ZipOutputStream zipOut,String baseDir)  throws IOException{
        if (!file.exists()){
            return;
        }
        BufferedInputStream bis = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(file));
            zipOut.putNextEntry(new ZipEntry(baseDir + file.getName()));
            int len;
            byte bytes[] = new byte[1024];
            while ((len = bis.read()) != -1) {
                zipOut.write(bytes, 0, len);
            }
        }finally {
            if(null != bis){
                bis.close();
            }
        }
    }
    
    
    public static void main(String[] args) throws IOException {
//        File file = new File("D:\\aly");
//        File zipFile = new File("D:\\aly.zip");
//        FileInputStream fis = new FileInputStream(file);
//        ZipOutputStream zipOutputStream = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
//        byte[] bytes = new byte[1024];
//        zipOutputStream.putNextEntry(new ZipEntry(file.getName()));
//        int lenth;
//        while((lenth=fis.read())!=-1){
//            zipOutputStream.write(bytes,0,lenth);
//        } 
//        zipOutputStream.close();
//        fis.close();
        String targetFolderPath = "D:\\hello.txt";
        String newZipFilePath = "D:\\Dev\\wxz.zip";
        //将目标目录的文件压缩成Zip文件
        new Test().compress(targetFolderPath , newZipFilePath);
    }
   
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值