java zip处理工具

 1、pom依赖

        <!--        zip打包工具-->
        <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant</artifactId>
            <version>1.10.5</version>
        </dependency>

2、utils工具类

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;


public class ZipUtils {

    /**
     * 压缩一个文件或者目录
     *
     * @param zipFileName 压缩后的文件名(绝对路径):
     *                    如E:\app_data\\upload\temp\batchDownload\A2023001_检查.zip
     * @param zipFilePath 需要被压缩的文件路径(绝对路径):
     *                    如E:\app_data\\upload\temp\batchDownload\A2023001_检查
     * @throws Exception
     */
    public static void zip(String zipFileName, String zipFilePath) throws Exception {
        zip(zipFileName, new File(zipFilePath));
    }

    /**
     * @param zipFileName 压缩后的文件名及路径
     * @param zipFilePath 要被压缩的文件的输入流
     * @throws Exception
     */
    public static void zip(String zipFileName, File zipFilePath) throws Exception {
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
        zip(out, zipFilePath, "");
        System.out.println("zip done");
        out.close();
    }

    /**
     * 用于压缩整个目录或者单个文件
     *
     * @param out  源文件的输出流
     * @param f    目标压缩文件的输入流
     * @param base a
     * @throws Exception
     */
    private static void zip(ZipOutputStream out, File f, String base) throws Exception {
        System.out.println("Zipping  " + f.getName());
        if (f.isDirectory()) {
            File[] fl = f.listFiles();
            //out.putNextEntry(new ZipEntry(base+"/"));
            out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + f.getName() + "/"));
            for (int i = 0; i < fl.length; i++) {
                //zip(out,fl[i],base);
                zip(out, fl[i], base + f.getName() + "/");
            }
        } else {
            //base=base.length()==0?"":base+"/";
            out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + f.getName()));
            FileInputStream in = new FileInputStream(f);
            int len;
            byte[] buffer = new byte[1024];
            while ((len = in.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
            in.close();
        }
    }

    /**
     * 解压缩
     *
     * @param zipFileName     压缩文件
     * @param outputDirectory 目标路径
     * @throws Exception
     */
    public static void unzip(String zipFileName, String outputDirectory) throws Exception {
        unzip(zipFileName, outputDirectory, Charset.forName("GBK"));
    }

    /**
     * 解压缩
     *
     * @param zipFile         压缩文件
     * @param outputDirectory 目标路径
     * @throws Exception
     */
    public static void unzip(File zipFile, String outputDirectory) throws Exception {
        ZipInputStream in = new ZipInputStream(new FileInputStream(zipFile));
        ZipEntry z;
        while ((z = in.getNextEntry()) != null) {
            System.out.println("unziping " + z.getName());
            if (z.isDirectory()) {
                String name = z.getName();
                name = name.substring(0, name.length() - 1);
                File f = new File(outputDirectory + File.separator + name);
                f.mkdir();
                System.out.println("mkdir " + outputDirectory + File.separator + name);
            } else {
                File f = new File(outputDirectory + File.separator + z.getName());
                f.createNewFile();
                FileOutputStream out = new FileOutputStream(f);
                int b;
                while ((b = in.read()) != -1) {
                    out.write(b);
                }
                out.close();
            }
        }
        in.close();
    }

    /**
     * 解压缩
     *
     * @param zipFileName     压缩文件
     * @param outputDirectory 目标路径
     * @throws Exception
     */
    public static void unzip(String zipFileName, String outputDirectory, Charset charset) throws Exception {
        ZipInputStream in = new ZipInputStream(new FileInputStream(zipFileName), charset);
        ZipEntry z;
        while ((z = in.getNextEntry()) != null) {
            System.out.println("unziping " + z.getName());
            if (z.isDirectory()) {
                String name = z.getName();
                name = name.substring(0, name.length() - 1);
                File f = new File(outputDirectory + File.separator + name);
                f.mkdir();
                System.out.println("mkdir " + outputDirectory + File.separator + name);
            } else {
                File f = new File(outputDirectory + File.separator + z.getName());
                f.createNewFile();
                FileOutputStream out = new FileOutputStream(f);
                int b;
                while ((b = in.read()) != -1) {
                    out.write(b);
                }
                out.close();
            }
        }
        in.close();
    }

    //测试压缩文件
    public static void main(String[] args) throws Exception {
        String zipFileName = "E:\\test\\aa.zip";
        String zipFilePath = "E:\\test\\aa";
        zip(zipFileName, zipFilePath);
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java后端开发中,可以使用Java自带的工具类来处理压缩文件。其中,java.util.zip包提供了ZipOutputStream类,专门用于对文件进行压缩操作。通过使用ZipOutputStream类,我们可以将文件或文件夹压缩成一个zip文件。 使用ZipOutputStream类进行压缩文件的操作步骤如下: 1. 创建一个ZipOutputStream对象,指定要输出的文件流。 2. 使用putNextEntry方法创建一个新的ZipEntry对象,指定要压缩的文件或文件夹的路径。 3. 使用BufferedInputStream读取要压缩的文件或文件夹的内容,并使用ZipOutputStream的write方法将内容写入压缩文件。 4. 循环执行步骤2和步骤3,直到所有要压缩的文件或文件夹都被处理完毕。 5. 关闭ZipOutputStream对象,完成压缩操作。 下面是一个示例代码,展示了如何使用JavaZipOutputStream类进行文件压缩操作: ```java import java.io.*; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class ZipUtils { public static void compressFile(String sourceFilePath, String zipFilePath) throws IOException { File sourceFile = new File(sourceFilePath); FileOutputStream fos = new FileOutputStream(zipFilePath); ZipOutputStream zos = new ZipOutputStream(fos); compress(sourceFile, zos, ""); zos.close(); fos.close(); } private static void compress(File file, ZipOutputStream zos, String parentPath) throws IOException { if (file.isDirectory()) { // 如果是文件夹,则递归处理子文件夹和文件 File[] files = file.listFiles(); for (File subFile : files) { compress(subFile, zos, parentPath + file.getName() + "/"); } } else { // 如果是文件,则压缩文件 FileInputStream fis = new FileInputStream(file); byte[] buffer = new byte
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

再写一行代码就下班

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值