以前寫過一篇:java利用commons-compress壓縮解壓縮targz文件工闺,這次記錄一下bz2的壓縮解壓縮崭闲。
linux命令壓縮解壓縮 bzip2命令
[root@localhost ~]# bzip2 [選項(xiàng)] 源文件
常用參數(shù):
-d 執(zhí)行解壓縮图筹,此時(shí)該選項(xiàng)后的源文件應(yīng)為標(biāo)記有 .bz2 后綴的壓縮包文件。
-k bzip2 在壓縮或解壓縮任務(wù)完成后,會(huì)刪除原始文件,若要保留原始文件,可使用此選項(xiàng)疼邀。
-f bzip2 在壓縮或解壓縮時(shí),若輸出文件與現(xiàn)有文件同名召锈,默認(rèn)不會(huì)覆蓋現(xiàn)有文件旁振,若使用此選項(xiàng),則會(huì)強(qiáng)制覆蓋現(xiàn)有文件涨岁。
-t 測試壓縮包文件的完整性拐袜。
-v 壓縮或解壓縮文件時(shí),顯示詳細(xì)信息梢薪。
-數(shù)字 這個(gè)參數(shù)和 gzip 命令的作用一樣蹬铺,用于指定壓縮等級(jí),-1 壓縮等級(jí)最低秉撇,壓縮比最差甜攀;-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實(shí)現(xiàn)
maven引入(截止發(fā)文,最新版本是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(獲取壓縮后的文件名或獲取解壓后的文件名工具類)這三個(gè)類琐馆。
上代碼:
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 目標(biāo)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);
}
}
如上所示運(yùn)行main方法即可實(shí)現(xiàn)壓縮bz2和解壓縮bz2规阀。