黎明前的黑暗
一切不行都是人不行
 /***
     * 文件压缩打包
     * @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;
    }

 

posted on 2019-05-16 13:52  无厘头的小情绪  阅读(375)  评论(0编辑  收藏  举报