zoukankan      html  css  js  c++  java
  • 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;
        }
  • 相关阅读:
    二维树状数组(模板)
    3033太鼓达人
    2503相框
    Ant Trip(画几笔)
    [ZJOI2004]嗅探器
    [USACO06JAN]冗余路径Redundant Paths(缩点)
    P3806 【模板】点分治1
    P4149 [IOI2011]Race
    P2634 [国家集训队]聪聪可可
    P4178 Tree
  • 原文地址:https://www.cnblogs.com/hanmian4511/p/10875216.html
Copyright © 2011-2022 走看看