Java批量压缩文件

 /***
     * 文件压缩打包
     * @param zipPath 文件夹路径
     * @param sourcePath 压缩文件夹名称
     * @param zipName 要压缩的文件名称
     * @param fileNmae 被压缩的文件名称集合
     * @return
     */
    public static boolean createCardImgZip(String zipPath, String sourcePath, String zipName, List<CreditResults> fileNmae) {
        LOG.info("压缩:start!");
        boolean result = false;
        File sourceFile = new File(sourcePath);

        FileInputStream fis = null;
        BufferedInputStream bis = null;
        FileOutputStream fos = null;
        ZipOutputStream zos = null;

        if (sourceFile.exists() == false) {
            LOG.info("压缩:File catalog:" + sourcePath + "not exist!");
            System.out.println("File catalog:" + sourcePath + "not exist!");
        } else {
            try {
                if (!new File(zipPath).exists()) {
                    new File(zipPath).mkdirs();
                }
                File zipFile = new File(zipPath + "/" + zipName + ".zip");
                if (zipFile.exists()) {
                    zipFile.delete();
                    LOG.info(zipPath + "压缩:Catalog File: " + zipName + ".zip" + "pack file.");
                    //System.out.println(zipPath + "Catalog File: " + zipName + ".zip" + "pack file.");
                } else {
                    File[] sourceFiles = sourceFile.listFiles();
                    if (null == sourceFiles || sourceFiles.length < 1) {
                        LOG.info("压缩:File Catalog:" + sourcePath + "nothing in there,don't hava to compress!");
                        System.out.println("压缩:File Catalog:" + sourcePath + "nothing in there,don't hava to compress!");
                    } else {
                        fos = new FileOutputStream(zipFile);
                        zos = new ZipOutputStream(new BufferedOutputStream(fos));
                        byte[] bufs = new byte[1024 * 10];
                        for (int i = 0; i < sourceFiles.length; i++) {
                            // create .zip and put pictures in
                            for (CreditResults item : fileNmae) {
                               System.out.println(sourceFiles[i].getName() + "---" + item.getPdfName());
                                if (sourceFiles[i].getName().equals(item.getPdfName())) {
                                    ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());
                                    zos.putNextEntry(zipEntry);
                                    // read documents and put them in the zip
                                    fis = new FileInputStream(sourceFiles[i]);
                                    bis = new BufferedInputStream(fis, 1024 * 10);
                                    int read = 0;
                                    while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) {
                                        zos.write(bufs, 0, read);
                                    }
                                }
                            }
                        }
                        LOG.info("压缩:zip successfu");
                        result = true;
                    }
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            } finally {
                try {
                    if (null != bis)
                        bis.close();
                    if (null != zos)
                        zos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }
        }
        return result;
    }

 

转载于:https://www.cnblogs.com/hanmian4511/p/10875216.html

Java调用Zip类批量压缩多个文件,此前有一个是压缩单个文件,也可参考,相关代码中可找到此源码。   public class ZipDemo extends JFrame{   JFileChooser fileChooser; //文件选择器   JList fileList; //待压缩的文件列表   Vector files; //文件数据(待压缩文件)   JButton jbAdd; //增加文件按钮   JButton jbDelete; //删除文件按钮   JButton jbZip; //压缩按钮   JTextField target; //目标文件文本域   public ZipDemo(){   super("用ZIP压缩多个文件"); //调用父类构造函数   fileChooser=new JFileChooser(); //实例化文件选择器   files=new Vector(); //实例化文件数据Vector   fileList=new JList(files); //实例化已选择文件列表   jbAdd=new JButton("增加"); //实例化按钮组件   jbDelete=new JButton("删除");   jbZip=new JButton("压缩");   target=new JTextField(18);   JPanel panel=new JPanel(); //实例化面板,用于容纳按钮   panel.add(jbAdd); //增加组件到面板上   panel.add(jbDelete);   panel.add(jbZip);   JPanel panel2=new JPanel();   panel2.add(new JLabel("目标文件"));   panel2.add(target);   JScrollPane jsp=new JScrollPane(fileList);   Container container=getContentPane(); //得到容器   container.add(panel2,BorderLayout.NORTH); //增加组件到容器   container.add(jsp,BorderLayout.CENTER);   container.add(panel,BorderLayout.SOUTH);   jsp.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); //设置边界
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值