Java如何将文件打包成Zip、Rar压缩包

1.将单个文件打成压缩包

需求:
将E盘下的 test.xls 文件打成压缩包保存到E盘目录下的 target.zip 文件中

代码实现:ZipUtil.java

import org.apache.commons.io.IOUtils;

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

/**
 * <p> @Title ZipUtil
 * <p> @Description 将单个文件打成压缩包
 *
 * @author ACGkaka
 * @date 2020/9/15 14:11
 */
public class ZipUtil {

    public static void main(String[] args) throws Exception {
    String dir = "E:\\test.xls";
    String zip = "E:\\target.zip";
    zip(dir, zip);
}

    /**
     * 打包
     *
     * @param dir            要打包的目录
     * @param zipFile        打包后的文件路径
     * @throws Exception
     */
    public static void zip(String dir, String zipFile) throws Exception {
        try (ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile))) {
            File sourceFile = new File(dir);
            out.putNextEntry(new ZipEntry(sourceFile.getName()));
            try (FileInputStream in = new FileInputStream(sourceFile)) {
                IOUtils.copy(in, out);
            } catch (Exception e) {
                throw new RuntimeException("打包异常: " + e.getMessage());
            }
        }
    }
}

2.将E:\zip目录打成压缩包

需求:
将E盘zip文件夹下面的内容打成压缩包保存到E盘目录下的 target.zip 文件中

代码实现:ZipUtil.java

import java.io.FileOutputStream;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.ArrayUtils;

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

/**
 * <p> @Title ZipUtil
 * <p> @Description 文件夹打压缩包
 *
 * @author ACGkaka
 * @date 2020/9/15 13:48
 */

public class ZipUtil {

    public static void main(String[] args) throws Exception {
        String dir = "E:\\zip";
        String zip = "E:\\target.zip";
        String rar = "E:\\target.rar";
        zip(dir, zip);
        zip(dir, rar, true);
    }


    /**
     * 打包
     *
     * @param dir     要打包的目录
     * @param zipFile 打包后的文件路径
     * @throws Exception
     */
    public static void zip(String dir, String zipFile) throws Exception {
        zip(dir, zipFile, false);
    }

    /**
     * 打包
     *
     * @param dir            要打包的目录
     * @param zipFile        打包后的文件路径
     * @param includeBaseDir 是否包括最外层目录
     * @throws Exception
     */
    public static void zip(String dir, String zipFile, boolean includeBaseDir) throws Exception {
        if (zipFile.startsWith(dir)) {
            throw new RuntimeException("打包生成的文件不能在打包目录中");
        }
        try (ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile))) {
            File fileDir = new File(dir);
            String baseDir = "";
            if (includeBaseDir) {
                baseDir = fileDir.getName();
            }
            compress(out, fileDir, baseDir);
        }
    }

    public static void compress(ZipOutputStream out, File sourceFile, String base) throws Exception {

        if (sourceFile.isDirectory()) {
            base = base.length() == 0 ? "" : base + File.separator;
            File[] files = sourceFile.listFiles();
            if (ArrayUtils.isEmpty(files)) {
                // todo 打包空目录
                // out.putNextEntry(new ZipEntry(base));
                return;
            }
            for (File file : files) {
                compress(out, file, base + file.getName());
            }
        } else {
            out.putNextEntry(new ZipEntry(base));
            try (FileInputStream in = new FileInputStream(sourceFile)) {
                IOUtils.copy(in, out);
            } catch (Exception e) {
                throw new RuntimeException("打包异常: " + e.getMessage());
            }
        }
    }
}






参考博客:https://blog.csdn.net/frankcheng5143/article/details/105129108

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java中,可以使用Java压缩库来将文件打包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 ZipFileSender { public static void main(String[] args) { String filePath = "path_to_file"; String zipFilePath = "path_to_zip_file"; File fileToZip = new File(filePath); try { FileOutputStream fos = new FileOutputStream(zipFilePath); ZipOutputStream zos = new ZipOutputStream(fos); addToZip(fileToZip, fileToZip.getName(), zos); zos.close(); fos.close(); } catch (IOException e) { e.printStackTrace(); } } private static void addToZip(File file, String fileName, ZipOutputStream zos) throws IOException { FileInputStream fis = new FileInputStream(file); ZipEntry zipEntry = new ZipEntry(fileName); zos.putNextEntry(zipEntry); byte[] buffer = new byte[1024]; int length; while ((length = fis.read(buffer)) > 0) { zos.write(buffer, 0, length); } zos.closeEntry(); fis.close(); } } ``` 在上面的示例中,我们创建了一个`ZipFileSender`类,它将提供的文件(`filePath`)打包为一个zip文件(`zipFilePath`)。我们使用`java.util.zip.ZipOutputStream`类来创建zip文件并添加文件zip文件中。`addToZip`方法用于将文件添加到zip文件中。最后,我们使用`FileOutputStream`类将zip文件发送给前端。运行以上代码,将会在指定路径生名为`zipFilePath`的zip文件,你可以将其发送给前端。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

不愿放下技术的小赵

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

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

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

打赏作者

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

抵扣说明:

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

余额充值