文件压缩工具类

Posted xiaolei2017

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了文件压缩工具类相关的知识,希望对你有一定的参考价值。

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("
压缩完成 路径:" + 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);
        }
    }
}

 

以上是关于文件压缩工具类的主要内容,如果未能解决你的问题,请参考以下文章

基于Dedup的数据打包技术

android -------- 压缩图片文件工具类

文件压缩和解压缩工具类

压缩文件工具类

文件压缩解压工具类。文件压缩格式为zip

AntZipUtils基于Ant的Zip压缩解压缩工具类