美文网首页
java使用commons-compress压缩解压缩bz2文件

java使用commons-compress压缩解压缩bz2文件

作者: haiyong6 | 来源:发表于2023-03-04 10:12 被阅读0次

    以前写过一篇:java利用commons-compress压缩解压缩targz文件,这次记录一下bz2的压缩解压缩。

    linux命令压缩解压缩 bzip2命令

    [root@localhost ~]# bzip2 [选项] 源文件
    

    常用参数:
    -d 执行解压缩,此时该选项后的源文件应为标记有 .bz2 后缀的压缩包文件。
    -k bzip2 在压缩或解压缩任务完成后,会删除原始文件,若要保留原始文件,可使用此选项。
    -f bzip2 在压缩或解压缩时,若输出文件与现有文件同名,默认不会覆盖现有文件,若使用此选项,则会强制覆盖现有文件。
    -t 测试压缩包文件的完整性。
    -v 压缩或解压缩文件时,显示详细信息。
    -数字 这个参数和 gzip 命令的作用一样,用于指定压缩等级,-1 压缩等级最低,压缩比最差;-9 压缩比最高

    例子:

    //压缩 把cms.txt压缩成cms.txt.bz2并保留源文件
    zhaohy@LAPTOP-34CQ982I MINGW64 /d/bz2-test
    $ bzip2 -k cms.txt
    //解压缩 把cms.txt.bz2解压成cms.txt并保留源文件
    zhaohy@LAPTOP-34CQ982I MINGW64 /d/bz2-test
    $ bzip2 -dk cms.txt.bz2
    

    java实现

    maven引入(截止发文,最新版本是1.22):

    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-compress -->
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-compress</artifactId>
                <version>1.22</version>
            </dependency>
    

    在commons-compress的api文档里是有api支持的:https://commons.apache.org/proper/commons-compress/apidocs/index.html
    主要使用了BZip2CompressorInputStream(压缩)BZip2CompressorOutputStream(解压缩)BZip2Utils(获取压缩后的文件名或获取解压后的文件名工具类)这三个类。

    上代码:

    package com.zhaohy.app.utils;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
    import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
    import org.apache.commons.compress.compressors.bzip2.BZip2Utils;
    
    public class Bz2Util {
        
        /**
         * 压缩生成bz2
         * @param file 源文件
         * @param targetPath 压缩之后生成bz2地址
         * @param delete 压缩后是否删除源文件
         */
        public static void compressToBz2(File file, String targetPath, boolean delete) {
            FileInputStream fis = null;
            FileOutputStream fos = null;
            BZip2CompressorOutputStream bz2Fos = null;
            try {
                fis = new FileInputStream(file);
                fos = new FileOutputStream(new File(targetPath));
                bz2Fos = new BZip2CompressorOutputStream(fos);
                int count;
                byte data[] = new byte[2048];
                while ((count = fis.read(data)) != -1) {
                    bz2Fos.write(data, 0, count);
                }
                bz2Fos.flush();
                if(delete && file.exists()) {
                    file.delete();
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if(null != bz2Fos) bz2Fos.close();
                    if(null != fos) fos.close();
                    if(null != fis) fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        
        /**
         * 解压缩bz2
         * @param file 压缩文件
         * @param targetPath 目标path
         * @param delete 解压后是否删除源文件
         */
        public static void uncompressFromBz2(File file, String targetPath, boolean delete) {
            FileInputStream fis = null;
            BZip2CompressorInputStream bz2Is = null;
            FileOutputStream fos = null;
            try {
                fis = new FileInputStream(file);
                bz2Is = new BZip2CompressorInputStream(fis);
                fos = new FileOutputStream(new File(targetPath));
                int count;
                byte data[] = new byte[2048];
                while ((count = bz2Is.read(data)) != -1) {
                    fos.write(data, 0, count);
                }
                fos.flush();
                if(delete && file.exists()) {
                    file.delete();
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if(null != fis) fis.close();
                    if(null != bz2Is) bz2Is.close();
                    if(null != fos) fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        
        public static void main(String[] args) {
            //压缩bz2
            String srcFilePath = "D:/bz2-test/cms.txt";
            File srcFile = new File(srcFilePath);
            String fileName = BZip2Utils.getCompressedFilename(srcFile.getName());
            String targetPath = srcFile.getParent() + File.separator + fileName;
            compressToBz2(srcFile, targetPath, true);
            
            //解压缩bz2
            srcFilePath = targetPath;
            srcFile = new File(srcFilePath);
            fileName = BZip2Utils.getUncompressedFilename(srcFile.getName());
            targetPath = srcFile.getParent() + File.separator + fileName;
            uncompressFromBz2(srcFile, targetPath, true);
        }
    }
    

    如上所示运行main方法即可实现压缩bz2和解压缩bz2。

    相关文章

      网友评论

          本文标题:java使用commons-compress压缩解压缩bz2文件

          本文链接:https://www.haomeiwen.com/subject/luyfldtx.html