Java实现多文件打包成压缩包下载

加哥最近发现有小伙伴需要实现把客户上传的一个一个文件全部打包成压缩包再去下载,但是又不知道该怎么下手怎么开发,接下来加哥给大家分享下思路及源代码,供大家一起探讨学习。

思路

(1)将全部的文件地址先获取到(文件是图片、excel、word、pdf等都是可以的)

(2)将文件都先临时下载到本地

(3)打包成临时压缩包

(4)下载后删除压缩包

源码

import com.ruoyi.common.annotation.Log;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@Slf4j
public class FileZipUtils {
    public static void main(String[] args) throws Exception {
        String img1 = "http://114.116.197.103:9000/nankai//nankai_hospital/%E7%A7%91%E6%95%99%E8%B5%84%E6%BA%90%E5%B9%B3%E5%8F%B0%E4%BC%98%E5%8C%96%E5%8F%8A%E6%96%B0%E5%A2%9E%E9%9C%80%E6%B1%82-2023061_202309011015314_202309011326116.docx?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=linkwisdom%2F20230901%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20230901T052614Z&X-Amz-Expires=604800&X-Amz-SignedHeaders=host&X-Amz-Signature=eb1f64f54399e7288cf5aa21860e68f2c64cf60dc0e43fb12ad24312e7bdb214";
        String img2 = "http://114.116.197.103:9000/nankai//nankai_hospital%5C%5C%E6%96%B0%E7%AB%8B%E9%A1%B9%E9%A1%B9%E7%9B%AE%E7%BB%9F%E8%AE%A1%20%281%29_202309011151452.xlsx?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=linkwisdom%2F20230901%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20230901T035145Z&X-Amz-Expires=604800&X-Amz-SignedHeaders=host&X-Amz-Signature=1778c8e9020935f85f90b7f8675cb42ee8b957f342b9ea9a25a3c7759ffe6448";
        String fileStr = "D:\\image\\";


        String zipFileStr = "D:\\imageZip\\压缩包.zip";

        List<Map<String, String>> imageUrls = new ArrayList<>();
        Map<String, String> imageUrl = new HashMap<>();
        imageUrl.put("图片1.docx", img1);
        imageUrl.put("图片2.xlsx", img2);
        imageUrls.add(imageUrl);
        FileZipUtils.FileMain(imageUrls,fileStr,zipFileStr);
    }

    public static void FileMain(List<Map<String, String>> imageUrls,String fileStr,String zipFileStr)  throws Exception{
        try {
            // 创建一个文件夹
            File file = new File(fileStr);
            file.mkdirs();
            //多个图片下载地址
            for(int i=0;i<imageUrls.size();i++) {
                Map<String, String> img = imageUrls.get(i);
                for (Map.Entry<String, String> stringStringEntry : img.entrySet()) {
                    String fileName = stringStringEntry.getKey();
                    String fileUrl = stringStringEntry.getValue();
                    //new一个URL对象
                    URL url = new URL(fileUrl);
                    //打开链接
                    HttpURLConnection conn = (HttpURLConnection)url.openConnection();
                    //设置请求方式为"GET"
                    conn.setRequestMethod("GET");
                    //超时响应时间为10秒
                    conn.setConnectTimeout(10 * 1000);
                    //通过输入流获取图片数据
                    InputStream inStream = conn.getInputStream();
                    //得到图片的二进制数据,以二进制封装得到数据,具有通用性
                    byte[] data = readInputStream(inStream);
                    //new一个文件对象用来保存图片,默认保存当前工程根目录

                    File imageFile = new File(fileStr + fileName);
                    //创建输出流
                    FileOutputStream outStream = new FileOutputStream(imageFile);
                    //写入数据
                    outStream.write(data);
                    //关闭输出流
                    outStream.close();
                }

            }

            // 压缩法
            FileOutputStream fos1 = new FileOutputStream(new File(zipFileStr));
            FileZipUtils.toZip1(fileStr, fos1,false);

            // 删除文件和压缩文件
            FileZipUtils.delFolder(fileStr);
            //FileUtil.delFolder(zipFileStr);

        } catch (MalformedURLException e) {
            log.warn("图片url格式错误:", e);
        } catch (IOException e) {
            log.warn("图片没找到:", e);
        }
    }

    /**
     * 递归压缩方法
     * @param sourceFile 源文件
     * @param zos        zip输出流
     * @param name       压缩后的名称
     * @param KeepDirStructure  是否保留原来的目录结构,true:保留目录结构;
     *                          false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
     * @throws Exception
     */
    private static void compress(File sourceFile, ZipOutputStream zos, String name,
                                 boolean KeepDirStructure) throws Exception{
        byte[] buf = new byte[2 * 1024];
        if(sourceFile.isFile()){
            // 向zip输出流中添加一个zip实体,构造器中name为zip实体的文件的名字
            zos.putNextEntry(new ZipEntry(name));
            // copy文件到zip输出流中
            int len;
            FileInputStream in = new FileInputStream(sourceFile);
            while ((len = in.read(buf)) != -1){
                zos.write(buf, 0, len);
            }
            // Complete the entry
            zos.closeEntry();
            in.close();
        } else {
            File[] listFiles = sourceFile.listFiles();
            if(listFiles == null || listFiles.length == 0){
                // 需要保留原来的文件结构时,需要对空文件夹进行处理
                if(KeepDirStructure){
                    // 空文件夹的处理
                    zos.putNextEntry(new ZipEntry(name + "/"));
                    // 没有文件,不需要文件的copy
                    zos.closeEntry();
                }
            }else {
                for (File file : listFiles) {
                    // 判断是否需要保留原来的文件结构
                    if (KeepDirStructure) {
                        // 注意:file.getName()前面需要带上父文件夹的名字加一斜杠,
                        // 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了
                        compress(file, zos, name + "/" + file.getName(),KeepDirStructure);
                    } else {
                        compress(file, zos, file.getName(),KeepDirStructure);
                    }
                }
            }
        }
    }

