因项目中的文件很大,非常占用磁盘空间,每次磁盘空间不足时客户都要我们手动删除文件,因此想对文件进行压缩,选用apache提供的commons-compress帮助类进行压缩

封装了Gzip、LZ4、Snappy、Zip、Tar几个工具类

请根据业务的不同,选着合适的压缩算法

先到生产环境验证各个压缩算法的效率(时间、空间),我们对压缩时间要求快,所以后面会选择一款快速压缩算法来压缩文件

commons-compress官方参考示例:
https://commons.apache.org/proper/commons-compress/examples.html

在这里插入图片描述

下面给出各个工具类,需引用 commons-compress
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-compress</artifactId>
    <version>1.21</version>
</dependency>
CompressUtil把各压缩算法集中起来
/**
 * 公用压缩工具类,支持Gzip、Zip、Tar、LZ4、Snappy压缩算法
 * @description:
 * @author: zhuyu
 * @date: 2021/11/27 21:40
 */
public class CompressUtil {

    public static class Gzip {
        public static void compress(String sourceFile , String targetFile){
            GzipUtil.compress(sourceFile , targetFile);
        }
        public static void uncompress(String sourceFile , String targetFile){
            GzipUtil.uncompress(sourceFile , targetFile);
        }
    }

    public static class LZ4 {
        public static void compress(String sourceFile , String targetFile){
            LZ4Util.compress(sourceFile , targetFile);
        }
        public static void uncompress(String sourceFile , String targetFile){
            LZ4Util.uncompress(sourceFile , targetFile);
        }
    }

    public static class Snappy {
        public static void compress(String sourceFile , String targetFile){
            SnappyUtil.compress(sourceFile , targetFile);
        }
        public static void uncompress(String sourceFile , String targetFile){
            SnappyUtil.uncompress(sourceFile , targetFile);
        }
    }

    public static class Zip {
        public static void compress(String sourceFile, String targetFile){
            ZipUtil.compress(sourceFile , targetFile);
        }
        public static void uncompress(String zipPath, String descDir) {
            ZipUtil.uncompress(zipPath , descDir);
        }
    }

    public static class Tar {
        public static void compress(String sourceFile, String targetFile) {
            TarUtil.compress(sourceFile , targetFile);
        }
        public static void uncompress(String tarPath, String descDir) {
            TarUtil.uncompress(tarPath , descDir);
        }
    }
}
1.GzipUtil
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;

public class GzipUtil {

    /**
     * 将文件压缩成gzip
     * @param sourceFile 源文件,如:archive.tar
     * @param targetFile 目标文件,如:archive.tar.gz
     */
    public static void compress(String sourceFile , String targetFile) {
        long d1 = System.currentTimeMillis();
        try (InputStream in = Files.newInputStream(Paths.get(sourceFile));
             OutputStream fout = Files.newOutputStream(Paths.get(targetFile));
             BufferedOutputStream out = new BufferedOutputStream(fout);
             GzipCompressorOutputStream gzOut = new GzipCompressorOutputStream(out);){
            int buffersize = 10240;
            final byte[] buffer = new byte[buffersize];
            int n = 0;
            while (-1 != (n = in.read(buffer))) {
                gzOut.write(buffer, 0, n);
            }
        } catch (IOException e) {
            System.out.println("压缩失败,原因:" + e.getMessage());
        }
        System.out.println("压缩完毕,耗时:" + (System.currentTimeMillis() - d1) + " ms");
    }

