栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java使用apache-commons-compress对文件进行压缩(LZ4、Gzip、Snappy、Zip、Tar)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java使用apache-commons-compress对文件进行压缩(LZ4、Gzip、Snappy、Zip、Tar)

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

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

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

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

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

下面给出各个工具类,需引用 commons-compress

    org.apache.commons
    commons-compress
    1.21

CompressUtil把各压缩算法集中起来
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 {

    
    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");
    }

    
    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 {

    
    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");
    }

    
    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 {

    
    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");
    }

    
    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");
    }

    
    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() {
                @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");
    }


    
    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");
    }

    
    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() {
                @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");
    }


    
    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");
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/605754.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号