    /**
     * 压缩成ZIP 方法1
     * @param srcDir 压缩文件夹路径
     * @param out    压缩文件输出流
     * @param KeepDirStructure  是否保留原来的目录结构,true:保留目录结构;
     *                          false:所有文件跑到压缩包根目录下(注意:不保留目录结构可能会出现同名文件,会压缩失败)
     * @throws RuntimeException 压缩失败会抛出运行时异常
     */
    public static void toZip1(String srcDir, OutputStream out, boolean KeepDirStructure)
            throws RuntimeException{
        long start = System.currentTimeMillis();
        ZipOutputStream zos = null ;
        try {
            zos = new ZipOutputStream(out);
            File sourceFile = new File(srcDir);
            compress(sourceFile,zos,sourceFile.getName(),KeepDirStructure);
            long end = System.currentTimeMillis();
            System.out.println("压缩完成,耗时:" + (end - start) +" ms");
        } catch (Exception e) {
            throw new RuntimeException("zip error from ZipUtils",e);
        }finally{
            if(zos != null){
                try {
                    zos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 压缩成ZIP 方法2
     * @param srcFiles 需要压缩的文件列表
     * @param out           压缩文件输出流
     * @throws RuntimeException 压缩失败会抛出运行时异常
     */
    public static void toZip2(List<File> srcFiles , OutputStream out)throws RuntimeException {
        long start = System.currentTimeMillis();
        ZipOutputStream zos = null ;
        try {
            zos = new ZipOutputStream(out);
            for (File srcFile : srcFiles) {
                byte[] buf = new byte[2 * 1024];
                zos.putNextEntry(new ZipEntry(srcFile.getName()));
                int len;
                FileInputStream in = new FileInputStream(srcFile);
                while ((len = in.read(buf)) != -1){
                    zos.write(buf, 0, len);
                }
                zos.closeEntry();
                in.close();
            }
            long end = System.currentTimeMillis();
            System.out.println("压缩完成,耗时:" + (end - start) +" ms");
        } catch (Exception e) {
            throw new RuntimeException("zip error from ZipUtils",e);
        }finally{
            if(zos != null){
                try {
                    zos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 得到图片的二进制数据,以二进制封装得到数据,具有通用性
     * @param inStream
     * @return
     * @throws Exception
     */
    public static byte[] readInputStream(InputStream inStream) throws Exception{
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        //创建一个Buffer字符串
        byte[] buffer = new byte[1024];
        //每次读取的字符串长度,如果为-1,代表全部读取完毕
        int len = 0;
        //使用一个输入流从buffer里把数据读取出来
        while( (len=inStream.read(buffer)) != -1 ){
            //用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
            outStream.write(buffer, 0, len);
        }
        //关闭输入流
        inStream.close();
        //把outStream里的数据写入内存
        return outStream.toByteArray();
    }

    /**
     * 删除文件夹
     * @param folderPath 文件夹完整绝对路径
     * @return
     */
    public static void delFolder(String folderPath) {
        try {
            delAllFile(folderPath); //删除完里面所有内容
            String filePath = folderPath;
            filePath = filePath.toString();
            File myFilePath = new File(filePath);
            myFilePath.delete(); //删除空文件夹
        }
        catch (Exception e) {
        }
    }


    /**
     * 删除指定文件夹下所有文件
     * @param path 文件夹完整绝对路径
     * @return
     * @return
     */
    public static boolean delAllFile(String path) {
        boolean bea = false;
        File file = new File(path);
        if (!file.exists()) {
            return bea;
        }
        if (!file.isDirectory()) {
            return bea;
        }
        String[] tempList = file.list();
        File temp = null;
        for (int i = 0; i < tempList.length; i++) {
            if (path.endsWith(File.separator)) {
                temp = new File(path + tempList[i]);
            }else{
                temp = new File(path + File.separator + tempList[i]);
            }
            if (temp.isFile()) {
                temp.delete();
            }
            if (temp.isDirectory()) {
                delAllFile(path+"/"+ tempList[i]);//先删除文件夹里面的文件
                delFolder(path+"/"+ tempList[i]);//再删除空文件夹
                bea = true;
            }
        }
        return bea;
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

加瓦程序设计师

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

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

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

打赏作者

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

抵扣说明:

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

余额充值