    /**
     * 将gzip文件解压
     * @param sourceFile 源文件,如:archive.tar.gz
     * @param targetFile 目标文件,如:archive.tar
     */
    public static void uncompress(String sourceFile , String targetFile) {
        long d1 = System.currentTimeMillis();
        try (InputStream fin = Files.newInputStream(Paths.get(sourceFile));
             BufferedInputStream in = new BufferedInputStream(fin);
             OutputStream out = Files.newOutputStream(Paths.get(targetFile));
             GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);){
            int buffersize = 10240;
            final byte[] buffer = new byte[buffersize];
            int n = 0;
            while (-1 != (n = gzIn.read(buffer))) {
                out.write(buffer, 0, n);
            }
        } catch (IOException e) {
            System.out.println("解压失败,原因:" + e.getMessage());
        }
        System.out.println("解压完毕,耗时:" + (System.currentTimeMillis() - d1) + " ms");
    }
}
2.LZ4Util
import org.apache.commons.compress.compressors.lz4.FramedLZ4CompressorInputStream;
import org.apache.commons.compress.compressors.lz4.FramedLZ4CompressorOutputStream;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;

public class LZ4Util {

    /**
     * 将文件压缩成LZ4文件
     *
     * @param sourceFile 源文件,如:archive.tar
     * @param targetFile 目标文件,如:archive.tar.lz4
     */
    public static void compress(String sourceFile, String targetFile) {
        long d1 = System.currentTimeMillis();
        try (InputStream in = Files.newInputStream(Paths.get(sourceFile));
             OutputStream fout = Files.newOutputStream(Paths.get(targetFile));
             BufferedOutputStream out = new BufferedOutputStream(fout);
             FramedLZ4CompressorOutputStream lzOut = new FramedLZ4CompressorOutputStream(out);){
            int buffersize = 10240;
            final byte[] buffer = new byte[buffersize];
            int n = 0;
            while (-1 != (n = in.read(buffer))) {
                lzOut.write(buffer, 0, n);
            }
        } catch (IOException e) {
            System.out.println("压缩失败,原因:" + e.getMessage());
        }
        System.out.println("压缩完毕,耗时:" + (System.currentTimeMillis() - d1) + " ms");
    }

    /**
     * 将LZ4文件进行解压
     * @param sourceFile 源文件,如:archive.tar.lz4
     * @param targetFile 目标文件,如:archive.tar
     */
    public static void uncompress(String sourceFile, String targetFile) {
        long d1 = System.currentTimeMillis();
        try (InputStream fin = Files.newInputStream(Paths.get(sourceFile));
             BufferedInputStream in = new BufferedInputStream(fin);
             OutputStream out = Files.newOutputStream(Paths.get(targetFile));
             FramedLZ4CompressorInputStream zIn = new FramedLZ4CompressorInputStream(in);){
            int buffersize = 10240;
            final byte[] buffer = new byte[buffersize];
            int n = 0;
            while (-1 != (n = zIn.read(buffer))) {
                out.write(buffer, 0, n);
            }
        } catch (IOException e) {
            System.out.println("解压失败,原因:" + e.getMessage());
        }
        System.out.println("解压完毕,耗时:" + (System.currentTimeMillis() - d1) + " ms");
    }
}
3.SnappyUtil
import org.apache.commons.compress.compressors.snappy.FramedSnappyCompressorInputStream;
import org.apache.commons.compress.compressors.snappy.FramedSnappyCompressorOutputStream;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;

public class SnappyUtil {

    /**
     * 将文件压缩成gzip
     * @param sourceFile 源文件,如:archive.tar
     * @param targetFile 目标文件,如:archive.tar.sz
     */
    public static void compress(String sourceFile , String targetFile) {
        long d1 = System.currentTimeMillis();
        try (InputStream in = Files.newInputStream(Paths.get(sourceFile));
             OutputStream fout = Files.newOutputStream(Paths.get(targetFile));
             BufferedOutputStream out = new BufferedOutputStream(fout);
             FramedSnappyCompressorOutputStream snOut = new FramedSnappyCompressorOutputStream(out);){
            int buffersize = 10240;
            final byte[] buffer = new byte[buffersize];
            int n = 0;
            while (-1 != (n = in.read(buffer))) {
                snOut.write(buffer, 0, n);
            }
        } catch (IOException e) {
            System.out.println("压缩失败,原因:" + e.getMessage());
        }
        System.out.println("压缩完毕,耗时:" + (System.currentTimeMillis() - d1) + " ms");
    }

