Java压缩目录成zip文件

目录打包供用户下载,zip是Java原生支持的。

代码


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipDirectory {
    public static void main(String[] args) throws IOException {
        //待压缩的目录
        String sourceFile = "/Users/itkey/IdeaProjects/ocefi/src/main/resources/static/efi";
        //压缩以后得到的efi文件路径efi.zip
        FileOutputStream fos = new FileOutputStream("/Users/itkey/IdeaProjects/ocefi/src/main/resources/static/efi.zip");
        ZipOutputStream zipOut = new ZipOutputStream(fos);
        File fileToZip = new File(sourceFile);

        zipFile(fileToZip, fileToZip.getName(), zipOut);
        zipOut.close();
        fos.close();
    }

    private static void zipFile(File fileToZip, String fileName, ZipOutputStream zipOut) throws IOException {
        if (fileToZip.isHidden()) {
            return;
        }
        if (fileToZip.isDirectory()) {
            if (fileName.endsWith("/")) {
                zipOut.putNextEntry(new ZipEntry(fileName));
                zipOut.closeEntry();
            } else {
                zipOut.putNextEntry(new ZipEntry(fileName + "/"));
                zipOut.closeEntry();
            }
            File[] children = fileToZip.listFiles();
            for (File childFile : children) {
                zipFile(childFile, fileName + "/" + childFile.getName(), zipOut);
            }
            return;
        }
        FileInputStream fis = new FileInputStream(fileToZip);
        ZipEntry zipEntry = new ZipEntry(fileName);
        zipOut.putNextEntry(zipEntry);
        byte[] bytes = new byte[1024];
        int length;
        while ((length = fis.read(bytes)) >= 0) {
            zipOut.write(bytes, 0, length);
        }
        fis.close();
    }
}

参考文档:

《Zipping and Unzipping in Java》
https://www.baeldung.com/java-compress-and-uncompress

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中可以使用java.util.zip包下的相关类来实现将文件压缩成zip文件的功能。以下是一个简单的示例代码: ```java import java.io.*; import java.util.zip.*; public class ZipUtils { public static void compressFileToZip(String sourceFilePath, String zipFilePath) { try { // 创建压缩文件输出流 FileOutputStream fos = new FileOutputStream(zipFilePath); ZipOutputStream zos = new ZipOutputStream(fos); // 创建待压缩文件输入流 FileInputStream fis = new FileInputStream(sourceFilePath); BufferedInputStream bis = new BufferedInputStream(fis); // 往压缩包中写入文件 zos.putNextEntry(new ZipEntry(sourceFilePath)); byte[] buffer = new byte[1024]; int len; while ((len = bis.read(buffer)) > 0) { zos.write(buffer, 0, len); } // 关闭输入输出流 bis.close(); fis.close(); zos.closeEntry(); zos.close(); System.out.println("文件压缩成功!"); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { String sourceFilePath = "source.txt"; // 待压缩文件路径 String zipFilePath = "compressed.zip"; // 压缩后的zip文件路径 compressFileToZip(sourceFilePath, zipFilePath); } } ``` 以上代码中,我们首先创建一个压缩文件输出流(`ZipOutputStream`),并指定输出文件路径。然后创建一个待压缩文件输入流(`FileInputStream`)和一个缓冲输入流(`BufferedInputStream`)。接下来,我们将待压缩文件写入压缩包(`zip` 文件)中,并使用缓冲区逐块写入。最后,关闭输入输出流,完文件压缩操作。 注意:上述代码只能压缩一个文件,如果要压缩多个文件,需要在循环中依次压缩每一个文件。另外,压缩后,可以通过解压缩操作还原被压缩文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值