java bzip2解压缩文件

package app.rtc.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;

public class BZip2Utils {
public static final int BUFFER = 1024;
public static final CharSequence EXT = ".bz2";
/**
* 数据压缩
*
* @param data
* @return
* @throws Exception
*/
public static byte[] compress(byte[] data) throws Exception {
ByteArrayInputStream bais = new ByteArrayInputStream(data);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 压缩
compress(bais, baos);
byte[] output = baos.toByteArray();
baos.flush();
baos.close();
bais.close();
return output;
}
/**
* 文件压缩
*
* @param file
* @throws Exception
*/
public static void compress(File file) throws Exception {
compress(file, true);
}
/**
* 文件压缩
*
* @param file
* @param delete
* 是否删除原始文件
* @throws Exception
*/
public static void compress(File file, boolean delete) throws Exception {
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(file.getPath() + EXT);
compress(fis, fos);
fis.close();
fos.flush();
fos.close();
if (delete) {
file.delete();
}
}
/**
* 数据压缩
*
* @param is
* @param os
* @throws Exception
*/
public static void compress(InputStream is, OutputStream os)
throws Exception {

BZip2CompressorOutputStream gos = new BZip2CompressorOutputStream(os);
int count;
byte data[] = new byte[BUFFER];
while ((count = is.read(data, 0, BUFFER)) != -1) {
gos.write(data, 0, count);
}
gos.finish();
gos.flush();
gos.close();
}
/**
* 文件压缩
*
* @param path
* @throws Exception
*/
public static void compress(String path) throws Exception {
compress(path, true);
}
/**
* 文件压缩
*
* @param path
* @param delete
* 是否删除原始文件
* @throws Exception
*/
public static void compress(String path, boolean delete) throws Exception {
File file = new File(path);
compress(file, delete);
}
/**
* 数据解压缩
*
* @param data
* @return
* @throws Exception
*/
public static byte[] decompress(byte[] data) throws Exception {
ByteArrayInputStream bais = new ByteArrayInputStream(data);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 解压缩
decompress(bais, baos);
data = baos.toByteArray();
baos.flush();
baos.close();
bais.close();
return data;
}
/**
* 文件解压缩
*
* @param file
* @throws Exception
*/
public static void decompress(File file) throws Exception {
decompress(file, true);
}
/**
* 文件解压缩
*
* @param file
* @param delete
* 是否删除原始文件
* @throws Exception
*/
public static void decompress(File file, boolean delete) throws Exception {
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(file.getPath().replace(EXT,
""));
decompress(fis, fos);
fis.close();
fos.flush();
fos.close();
if (delete) {
file.delete();
}
}
/**
* 数据解压缩
*
* @param is
* @param os
* @throws Exception
*/
public static void decompress(InputStream is, OutputStream os)
throws Exception {
BZip2CompressorInputStream gis = new BZip2CompressorInputStream(is);
int count;
byte data[] = new byte[BUFFER];
while ((count = gis.read(data, 0, BUFFER)) != -1) {
os.write(data, 0, count);
}
gis.close();
}
/**
* 文件解压缩
*
* @param path
* @throws Exception
*/
public static void decompress(String path) throws Exception {
decompress(path, true);
}
/**
* 文件解压缩
*
* @param path
* @param delete
* 是否删除原始文件
* @throws Exception
*/
public static void decompress(String path, boolean delete) throws Exception {
File file = new File(path);
decompress(file, delete);
}

public static void main(String[] args) {
try {
BZip2Utils.compress(new File("D:/CHKRPT/report_result/20120923/60/0013100152373/IM-DQ0001-0013100152373.xls"), false);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值