[java]代码库/**

* 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;

}

}

c 压缩java解压文件,java 压缩文件 解压缩文件相关推荐

  1. linux 压缩7z文件夹,Linux 下压缩与解压.zip和.rar及.7z文件

    Linux 下压缩与解压.zip和.rar及.7z文件对于Window下的常见压缩文件.zip和.rar,Linux也有相应的方法来解压它们: 1)对于.zip linux下提供了zip和unzip程 ...

  2. 利用huffman编码对文本文件进行压缩与解压(java实现)

    利用huffman编码对文本文件进行压缩与解压 输入:一个文本文件 输出:压缩后的文件 算法过程: (1)统计文本文件中每个字符的使用频度 (2)构造huffman编码 (3)以二进制流形式压缩文件 ...

  3. Linux中常用压缩、解压命令和压缩比率对比

    本人有一个待解决的问题:当需要压缩比较大的文件,比如1T的时候,如何看到压缩文件进度?挂在后台完全没办法知道最后压缩的文件是不是完整! linux常用的压缩格式为:zip,tar.gz,tar,tar ...

  4. java解压_Java ZIP压缩和解压缩文件(解决中文文件名乱码问题)

    JDK中自带的ZipOutputStream在压缩文件时,如果文件名中有中文,则压缩后的 zip文件打开时发现中文文件名变成乱码. 解决的方法是使用apache-ant-zip.jar包(见附件)中的 ...

  5. Java解压上传zip或rar文件,并解压遍历文件中的html的路径

    1.本文只提供了一个功能的代码 public String addFreeMarker() throws Exception {HttpSession session = request.getSes ...

  6. linux解压7z文件,Linux 下压缩与解压.zip和.rar及.7z文件

    对于Window下的常见压缩文件.zip和.rar,Linux也有相应的方法来解压它们: 1)对于.zip linux下提供了zip和unzip程序,zip是压缩程序,unzip是解压程序.它们的参数 ...

  7. 使用java.util.zip包实现根据文件目录控制文件的压缩与解压

    根据文件目录实现文件的压缩与解压 import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputSt ...

  8. c# 文件压缩、解压及下载

    C#打包文件夹成zip格式(包括文件夹和子文件夹下的所有文件) C# 文件压缩与解压(ZIP格式) asp.net实现文件夹及文件压缩,并实现下载 转载于:https://www.cnblogs.co ...

  9. linux把一个大文件压缩,linux大文件压缩及解压需要注意问题

    注意: 大文件压缩及解压需要在后台进行,如果要查看解压详情,就要输出重定向. 远程服务器,要防止网络断开连接,导致终端关闭,此时终端断开,即使后台进行,解压以及压缩也会停止.解决方法:在指令前加noh ...

  10. cordova 安卓文件多选_安卓zip文件压缩RAR解压软件下载-安卓zip文件压缩RAR解压下载v3.0.4安卓版...

    安卓zip文件压缩RAR解压是一款非常好用的手机压缩解压缩神器,在安卓zip文件压缩RAR解压上我们可以看到很多的实用的功能,软件可以帮助我们更好的处理我们手机中的文件,感兴趣的朋友赶紧下载安卓zip ...

最新文章

  1. Java读取property配置文件
  2. HTML5开发笔记:初窥CANVAS,上传canvas图片到服务器
  3. 总结PHP如何获取当前主机、域名、网址、路径、端口和参数等
  4. 记录 之 tf.data进行数据集处理常用的几个函数介绍
  5. usb3.0 linux无法识别,USB3.0接口不能识别U盘的解决方法
  6. Logan:美团点评的开源移动端基础日志库
  7. 【PHP学习】—PHP连接数据库(六)
  8. android渲染是skia与egl,Huang_Dongsung
  9. python fileinput处理多文件
  10. Linux基础命令1
  11. Springboot实现QQ授权登录
  12. 虚拟机连不上网问题详解
  13. qpython3使用手册图_qpython 图
  14. 胆囊结石在我们的生活中有哪些危害呢?
  15. Android 差分包制作流程分析
  16. CA Server证书申请与颁发 Apache2 HTTPS
  17. TensorFlow输出矩阵的乘法
  18. window系统中打开命令行的四种方式
  19. hexo设置网站的图标Favicon
  20. java创建万年历,维护节假日信息

热门文章

  1. R语言4.04安装教程
  2. pyqt5 yolov4实现车牌识别系统
  3. 知了课堂项目初始化数据库 进行数据迁移
  4. ZZULIOJ.1706: 神奇的编码
  5. 药店管理系统/APP/小程序/网站
  6. asp.net小区停车场管理系统
  7. 复制pdf里的文字,去掉多余换行符的最简单方法(不用word不用python)
  8. ROS安装教程(详细)
  9. Android控件 TextView属性大全
  10. CREO:CREO软件之零件【模型】扫描之扫描、螺旋扫描、可变剖面扫描、扫描混合、混合、边界混合、可变剖面扫描的简介及其使用方法(图文教程)之详细攻略