图片下载ZIP压缩包

package com.sf.vsolution.hb.pcc.util.pictures;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.io.IOUtils;
import org.springframework.util.ResourceUtils;

import javax.swing.filechooser.FileSystemView;
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @author: yachun
 * @description: 订单模块_特殊派送
 */
public class ImgToZipUtils {
    /**
     * @param needZipPath 压缩文件夹路径
     * @param out         压缩文件输出流
     */
    public static void filetoZip(String needZipPath, OutputStream out) throws RuntimeException {
        //计算压缩所需要的时间
        long start = System.currentTimeMillis();
        ZipOutputStream zos = null;
        try {
            zos = new ZipOutputStream(out);
            //获取要压缩的文件
            File sourceFile = new File(needZipPath);
            System.out.println("sourceFile.getName() = " + sourceFile.getName());
            //开始压缩
            doFileToZip(sourceFile, zos, sourceFile.getName());
            long end = System.currentTimeMillis();
            //压缩耗费的时间
            long useTime = end - start;
            System.err.println("本次压缩,耗时" + useTime + "毫秒");
        } catch (Exception e) {
            throw new RuntimeException("压缩失败了,呵呵", e);
        } finally {
            if (zos != null) {
                try {
                    zos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 递归压缩方法
     *
     * @param sourceFile 源文件
     * @param zos        zip输出流
     * @param name       压缩后的名称
     */
    private static void doFileToZip(File sourceFile, ZipOutputStream zos, String name) throws Exception {
        byte[] buf = new byte[1024 * 2];
        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);
            }
            zos.closeEntry();
            in.close();
        } else {
            File[] listFiles = sourceFile.listFiles();
            if (listFiles == null || listFiles.length == 0) {
                // 需要保留原来的文件结构时,需要对空文件夹进行处理
                zos.putNextEntry(new ZipEntry(name + "/"));
                zos.closeEntry();// 没有文件,不需要文件的copy
            } else {
                for (File file : listFiles) {
                    // 判断是否需要保留原来的文件结构
                    // 注意:file.getName()前面需要带上父文件夹的名字加一斜杠,
                    // 不然最后压缩包中就不能保留原来的文件结构,即:所有文件都跑到压缩包根目录下了
                    doFileToZip(file, zos, name + "/" + file.getName());
                }
            }
        }
    }


    /**
     * 把一个目录打包到zip文件中的某目录
     *
     * @param dirpath     目录绝对地址
     * @param zipFilePath zip中目录
     */

    public static void packToolFiles(String dirpath, String zipFilePath) throws Exception {

        OutputStream out = null;
        BufferedOutputStream bos = null;
        ZipArchiveOutputStream zaos = null;
        File zipFile = new File(zipFilePath);
        if (!zipFile.getParentFile().exists()) {
            zipFile.getParentFile().mkdirs();
        }
        zipFile.delete();
        try {
            out = new FileOutputStream(zipFile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        bos = new BufferedOutputStream(out);
        zaos = new ZipArchiveOutputStream(bos);
        zaos.setEncoding("GBK");
        packToolFiles(zaos, dirpath, "");
        zaos.flush();
        zaos.close();
        bos.flush();
        bos.close();
        out.flush();
        out.close();
    }

    /**
     * 把一个目录打包到一个指定的zip文件中
     *
     * @param dirpath  目录绝对地址
     * @param pathName zip文件抽象地址
     */

    private static void packToolFiles(ZipArchiveOutputStream zaos, String dirpath, String pathName) throws FileNotFoundException, IOException {
        ByteArrayOutputStream tempbaos = new ByteArrayOutputStream();
        BufferedOutputStream tempbos = new BufferedOutputStream(tempbaos);
        File dir = new File(dirpath);
        // 返回此绝对路径下的文件
        File[] files = dir.listFiles();
        if (files == null || files.length < 1) {
            return;
        }
        for (int i = 0; i < files.length; i++) {
            // 判断此文件是否是一个文件夹
            if (files[i].isDirectory()) {
                packToolFiles(zaos, files[i].getAbsolutePath(), pathName
                        + files[i].getName() + File.separator);
            } else {
                zaos.putArchiveEntry(new ZipArchiveEntry(pathName
                        + files[i].getName()));
                IOUtils.copy(new FileInputStream(files[i].getAbsolutePath()),
                        zaos);
                zaos.closeArchiveEntry();
            }
        }
        tempbaos.flush();
        tempbaos.close();
        tempbos.flush();
        tempbos.close();
    }


    public static void main(String[] args) {
        FileOutputStream fos;
        File desktopDir = FileSystemView.getFileSystemView().getHomeDirectory();
        String desktopPath = desktopDir.getAbsolutePath() + "/derImg.zip";
        System.out.println("desktopPath = " + desktopPath);
        File subFile = new File("https://cbu01.alicdn.com/img/ibank/2017/041/711/4771117140_1239574879.jpg");
//        String filePath ="https://cbu01.alicdn.com/img/ibank/2017/041/711/4771117140_1239574879.jpg";
        try {
//            desktopPath ="F:/pcc/mgmt-itsc-hb-pcc-core/hb-biz-pcc-core/target/classes/static/downLoadZipPath2/";
//            fos = new FileOutputStream(new File("src/main/resources/static/uploadFile"+"/img2.zip"));
//            fos = new FileOutputStream(new File("D:/Desktop/bb/img7.zip"));

//            filetoZip("D:/Desktop/imgout", fos);


//            filetoZip("hb-biz-pcc-core/src/main/resources/static/downLoadFile2", fos);
            String needZipPathOut = "";
            try {
                File path = new File(ResourceUtils.getURL("classpath:").getPath());
                if (!path.exists()) {
                    path = new File("");
                }
                //TODO 路径downLoadZipPath-----------------
                File upload = new File(path.getAbsolutePath(), "static/downLoadZipPath2/");
                if (!upload.exists()) {
                    upload.mkdirs();
                }
                needZipPathOut = upload.getAbsolutePath() + "/testderImg.zip";
                System.out.println("needZipPathOut = " + needZipPathOut);

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

            fos = new FileOutputStream(new File(desktopPath));
            filetoZip("F:/pcc/mgmt-itsc-hb-pcc-core/hb-biz-pcc-core/target/classes/static/downLoadZipPath/", fos);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }


//        try {
//            ImgToZipUtils.packToolFiles("F:/pcc/mgmt-itsc-hb-pcc-core/hb-biz-pcc-core/target/classes/static/downLoadZipPath", "d:\\user\\01380217\\desktop\\test\\ts22t.zip");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值