    /**
     * 将gzip文件解压
     * @param sourceFile 源文件,如:archive.tar.sz
     * @param targetFile 目标文件,如:archive.tar
     */
    public static void uncompress(String sourceFile , String targetFile) {
        long d1 = System.currentTimeMillis();
        try (InputStream fin = Files.newInputStream(Paths.get(sourceFile));
             BufferedInputStream in = new BufferedInputStream(fin);
             OutputStream out = Files.newOutputStream(Paths.get(targetFile));
             FramedSnappyCompressorInputStream zIn = new FramedSnappyCompressorInputStream(in);){
            int buffersize = 10240;
            final byte[] buffer = new byte[buffersize];
            int n = 0;
            while (-1 != (n = zIn.read(buffer))) {
                out.write(buffer, 0, n);
            }
        } catch (IOException e) {
            System.out.println("解压失败,原因:" + e.getMessage());
        }
        System.out.println("解压完毕,耗时:" + (System.currentTimeMillis() - d1) + " ms");
    }
}
4.ZipUtil
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.ArchiveOutputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils;

import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Objects;

public class ZipUtil {

    public static void main(String[] args) {
        String rootpath = "C:\\Temp\\chdlTemp\\testcompress";
        String sourceFile = rootpath + "\\springboot项目jar瘦身.docx";
        String targetFile = rootpath + "\\springboot项目jar瘦身.docx.zip";

        //压缩文件
        compress(sourceFile , targetFile);
        //解压文件
        uncompress(targetFile, rootpath + "\\springboot项目jar瘦身2.docx");

        //压缩目录
        //compress(rootpath , rootpath + ".zip");
        //解压目录
        //uncompress(rootpath + ".zip" , rootpath + "2");
    }

