文件解压和压缩

 

文件解压和压缩

/**
  * compress file
  * 
  * @param in
  * @param out
  * @param compressionAlgorithm
  * @param calucateCompressedMd5
  * @param listener
  * @return
  * @throws TemplateServiceException
  */
 public final static String compress(File in, File out,
  String compressionAlgorithm, boolean calucateCompressedMd5,
  FileHandleListener listener) throws TemplateServiceException {

try {
  FileInputStream fis = new FileInputStream(in);
  FileOutputStream fos = new FileOutputStream(out);
  return compress(
	  fis,
	  fos,
	  compressionAlgorithm,
	  calucateCompressedMd5,
	  listener);
} catch (FileNotFoundException e) {
  throw new TemplateServiceException(
	  TemplateServiceException.FILE_NOT_EXIST_ERROR,
	  new String[] { in.getPath() });
}

 }

 public final static String compress(InputStream in, OutputStream out,
  String compressionAlgorithm, boolean calucateCompressedMd5,
  FileHandleListener listener) throws TemplateServiceException {
try {
  MessageDigest md = null;
  if (calucateCompressedMd5) {
	md = MessageDigest.getInstance("md5");
	out = new DigestOutputStream(out, md);
  }

  CompressorStreamFactory csf = new CompressorStreamFactory();
  out = csf.createCompressorOutputStream(
	  toCompressorAlgorithm(compressionAlgorithm),
	  out);

  if (listener != null) {
	out = new FileHandleOutputStream(out, listener);
  }

  FileCopyUtils.copy(in, out, null);

  if (calucateCompressedMd5) {
	byte[] md5Bytes = md.digest();
	String md5 = OutputFormatter.binaryToHex(md5Bytes);
	return md5;
  }

  return null;
} catch (NoSuchAlgorithmException e) {
  throw new TemplateServiceException(TemplateServiceException.UNKNOWN_ERROR);
} catch (CompressorException e) {
  throw new TemplateServiceException(TemplateServiceException.UNKNOWN_ERROR);
}

 }

 /**
  * uncompress file
  * 
  * @param in
  * @param out
  * @param compressionAlgorithm
  * @param calucateUncompressedMd5
  * @param listener
  * @return
  * @throws TemplateServiceException
  */
 public final static String uncompress(File in, File out,
  String compressionAlgorithm, boolean calucateUncompressedMd5,
  FileHandleListener listener) throws TemplateServiceException {
try {
  FileInputStream fis = new FileInputStream(in);
  FileOutputStream fos = new FileOutputStream(out);
  return uncompress(
	  fis,
	  fos,
	  compressionAlgorithm,
	  calucateUncompressedMd5,
	  listener);
} catch (FileNotFoundException e) {
  throw new TemplateServiceException(
	  TemplateServiceException.FILE_NOT_EXIST_ERROR,
	  new String[] { in.getPath() });
}
 }

 public final static String uncompress(InputStream in, OutputStream out,
  String compressionAlgorithm, boolean calucateUncompressedMd5,
  FileHandleListener listener) throws TemplateServiceException {
try {
  MessageDigest md = null;
  if (calucateUncompressedMd5) {
	md = MessageDigest.getInstance("md5");
	out = new DigestOutputStream(out, md);
  }

  CompressorStreamFactory csf = new CompressorStreamFactory();
  in = csf.createCompressorInputStream(
	  toCompressorAlgorithm(compressionAlgorithm),
	  in);

  if (listener != null) {
	out = new FileHandleOutputStream(out, listener);
  }

  FileCopyUtils.copy(in, out, null);

  if (calucateUncompressedMd5) {
	byte[] md5Bytes = md.digest();
	String md5 = OutputFormatter.binaryToHex(md5Bytes);
	return md5;
  }

  return null;
} catch (NoSuchAlgorithmException e) {
  throw new TemplateServiceException(TemplateServiceException.UNKNOWN_ERROR);
} catch (CompressorException e) {
  throw new TemplateServiceException(TemplateServiceException.UNKNOWN_ERROR);
}
 }

 public final static String toCompressorAlgorithm(String compressionAlgorithm) {
if ("bz2".equalsIgnoreCase(compressionAlgorithm)
    || "bzip2".equalsIgnoreCase(compressionAlgorithm)) {
  return CompressorStreamFactory.BZIP2;
} else if ("gz".equalsIgnoreCase(compressionAlgorithm)
           || "gzip".equalsIgnoreCase(compressionAlgorithm)) {
  return CompressorStreamFactory.GZIP;
} else if ("xz".equalsIgnoreCase(compressionAlgorithm)) {
  return CompressorStreamFactory.XZ;
} else if ("pack200".equalsIgnoreCase(compressionAlgorithm)) {
  return CompressorStreamFactory.PACK200;
} else {
  return compressionAlgorithm;
}
 }

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值