开发者

springboot多文件压缩实现过程

目录
  • springboot多文件压缩
    • 环境依赖
    • 实现步骤
  • 总结

    springboot多文件压缩

    项目要求需要将所有附件进行压缩,记录下步骤

    环境依赖

    我使用的时jdk8,基本已经包含了所有可用的包,但是若是又特殊要求需要用到的自行导入

    实现步骤

    直接贴代码吧,我在本地自己测试用,所以文件路径都是本地路径,记得修改

    	// 多文件压缩为zip
        //多个文件,压缩成zip后下载
       public void downloadMoreFile(HttpServletResponse response) {
    
            String fileName1= "XXXXX\\Desktop\\2022.1fp\\1.xlsx";
            String fileName2= "XXXXX\\Desktop\\2022.1fp\\2.xlsx";
            // 创建文件对象
            File aFile= neMHGfuw File(fileName1);
            File bFile= new File(fileName2);
            // 构建文件集合,存放所有待压缩的文件
            List<File> files = new ArrayList<>();
            files.add(aFile);
            files.add(bFile);
            // 判断文件存在性,若不存在文件无法压缩
            if (aFile.exists() && bFile.exists()) {
                // 压缩包绝对路径
            python    String zipTmp = "XXXXXX\\Desktop\\2022.1fp\\2.zip";
                // 压缩
                zipd(zipTmp,files,response);
            }
        }
    • zipd实现内容
    /**
         * 压缩
         * @param zipTmp 压缩包绝对路径
         * @param files  待压缩文件集合
         * @param response  http请求代表响应的对象
         */
        public void zipd(String zipTmp,List<File> files,HttpServletResponse response){
            // 创建压缩包文件对象
            File zipTmpFile = new File(zipTmp);
            try {
    //            压缩包文件若已存在则删除原本的文件
                if (zipTmpFile.exists()) {
                    zipTmpFile.delete();
                }
    //            不存在新建压缩文件
                zipTmpFile.createNewFile();
    //            去除JSP编译html首部的空白行。
                response.reset();
    //            FileOutputStream字节输出流超类 文件输出流是用于将数据写入到输出流File或一个FileDescriptor
                // 创建文件输出流
                FileOutputStream fous = new FileOutputStream(zipTmpFile);
    //            ZipOutputStream压缩流
    //            此流用于以 ZIP 文件格式写入文件,包括对压缩和未压缩条目的支持,也就是把文件打包成压缩文件,常用于附件下载(多文件下载),文件压缩存储。
                ZipOutputStream zipOut = new ZipOutputStream(fous);
                // 循环处理待压缩文件
                int size = files.size();
                for (int i = 0; i < size; i++) {
                    File file = files.get(i);
                    // 将文件写入压缩流
                    zipFile(file, zipOut);
                }
                // 关闭流
                zipOut.close();
                fous.close();
    //            downloadzip(zipTmpFile, response);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    • zipFile实现内容
       /**
         *
         * @param inputFile 待压缩文件
         * @param ouputStream 压缩流
         */
        public void zipFile(File inputFile, ZipOutputStream ouputStream) {
            try {
                if (inputFile.exists()) {
                    if (inputFile.isFile()) {
                        // 待压缩文件输入流
                        FileInputStream IN = new FileInputStream(inputFile);
    //                    创建 BufferedInputStream具有指定缓冲区大小,并保存其参数,输入流 in ,供以后使用。
                        BufferedInputStream bins = new BufferedInputStream(IN, 512);
    //                    ZipEnter:表示压缩文件的条目(文件目录)
                        ZipEntry entry = new ZipEntry(inputFile.getName());
                        // 在zip流中创建一个entry文件
                        ouputStream.putNextEntry(entry);
    
                        int nNumber;
                        byte[] buffer = new byte[512];
                        while ((nNumber = bins.read(buffer)) != -1) {
                            ouputStream.write(buffer, 0, nNumber);
                        }
                        // 关闭流
                        bins.close();
                        IN.close();
                    } else {
                        /编程/ 处理文件夹的情况
                        try {
                            File[] files = inputFile.listFiles();
                            for (int i = 0; i < files.length; i++) {
                                zipFile(files[i], ouputStream);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
             php           }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    若需要下载,记得加后面的方法

    public static HttpServletResponse downloadZip(File file, HttpServletResponse response) {
            if (file.exists() == false) {
                System.out.println("待压缩的文件目录:" + file + "不存在.");
            } else {
                try {
                    // 以流的形式下载文件。
                    InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
                    byte[] buffer = new byte[fis.available()];
                    fis.read(buffer);
                    fis.close();
                    // 清空response
                    response.reset();
    
                    OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
                    response.setContentType("application/octet-stream");
    
                    // 如果输出的是中文名的文件,在此处就要用URLEncoder.encode方法进行处理
                    response.setHeader("Content-Disposition",
                            "attachment;filename=" + new String(file.getName().getBytes(python"GB2312"), "ISO8859-1"));
                    toClient.write(buffer);
                    toClient.flush();
                    toClient.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                } finally {
                    try {
                        File f = new File(file.getPath());
                        f.delete();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            return response;
        }

    总结

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程客栈(www.devze.com)。

    0

    上一篇:

    下一篇:

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    最新开发

    开发排行榜