码迷,mamicode.com
首页 > 其他好文 > 详细

文件压缩工具类

时间:2018-09-14 11:52:58      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:tput   package   color   存在   return   time   ati   stat   param   

package com.csf.myproduct.common;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * Createy by user on 9/14/2018.10:48
 */
public class ZipCompressorUtils {

    private static final int BUFFER = 8192;

    public static void main(String[] args) {
        String namePath = "E:/压缩test/sys.zip";
        List<String> list = new ArrayList<String>();
        list.add("E:/压缩test/file/log4j.properties");
        list.add("E:/压缩test/file/pom引入jar包.txt");
        list.add("E:/压缩test/file/PUSH网关常见问题排查手册.doc");
        list.add("E:/压缩test/file/交易账户信息2018-08-15.xls");
//        list.add("E:/压缩test/file/推送代码.zip");
        compress(namePath, list.toArray(new String[list.size()]));
        System.out.println("\n压缩完成 路径:" + namePath);
    }

    /**
     * 压缩入口
     *
     * @param compPathname 压缩输出路径
     * @param pathName     需要进行压缩的文件列表
     */
    public static void compress(String compPathname, String... pathName) {
        File zipFile = new File(compPathname);
        ZipOutputStream out = null;
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
            CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream, new CRC32());
            out = new ZipOutputStream(cos);
            String basedir = "";
            for (int i = 0; i < pathName.length; i++) {
                compress(new File(pathName[i]), out, basedir);
            }
            out.close();
        } catch (Exception e) {
            System.out.println("压缩异常============");
            throw new RuntimeException(e);
        }
    }

    public static void compress(String srcPathName, String compPathname) throws IOException {
        File zipFile = new File(compPathname);
        File file = new File(srcPathName);
        if (!file.exists())
            throw new RuntimeException(srcPathName + "不存在!");
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
            CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream, new CRC32());
            ZipOutputStream out = new ZipOutputStream(cos);
            String basedir = "";

            File[] files = file.listFiles();
            for (File file2 : files) {
                compress(file2, out, basedir);
            }

            out.close();
        } catch (Exception e) {
            System.out.println("压缩异常============");
            throw new RuntimeException(e);
        }
    }

    /**
     * @param file    需要压缩的文件
     * @param out     压缩工具
     * @param basedir
     */
    private static void compress(File file, ZipOutputStream out, String basedir) {

        /* 判断是目录还是文件 */
        if (file.isDirectory()) {
            System.out.println("压缩:" + basedir + file.getName());
            compressDirectory(file, out, basedir);
        } else {
            System.out.println("压缩:" + basedir + file.getName());
            compressFile(file, out, basedir);
        }
    }

    /**
     * 压缩一个目录
     *
     * @param dir     需要压缩的文件
     * @param out     压缩工具
     * @param basedir
     */
    private static void compressDirectory(File dir, ZipOutputStream out, String basedir) {
        if (!dir.exists())
            return;

        try {
//            if (!dir.getName().startsWith("2017")) {
//                basedir += dir.getName() + "/";
//                out.putNextEntry(new ZipEntry(basedir));
//            }
            basedir += dir.getName() + "/";
            out.putNextEntry(new ZipEntry(basedir));

            File[] files = dir.listFiles();
            for (int i = 0; i < files.length; i++) {
                /* 递归 */
                compress(files[i], out, basedir);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 压缩一个文件
     *
     * @param file    需要压缩的文件
     * @param out     压缩工具
     * @param basedir
     */
    private static void compressFile(File file, ZipOutputStream out, String basedir) {
        if (!file.exists()) {
            System.out.println("文件不存在");
            return;
        }
        try {
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
            ZipEntry entry = new ZipEntry(basedir + file.getName());
            out.putNextEntry(entry);
            int count;
            byte data[] = new byte[BUFFER];
            while ((count = bis.read(data, 0, BUFFER)) != -1) {
                out.write(data, 0, count);
            }
            bis.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

 

文件压缩工具类

标签:tput   package   color   存在   return   time   ati   stat   param   

原文地址:https://www.cnblogs.com/xiaolei2017/p/9645369.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!