    /**
     * 将文件压缩成zip
     *
     * @param sourceFile 源文件或目录,如:archive.tar
     * @param targetFile 目标文件,如:archive.tar.zip
     */
    public static void compress(String sourceFile, String targetFile) {
        long d1 = System.currentTimeMillis();
        try (OutputStream fos = new FileOutputStream(targetFile);
             OutputStream bos = new BufferedOutputStream(fos);
             ArchiveOutputStream aos = new ZipArchiveOutputStream(bos);){
            Path dirPath = Paths.get(sourceFile);
            Files.walkFileTree(dirPath, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                    ArchiveEntry entry = new ZipArchiveEntry(dir.toFile(), dirPath.relativize(dir).toString());
                    aos.putArchiveEntry(entry);
                    aos.closeArchiveEntry();
                    return super.preVisitDirectory(dir, attrs);
                }

                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    ArchiveEntry entry = new ZipArchiveEntry(
                            file.toFile(), dirPath.relativize(file).toString());
                    aos.putArchiveEntry(entry);
                    IOUtils.copy(new FileInputStream(file.toFile()), aos);
                    aos.closeArchiveEntry();
                    return super.visitFile(file, attrs);
                }
            });
        } catch (IOException e) {
            System.out.println("压缩失败,原因:" + e.getMessage());
        }
        System.out.println("压缩完毕,耗时:" + (System.currentTimeMillis() - d1) + " ms");
    }


    /**
     * 将zip文件解压到指定目录
     *
     * @param zipPath 源文件,如:archive.zip
     * @param descDir 解压目录
     */
    public static void uncompress(String zipPath, String descDir) {
        long d1 = System.currentTimeMillis();
        try (InputStream fis = Files.newInputStream(Paths.get(zipPath));
             InputStream bis = new BufferedInputStream(fis);
             ArchiveInputStream ais = new ZipArchiveInputStream(bis);
             ){
            ArchiveEntry entry;
            while (Objects.nonNull(entry = ais.getNextEntry())) {
                if (!ais.canReadEntryData(entry)) {
                    continue;
                }
                String name = descDir + File.separator + entry.getName();
                File f = new File(name);
                if (entry.isDirectory()) {
                    if (!f.isDirectory() && !f.mkdirs()) {
                        f.mkdirs();
                    }
                } else {
                    File parent = f.getParentFile();
                    if (!parent.isDirectory() && !parent.mkdirs()) {
                        throw new IOException("failed to create directory " + parent);
                    }
                    try (OutputStream o = Files.newOutputStream(f.toPath())) {
                        IOUtils.copy(ais, o);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("解压失败,原因:" + e.getMessage());
        }
        System.out.println("解压完毕,耗时:" + (System.currentTimeMillis() - d1) + " ms");
    }
}
5.TarUtil
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.ArchiveOutputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils;

import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Objects;

public class TarUtil {

    public static void main(String[] args) {
        String rootpath = "C:\\Temp\\chdlTemp\\testcompress";
        String sourceFile = rootpath + "\\springboot项目jar瘦身.docx";
        String targetFile = rootpath + "\\springboot项目jar瘦身.docx.tar";

        //压缩文件
        compress(sourceFile , targetFile);
        //解压文件
        uncompress(targetFile, rootpath + "\\springboot项目jar瘦身2.docx");
    }

    /**
     * 将文件压缩成tar
     *
     * @param sourceFile 源文件或目录,如:archive
     * @param targetFile 目标文件,如:archive.tar
     */
    public static void compress(String sourceFile, String targetFile) {
        long d1 = System.currentTimeMillis();
        try (OutputStream fos = new FileOutputStream(targetFile);
             OutputStream bos = new BufferedOutputStream(fos);
             ArchiveOutputStream aos = new TarArchiveOutputStream(bos);){
            Path dirPath = Paths.get(sourceFile);
            Files.walkFileTree(dirPath, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                    ArchiveEntry entry = new TarArchiveEntry(dir.toFile(), dirPath.relativize(dir).toString());
                    aos.putArchiveEntry(entry);
                    aos.closeArchiveEntry();
                    return super.preVisitDirectory(dir, attrs);
                }

                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    ArchiveEntry entry = new TarArchiveEntry(
                            file.toFile(), dirPath.relativize(file).toString());
                    aos.putArchiveEntry(entry);
                    IOUtils.copy(new FileInputStream(file.toFile()), aos);
                    aos.closeArchiveEntry();
                    return super.visitFile(file, attrs);
                }
            });
        } catch (IOException e) {
            System.out.println("压缩失败,原因:" + e.getMessage());
        }
        System.out.println("压缩完毕,耗时:" + (System.currentTimeMillis() - d1) + " ms");
    }


    /**
     * 将tar文件解压到指定目录
     *
     * @param tarPath 源文件,如:archive.tar
     * @param descDir 解压目录
     */
    public static void uncompress(String tarPath, String descDir) {
        long d1 = System.currentTimeMillis();
        try (InputStream fis = Files.newInputStream(Paths.get(tarPath));
             InputStream bis = new BufferedInputStream(fis);
             ArchiveInputStream ais = new TarArchiveInputStream(bis);
        ){
            ArchiveEntry entry;
            while (Objects.nonNull(entry = ais.getNextEntry())) {
                if (!ais.canReadEntryData(entry)) {
                    continue;
                }
                String name = descDir + File.separator + entry.getName();
                File f = new File(name);
                if (entry.isDirectory()) {
                    if (!f.isDirectory() && !f.mkdirs()) {
                        f.mkdirs();
                    }
                } else {
                    File parent = f.getParentFile();
                    if (!parent.isDirectory() && !parent.mkdirs()) {
                        throw new IOException("failed to create directory " + parent);
                    }
                    try (OutputStream o = Files.newOutputStream(f.toPath())) {
                        IOUtils.copy(ais, o);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("解压失败,原因:" + e.getMessage());
        }
        System.out.println("解压完毕,耗时:" + (System.currentTimeMillis() - d1) + " ms");
    }
}
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