java 打包压缩包下载文件

1. 下载压缩包zip方法

@Override
public void downloadZip(HttpServletResponse servletResponse) {
        String nowTime = DateUtil.DateToString(new Date(), DateStyle.YYYYMMDDHHMMSS);
        // 打包文件夹路径
        String baseTempPath = "D:/downloads/zipDownTest/";
    // 定义压缩包文件名
        String zipFileName = "申请表" +".zip";
        // 压缩
        ZipCompress compress = new ZipCompress(zipFileName, baseTempPath);
        compress.zip();
        // 获取压缩文件流
        InputStream zipStream = new FileInputStream(new File(zipFileName));
        // 下载文件名中文处理
        String downZipFileName = URLEncoder.encode(zipFileName.substring(zipFileName.lastIndexOf("/") + 1), "UTF-8");
        // 下载到浏览器
        servletResponse.setHeader("Content-disposition", "attachment; filename*=UTF-8''" + downZipFileName);
        BufferedOutputStream bufferedOs = new BufferedOutputStream(servletResponse.getOutputStream());
        byte[] buffer = new byte[10240];
        int bytesRead = 0;
        while ((bytesRead = zipStream.read(buffer)) != -1) {
            bufferedOs.write(buffer, 0, bytesRead);
        }
        bufferedOs.flush();
        bufferedOs.close();
        zipStream.close();
        // 删除临时的打包文件目录和删除下载生成的临时压缩包
        FileUtil.deleteDir(baseTempPath);
        new File(zipFileName).delete();
}

2.文件压缩工具类

package com.talebase.cloud.cas.util.zip;
import lombok.extern.log4j.Log4j2;
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
 * 文件压缩工具类
 */
@Log4j2
public class ZipCompress {

    private String zipFileName; // 目的地Zip文件
    private String sourceFileName; // 源文件(带压缩的文件或文件夹)

    public ZipCompress(String zipFileName, String sourceFileName) {
        this.zipFileName = zipFileName;
        this.sourceFileName = sourceFileName;
    }


    public void zip() throws Exception {
        //File zipFile = new File(zipFileName);
        log.info("开始压缩中...");
        long begin = System.currentTimeMillis();
        //创建zip输出流
        File zipFile = new File(zipFileName);
        if (!zipFile.exists()) {
            zipFile.createNewFile();
        }
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
        //创建缓冲输出流
        BufferedOutputStream bos = new BufferedOutputStream(out);
        File sourceFile = new File(sourceFileName);
        //调用函数
        compress(out, bos, sourceFile, sourceFile.getName());
        bos.close();
        out.close();
        long currentTimeMillis = System.currentTimeMillis();
        log.info(zipFileName + "压缩完成(100%)....." + (currentTimeMillis - begin) + "ms");
    }

    public void zipNoDirectory() throws Exception {
        //File zipFile = new File(zipFileName);
        log.info("开始压缩中...");
        long begin = System.currentTimeMillis();
        //创建zip输出流
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
        //创建缓冲输出流
        BufferedOutputStream bos = new BufferedOutputStream(out);
        File sourceFile = new File(sourceFileName);
        if (sourceFile.isDirectory()) {
            File[] flist = sourceFile.listFiles();
            for (int i = 0; i < flist.length; i++) {
                compress(out, bos, flist[i], flist[i].getName());
            }
        }
        //调用函数
//        compress(out,bos,sourceFile,sourceFile.getName());
        bos.close();
        out.close();
        long currentTimeMillis = System.currentTimeMillis();
        log.info(zipFileName + "压缩完成(100%)....." + (currentTimeMillis - begin) + "ms");
    }

    public void compress(ZipOutputStream out, BufferedOutputStream bos, File sourceFile, String base) throws Exception {
        //如果路径为目录(文件夹)
        if (sourceFile.isDirectory()) {
            //取出文件夹中的文件(或子文件夹)
            File[] flist = sourceFile.listFiles();
            if (flist.length == 0) {//如果文件夹为空,则只需在目的地zip文件中写入一个目录进入点
                //System.out.println("空:******"+base+"/");
                out.putNextEntry(new ZipEntry(base + File.separator));
            } else {//如果文件夹不为空,则递归调用compress,文件夹中的每一个文件(或文件夹)进行压缩
                for (int i = 0; i < flist.length; i++) {
                    compress(out, bos, flist[i], base + "/" + flist[i].getName());
                }
            }
        } else {//如果不是目录(文件夹),即为文件,则先写入目录进入点,之后将文件写入zip文件中
            out.putNextEntry(new ZipEntry(base));
            FileInputStream fos = new FileInputStream(sourceFile);
            BufferedInputStream bis = new BufferedInputStream(fos);
            int tag;
            // System.out.println(base);
            //将源文件写入到zip文件中
            while ((tag = bis.read()) != -1) {
                bos.write(tag);
            }
            bos.flush();
            bis.close();
            fos.close();
        }
    }
}
阅读终点,创作起航,您可以撰写心得或摘录文章要点写篇博文。去创作
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java图书管理系统文件压缩包可以理解为将图书管理系统的相关文件进行压缩打包文件格式。一般情况下,我们可以使用Java中自带的ZipOutputStream和ZipInputStream类来实现文件的压缩和解压操作。 首先,我们需要将图书管理系统的相关文件收集起来并打包压缩包。可以通过创建一个ZipOutputStream对象,指定文件输出路径以及压缩包名称,然后调用putNextEntry方法创建一个新的条目,并将相关的文件数据写入到压缩包中。这样就可以将图书管理系统的文件按需求打包压缩包中。 接下来,压缩包文件可以方便地进行传输、备份和存储。通过Java的Socket或其他网络传输相关的类,我们可以将压缩包文件传输到远程服务器或其他设备。同时,压缩包文件也可以作为系统的备份文件,以防止数据丢失或系统损坏时的恢复。此外,还可以将压缩包文件存储在云存储服务中,便于随时获取和共享。 最后,当需要使用图书管理系统时,我们可以通过解压缩方式将压缩包文件解包成原始的文件。通过创建一个ZipInputStream对象,指定压缩包文件路径,然后调用getNextEntry方法来获取各个条目,并将相应的数据写入到目标文件中即可实现解压缩操作。 综上所述,Java图书管理系统文件压缩包是通过将系统的相关文件进行压缩打包,方便传输、备份和存储。使用Java的ZipOutputStream和ZipInputStream类可以实现文件打包和解压缩操作,以满足对图书管理系统文件的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Mark_Luo123

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

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

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

打赏作者

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

抵扣说明:

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

余额充值