批量zip压缩PDF文件

  1. 引用依赖;

    <!--打成zip压缩包-->
    <dependency>
        <groupId>org.apache.ant</groupId>
        <artifactId>ant</artifactId>
        <version>1.10.5</version>
    </dependency>
    
  2. zipUtils.java文件;

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Component;
    
    import java.io.*;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipOutputStream;
    
    /**
     * @author Mayz
     * @description 文件转压缩
     * @date 2021-08-23 17:26
     */
    @Component
    public class ZipUtil {
        private static final Logger log = LoggerFactory.getLogger(ZipUtil.class);
    
        public static void zip(String inputFileName, String zipFileName)
                throws Exception {
            zip(zipFileName, new File(inputFileName));
        }
    
        private static void zip(String zipFileName, File inputFile)
                throws Exception {
            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
                    zipFileName));
            zip(out, inputFile, "");
            System.out.println("zip done");
            out.close();
        }
    
        private static void zip(ZipOutputStream out, File f, String base)
                throws Exception {
            if (f.isDirectory()) {
                File[] fl = f.listFiles();
                out.putNextEntry(new ZipEntry(base + "/"));
                base = base.length() == 0 ? "" : base + "/";
                for (int i = 0; i < fl.length; i++) {
                    zip(out, fl[i], base + fl[i].getName());
                }
            } else {
                out.putNextEntry(new ZipEntry(base));
                FileInputStream in = new FileInputStream(f);
                int b;
                //System.out.println(base);
                while ((b = in.read()) != -1) {
                    out.write(b);
                }
                in.close();
            }
        }
    
        /**
         * 创建ZIP文件
         * @param sourcePath 文件或文件夹路径
         * @param zipPath 生成的zip文件存在路径(包括文件名)
         */
        public static void createZip(String sourcePath, String zipPath) {
            FileOutputStream fos = null;
            ZipOutputStream zos = null;
            try {
                fos = new FileOutputStream(zipPath);
                zos = new ZipOutputStream(fos);
                writeZip(new File(sourcePath), "", zos);
            } catch (FileNotFoundException e) {
                log.error("ZipUtils createZip  Failed to create ZIP file", e);
            } finally {
                try {
                    if (zos != null) {
                        log.debug("ZipUtils createZip Create a ZIP file successfully! the path in:{}",zipPath);
                        zos.close();
                        //压缩成功后,删除打包前的文件
                        deleteFile( new File(sourcePath) );
                    }
                } catch (IOException e) {
                    log.error("ZipUtils createZip  Failed to create ZIP file", e);
                }
            }
        }
    
        private static void writeZip(File file, String parentPath,
                                     ZipOutputStream zos) {
            if (file.exists()) {
                if (file.isDirectory()) {// 处理文件夹
                    parentPath += file.getName() + File.separator;
                    File[] files = file.listFiles();
                    for (File f : files) {
                        writeZip(f, parentPath, zos);
                    }
                } else {
                    FileInputStream fis = null;
                    try {
                        fis = new FileInputStream(file);
                        ZipEntry ze = new ZipEntry(parentPath + file.getName());
                        zos.putNextEntry(ze);
                        byte[] content = new byte[1024];
                        int len;
                        while ((len = fis.read(content)) != -1) {
                            zos.write(content, 0, len);
                            zos.flush();
                        }
                    } catch (FileNotFoundException e) {
                        log.error("ZipUtils createZip  Failed to create ZIP file",e);
                    } catch (IOException e) {
                        log.error("ZipUtils createZip  Failed to create ZIP file",e);
                    } finally {
                        try {
                            if (fis != null) {
                                fis.close();
                            }
                        } catch (IOException e) {
                            log.error("ZipUtils createZip  Failed to create ZIP file",e);
                        }
                    }
                }
            }
        }
    
        public static void copyResource(List<String> oldResPath, String newResPath) {
            for (int m = 0; m < oldResPath.size(); m++) {
                try {
                    // 如果文件夹不存在 则建立新文件夹
                    (new File(newResPath)).mkdirs();
                    File a = new File(oldResPath.get(m));
                    // 如果已经是具体文件,读取
                    if (a.isFile()) {
                        FileInputStream input = new FileInputStream(a);
                        FileOutputStream output = new FileOutputStream(newResPath + "/" + (a.getName()).toString());
                        byte[] b = new byte[1024 * 4];
                        int len;
                        while ((len = input.read(b)) != -1) {
                            output.write(b, 0, len);
                        }
                        output.flush();
                        output.close();
                        input.close();
                        // 如果文件夹下还存在文件,遍历,直到得到具体的文件
                    } else {
                        String[] file = a.list();
                        File temp = null;
                        for (int i = 0; i < file.length; i++) {
                            if (oldResPath.get(m).endsWith(File.separator)) {
                                temp = new File(oldResPath.get(m) + file[i]);
                            } else {
                                temp = new File(oldResPath.get(m) + File.separator + file[i]);
                            }
    
                            if (temp.isFile()) {
                                FileInputStream input = new FileInputStream(temp);
                                FileOutputStream output = new FileOutputStream(newResPath + "/" + (temp.getName()).toString());
                                byte[] b = new byte[1024 * 4];
                                int len;
                                while ((len = input.read(b)) != -1) {
                                    output.write(b, 0, len);
                                }
                                output.flush();
                                output.close();
                                input.close();
                            }
                            if (temp.isDirectory()) {
                                List<String> oldChildPath = new ArrayList<String>();
                                oldChildPath.add(oldResPath.get(m) + "/" + file[i]);
                                newResPath = newResPath + "/" + file[i];
                                // 如果是子文件夹 递归循环
                                copyResource(oldChildPath, newResPath);
                            }
                        }
                    }
                } catch (Exception e) {
                    log.error("copy all files failed", e);
                }
            }
        }
        /**
         * 删除文件夹
         * @param file
         */
        public static void deleteFile(File file) {
            if (file.exists()) {                               // 判断文件是否存在
                if (file.isFile()) {                           // 判断是否是文件
                    file.delete();
                } else if (file.isDirectory()) {               // 否则如果它是一个目录
                    File files[] = file.listFiles();           // 声明目录下所有的文件 files[];
                    for (int i = 0; i < files.length; i++) {   // 遍历目录下所有的文件
                        deleteFile(files[i]);                  // 把每个文件 用这个方法进行迭代
                    }
                }
                file.delete();
            }
        }
    
        /**
         * 时间格式化
         *
         * @return
         */
        public static String dateToString() {
            Date d = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
            String time = formatter.format(d);
            return time;
        }
    
    
        //main方法测试
        public static void main(String[] args) {
            List<String> oldResPath = new ArrayList<String>();
            // 文件路径
            oldResPath.add("C:\\Users\\mayz1\\Desktop\\1.pdf");
            oldResPath.add("C:\\Users\\mayz1\\Desktop\\模板.pdf");
    //        String newResPath = "C:\\Users\\mayz1\\Desktop\\" + "pdf" + dateToString(); // 生成的文件夹名
    
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String date = sdf.format(new Date());
            String newResPath = "C:\\Users\\mayz1\\Desktop\\test"+ date;
            // 压缩的文件夹名
            String zipPath = newResPath + ".zip";
            copyResource(oldResPath, newResPath);
            // 打包改目录成.zip包
            createZip(newResPath, zipPath);
        }
    }
    
  3. 运行结果展示;

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Mayz梅子子子

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

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

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

打赏作者

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

抵扣说明:

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

余额充值