新增BZip2解压缩工具

This commit is contained in:
椰子 2022-08-09 00:11:42 +08:00
parent ab645264df
commit 0b8bc52f99
3 changed files with 236 additions and 1 deletions

View File

@ -12,9 +12,14 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.2</version>
<version>5.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.21</version>
</dependency>
</dependencies>
<properties>

View File

@ -0,0 +1,204 @@
package com.simaek.util;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
import java.io.*;
/**
* BZip2文件解压缩工具
*
* @author Yaser Hsueh
*/
@SuppressWarnings({"ResultOfMethodCallIgnored", "unused"})
public abstract class BZip2Utils {
private static final int BUFFER = 8;
private static final String EXT_SUFFIX = ".bz2";
/**
* 对字节数据进行压缩
*
* @param data 原始字节数据
* @return 压缩后的字节数据
* @throws IOException 数据读写异常
*/
public static byte[] compress(byte[] data) throws IOException {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
compress(byteArrayInputStream, byteArrayOutputStream);
byteArrayOutputStream.flush();
byteArrayOutputStream.close();
byteArrayInputStream.close();
return byteArrayOutputStream.toByteArray();
}
/**
* 对数据流进行压缩
*
* @param is 压缩前的数据输入流
* @param os 压缩后的数据输出流
* @throws IOException 数据流读写异常
*/
public static void compress(InputStream is, OutputStream os) throws IOException {
BZip2CompressorOutputStream bzip2OS = new BZip2CompressorOutputStream(os, 8);
int count;
byte[] data = new byte[BUFFER];
while ((count = is.read(data, 0, data.length)) != -1) {
bzip2OS.write(data, 0, count);
}
bzip2OS.finish();
bzip2OS.flush();
bzip2OS.close();
}
/**
* 对文件进行压缩
*
* @param file 待压缩的文件
* @param delete 压缩完成后删除源文件
* @throws IOException 文件读写异常
*/
public static void compress(File file, boolean delete) throws IOException {
if (!file.exists()) {
throw new FileNotFoundException("源文件不存在");
}
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(file.getPath() + EXT_SUFFIX);
compress(fis, fos);
fis.close();
fos.flush();
fos.close();
if (delete) {
file.delete();
}
}
/**
* 对文件进行压缩压缩完成后删除源文件
*
* @param file 待压缩的文件
* @throws IOException 文件读写异常
*/
public static void compress(File file) throws IOException {
compress(file, true);
}
/**
* 对指定路径的文件进行压缩
*
* @param path 待压缩的文件路径
* @param delete 压缩完成后删除源文件
* @throws IOException 文件读写异常
*/
public static void compress(String path, boolean delete) throws IOException {
File file = new File(path);
compress(file, delete);
}
/**
* 对指定路径的文件进行压缩压缩完成后删除源文件
*
* @param path 待压缩的文件路径
* @throws IOException 文件读写异常
*/
public static void compress(String path) throws IOException {
compress(path, true);
}
/**
* 对字节数据进行解压
*
* @param data 压缩过的字节数据
* @return 解压后的字节数据
* @throws IOException 数据读写异常
*/
public static byte[] uncompress(byte[] data) throws IOException {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
uncompress(byteArrayInputStream, byteArrayOutputStream);
byteArrayInputStream.close();
return byteArrayOutputStream.toByteArray();
}
/**
* 对数据流进行解压
*
* @param is 压缩后的数据输入流
* @param os 解压后的数据输出流
* @throws IOException 数据流读写异常
*/
private static void uncompress(InputStream is, OutputStream os) throws IOException {
/*
* 对于多个合并的bz2输入流
* BZip2CompressorInputStream decompressConcatenated
* if true, decompress until the end of the input;
* if false, stop after the first .bz2 stream and
* leave the input position to point to the next
* byte after the .bz2 stream
*/
BZip2CompressorInputStream bzip2IS = new BZip2CompressorInputStream(is, true);
int count;
byte[] data = new byte[BUFFER];
while ((count = bzip2IS.read(data, 0, data.length)) != -1) {
os.write(data, 0, count);
}
os.flush();
os.close();
bzip2IS.close();
}
/**
* 对文件进行解压
*
* @param file 压缩文件
* @param delete 解压完成后删除源文件
* @throws IOException 文件读写异常
*/
public static void uncompress(File file, boolean delete) throws IOException {
if (!file.exists()) {
throw new FileNotFoundException("源文件不存在");
}
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(file.getPath().replace(EXT_SUFFIX, ""));
uncompress(fis, fos);
fis.close();
fos.flush();
fos.close();
if (delete) {
file.delete();
}
}
/**
* 对文件进行解压解压完成后删除源文件
*
* @param file 压缩文件
* @throws IOException 文件读写异常
*/
public static void uncompress(File file) throws IOException {
uncompress(file, true);
}
/**
* 对指定路径的文件进行解压
*
* @param path 压缩文件路径
* @param delete 解压完成后删除源文件
* @throws IOException 文件读写异常
*/
public static void uncompress(String path, boolean delete) throws IOException {
File file = new File(path);
uncompress(file, delete);
}
/**
* 对指定路径的文件进行解压解压完成后删除源文件
*
* @param path 压缩文件路径
* @throws IOException 文件读写异常
*/
public static void uncompress(String path) throws IOException {
uncompress(path, true);
}
}

View File

@ -0,0 +1,26 @@
package com.simaek.util;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.IOException;
/**
* 测试BZip2文件解压缩工具
*
* @author Yaser Hsueh
*/
class BZip2UtilsTest {
@Test
void compress() throws IOException {
String path = "/data/HS_H08_20210728_0010_B01_FLDK_R10_S0310.DAT";
BZip2Utils.compress(new File(path));
}
@Test
void uncompress() throws IOException {
String path = "/data/HS_H08_20210728_0010_B01_FLDK_R10_S0310.DAT.bz2";
BZip2Utils.uncompress(new File(path));
}
}