文件解压和压缩
/** * 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; } }
相关推荐
解压专家-文件解压和压缩.apk
CompresszZipFile 中带是 zip解压 ZipFileUtil 是文件的压缩 和rar的解压
java实现的文件解压和压缩,没有中文乱码问题,多么复杂的压缩文件都可以解压,多么烦琐的文件都可以压缩
java代码实现文件解压以及多个文件或者文件夹的压缩,
哈夫曼树解压与压缩--14页.pdf
电脑已安装rar.exe程序 内含解压rar文件和压缩rar文件的bat脚本及使用说明 使用简单 建议直接存在桌面进行解压和压缩操作,只需要修改rar文件名即可
代码实现解压,压缩linux下的GZ格式文件
JAVA 实现ZIP文件解压和压缩的功能,所需jar包也在里面了,亲测可行,若是需要java后台实现上传功能可留评论更新
实验内容:写出程序,利用哈弗曼编码实现对文件的压缩,并能解压文件。 实验步骤: 1、压缩 (1) 统计原始文件中各字节出现的概率(次数); (2) 采用哈弗曼算法对各字节进行编码,建立哈弗曼对照表; a) 构造...
一款可以实现文件解压和压缩小应用源码,该应用是实现了怎样利用第三方类库 ZipArchive 来解压和压缩.zip文件的,还可以实现了先从网络下载一个.zip文件下来,然后对它进行解压,最后可以对再进行压缩等功能,也基本...
java解压zip压缩文件,读取压缩文件中的内容并输出到输出文件中。
压缩器解压器为了节省存储空间,常常需要把文本文件采用压缩编码的方式储存。例如:一个包含1000个x的字符串和2000个y的字符串的文本文件在不压缩时占用的空间为3002字节(每个x或每个y占用一个字节,两个字节用来...
实现对bmp文件的压缩解压,提供了压缩和解压两个方法
C++利用Zlib库实现zip文件压缩及解压 支持递归压缩.可配合自动更新功能实现zip压缩包进得软件更新
5 比较decompress txt和input txt中的字符是否完全相同 并计算压缩率 压缩率 compress txt文件大小 input txt的文件大小 100%">1 初始化:给出十万个随机字符 字符中只包含小写的英文字母a z 输出初始化文件名为...
今天梳理一下项目中用到的压缩、解压文件夹或文件的方法,发现因为需求不同,已经用了好几个不同组件。今天就好好整理记录下,别下次遇到需求又重头开始了。 DotNetZip DotNetZip是一个开源的免费类库,主要提供了...
利用哈夫曼编码进行对已有文件进行重新编码可以大大提高减小文件大小,减少存储空间。但是,这要求在首先对一个现有文件进行编码行成新的文件,也...在文件使用时,再对压缩文件进行解压缩,也就是译码,复原原有文件
3将命令行参数指定的压缩文件解压到当前目录。 其Usage为:MyZip /E <压缩文件名> 提示:只需修改MyZip程序,将原先读取要加入的文件的内容后直接写到压缩文件中的代码,修改为先将要加入的文件压缩为一个临时文件后...
UPX压缩和解压缩器,不是源码,是工具,可以实现exe文件的压缩和解压缩
对zip文件进行解压缩工具类,以及jar包