Java压缩类库的使用-4.Apache commons compress中的打包、压缩类库

简介:

  inkfish原创,请勿商业性质转载,转载请注明来源(http://blog.csdn.net/inkfish )。

Apache commons compress BZIP2压缩:(来源:http://blog.csdn.net/inkfish)

package study.inkfish.compress; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream; import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.IOUtils; public class CommonsBZip2Compress extends Compress { @Override protected void doCompress(File srcFile, File destFile) throws IOException { OutputStream out = null; InputStream is = null; try { is = new BufferedInputStream(new FileInputStream(srcFile), bufferLen); out = new GzipCompressorOutputStream(new BufferedOutputStream(new FileOutputStream(destFile), bufferLen)); IOUtils.copy(is, out); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(out); } } @Override protected void doDecompress(File srcFile, File destDir) throws IOException { InputStream is = null; OutputStream os = null; try { File destFile = new File(destDir, FilenameUtils.getBaseName(srcFile.toString())); is = new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(srcFile), bufferLen)); os = new BufferedOutputStream(new FileOutputStream(destFile), bufferLen); IOUtils.copy(is, os); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(os); } } }

Apache commons compress GZIP压缩:(来源:http://blog.csdn.net/inkfish)

package study.inkfish.compress; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream; import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream; import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.IOUtils; public class CommonsGZIPCompress extends Compress { @Override protected void doCompress(File srcFile, File destFile) throws IOException { OutputStream out = null; InputStream is = null; try { is = new BufferedInputStream(new FileInputStream(srcFile), bufferLen); out = new BZip2CompressorOutputStream(new BufferedOutputStream(new FileOutputStream(destFile), bufferLen)); IOUtils.copy(is, out); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(out); } } @Override protected void doDecompress(File srcFile, File destDir) throws IOException { InputStream is = null; OutputStream os = null; try { File destFile = new File(destDir, FilenameUtils.getBaseName(srcFile.toString())); is = new BZip2CompressorInputStream(new BufferedInputStream(new FileInputStream(srcFile), bufferLen)); os = new BufferedOutputStream(new FileOutputStream(destFile), bufferLen); IOUtils.copy(is, os); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(os); } } }

Apache commons compress ZIP压缩:(来源:http://blog.csdn.net/inkfish)

package study.inkfish.compress; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream; import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; import org.apache.commons.io.IOUtils; public class CommonsZipCompress extends Compress { /**用于单文件压缩*/ @Override protected void doCompress(File srcFile, File destFile) throws IOException { ZipArchiveOutputStream out = null; InputStream is = null; try { is = new BufferedInputStream(new FileInputStream(srcFile), bufferLen); out = new ZipArchiveOutputStream(new BufferedOutputStream(new FileOutputStream(destFile), bufferLen)); ZipArchiveEntry entry = new ZipArchiveEntry(srcFile.getName()); entry.setSize(srcFile.length()); out.putArchiveEntry(entry); IOUtils.copy(is, out); out.closeArchiveEntry(); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(out); } } @Override protected void doDecompress(File srcFile, File destDir) throws IOException { ZipArchiveInputStream is = null; try { is = new ZipArchiveInputStream(new BufferedInputStream(new FileInputStream(srcFile), bufferLen)); ZipArchiveEntry entry = null; while ((entry = is.getNextZipEntry()) != null) { if (entry.isDirectory()) { File directory = new File(destDir, entry.getName()); directory.mkdirs(); } else { OutputStream os = null; try { os = new BufferedOutputStream( new FileOutputStream(new File(destDir, entry.getName())), bufferLen); IOUtils.copy(is, os); } finally { IOUtils.closeQuietly(os); } } } } finally { IOUtils.closeQuietly(is); } } }

Apache commons compress AR打包:(来源:http://blog.csdn.net/inkfish)

package study.inkfish.compress; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import org.apache.commons.compress.archivers.ar.ArArchiveEntry; import org.apache.commons.compress.archivers.ar.ArArchiveInputStream; import org.apache.commons.compress.archivers.ar.ArArchiveOutputStream; import org.apache.commons.io.IOUtils; public class CommonsArCompress extends Compress { @Override protected void doCompress(File srcFile, File destFile) throws IOException { ArArchiveOutputStream zout = null; InputStream is = null; try { is = new BufferedInputStream(new FileInputStream(srcFile), bufferLen); zout = new ArArchiveOutputStream(new BufferedOutputStream(new FileOutputStream(destFile), bufferLen)); zout.putArchiveEntry(new ArArchiveEntry(srcFile, srcFile.getName())); IOUtils.copy(is, zout); zout.closeArchiveEntry(); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(zout); } } @Override protected void doDecompress(File srcFile, File destDir) throws IOException { ArArchiveInputStream is = null; try { is = new ArArchiveInputStream(new BufferedInputStream(new FileInputStream(srcFile), bufferLen)); ArArchiveEntry entry = null; while ((entry = is.getNextArEntry()) != null) { if (entry.isDirectory()) { File directory = new File(destDir, entry.getName()); directory.mkdirs(); } else { BufferedOutputStream bos = null; try { byte[] buffer = new byte[bufferLen]; bos = new BufferedOutputStream(new FileOutputStream( new File(destDir, entry.getName())), bufferLen); int len; long size = entry.getSize(); while (size > 0) { if (size < bufferLen) { len = is.read(buffer, 0, (int) size); size -= len; } else { len = is.read(buffer); size -= len; } bos.write(buffer, 0, len); } } finally { IOUtils.closeQuietly(bos); } } } } finally { IOUtils.closeQuietly(is); } } }

Apache commons compress CPIO打包:(来源:http://blog.csdn.net/inkfish)

package study.inkfish.compress; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import org.apache.commons.compress.archivers.cpio.CpioArchiveEntry; import org.apache.commons.compress.archivers.cpio.CpioArchiveInputStream; import org.apache.commons.compress.archivers.cpio.CpioArchiveOutputStream; import org.apache.commons.io.IOUtils; public class CommonsCPIOCompress extends Compress { @Override protected void doCompress(File srcFile, File destFile) throws IOException { CpioArchiveOutputStream out = null; InputStream is = null; try { is = new BufferedInputStream(new FileInputStream(srcFile), bufferLen); out = new CpioArchiveOutputStream(new BufferedOutputStream(new FileOutputStream(destFile), bufferLen)); out.putArchiveEntry(new CpioArchiveEntry(srcFile, srcFile.getName())); IOUtils.copy(is, out); out.closeArchiveEntry(); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(out); } } @Override protected void doDecompress(File srcFile, File destDir) throws IOException { CpioArchiveInputStream is = null; try { is = new CpioArchiveInputStream(new BufferedInputStream(new FileInputStream(srcFile), bufferLen)); CpioArchiveEntry entry = null; while ((entry = is.getNextCPIOEntry()) != null) { if (entry.isDirectory()) { File directory = new File(destDir, entry.getName()); directory.mkdirs(); } else { BufferedOutputStream bos = null; try { byte[] buffer = new byte[bufferLen]; bos = new BufferedOutputStream(new FileOutputStream( new File(destDir, entry.getName())), bufferLen); int len; long size = entry.getSize(); while (size > 0) { if (size < bufferLen) { len = is.read(buffer, 0, (int) size); size -= len; } else { len = is.read(buffer); size -= len; } bos.write(buffer, 0, len); } } finally { IOUtils.closeQuietly(bos); } } } } finally { IOUtils.closeQuietly(is); } } }

Apache commons compress TAR打包:(来源:http://blog.csdn.net/inkfish)

package study.inkfish.compress; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import org.apache.commons.compress.archivers.tar.TarArchiveEntry; import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; import org.apache.commons.io.IOUtils; public class CommonsTarCompress extends Compress { @Override protected void doCompress(File srcFile, File destFile) throws IOException { TarArchiveOutputStream out = null; InputStream is = null; try { is = new BufferedInputStream(new FileInputStream(srcFile), bufferLen); out = new TarArchiveOutputStream(new BufferedOutputStream(new FileOutputStream(destFile), bufferLen)); TarArchiveEntry entry = new TarArchiveEntry(srcFile.getName()); entry.setSize(srcFile.length()); out.putArchiveEntry(entry); IOUtils.copy(is, out); out.closeArchiveEntry(); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(out); } } @Override protected void doDecompress(File srcFile, File destDir) throws IOException { TarArchiveInputStream is = null; try { is = new TarArchiveInputStream(new BufferedInputStream(new FileInputStream(srcFile), bufferLen)); TarArchiveEntry entry = null; while ((entry = is.getNextTarEntry()) != null) { if (entry.isDirectory()) { File directory = new File(destDir, entry.getName()); directory.mkdirs(); } else { BufferedOutputStream bos = null; try { byte[] buffer = new byte[bufferLen]; bos = new BufferedOutputStream(new FileOutputStream( new File(destDir, entry.getName())), bufferLen); int len; long size = entry.getSize(); while (size > 0) { if (size < bufferLen) { len = is.read(buffer, 0, (int) size); size -= len; } else { len = is.read(buffer); size -= len; } bos.write(buffer, 0, len); } } finally { IOUtils.closeQuietly(bos); } } } } finally { IOUtils.closeQuietly(is); } } }

  注:org.apache.commons.io包为Apache common io,项目首页:http://commons.apache.org/io/ ,提供了IO操作的很多方便的方法,基于Apache 2.0 License,可用于商业用途。(来源:http://blog.csdn.net/inkfish)

目录
相关文章
|
20天前
|
前端开发 JavaScript Java
Java打包jar运行时分离lib和jar
在`pom.xml`的`build`节点中,设置`packaging`为`jar`,并配置插件分离依赖库到`lib`目录和资源文件到`resources`目录。这样可以在运行时通过`-Dloader.path=lib,resources`加载外部依赖和资源文件,便于独立升级依赖库和修改资源文件,而无需重新打包程序。具体插件包括`maven-dependency-plugin`、`maven-resources-plugin`和`spring-boot-maven-plugin`等。
58 1
|
3月前
|
Java Maven
java项目中jar启动执行日志报错:no main manifest attribute, in /www/wwwroot/snow-server/z-server.jar-jar打包的大小明显小于正常大小如何解决
在Java项目中,启动jar包时遇到“no main manifest attribute”错误,且打包大小明显偏小。常见原因包括:1) Maven配置中跳过主程序打包;2) 缺少Manifest文件或Main-Class属性。解决方案如下:
856 8
java项目中jar启动执行日志报错:no main manifest attribute, in /www/wwwroot/snow-server/z-server.jar-jar打包的大小明显小于正常大小如何解决
|
5月前
|
分布式计算 大数据 Java
大数据-86 Spark 集群 WordCount 用 Scala & Java 调用Spark 编译并打包上传运行 梦开始的地方
大数据-86 Spark 集群 WordCount 用 Scala & Java 调用Spark 编译并打包上传运行 梦开始的地方
85 1
大数据-86 Spark 集群 WordCount 用 Scala & Java 调用Spark 编译并打包上传运行 梦开始的地方
|
4月前
|
存储 Java API
Java实现导出多个excel表打包到zip文件中,供客户端另存为窗口下载
Java实现导出多个excel表打包到zip文件中,供客户端另存为窗口下载
202 4
|
4月前
|
Java Maven Android开发
【Azure Developer】VS Code打包Java maven Project 遇见 BUILD FAILURE
Unknown lifecycle phase "lean". You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>
|
5月前
|
Java Maven 数据安全/隐私保护
如何实现Java打包程序的加密代码混淆,避免被反编译?
【10月更文挑战第15天】如何实现Java打包程序的加密代码混淆,避免被反编译?
826 2
|
6月前
|
IDE Java 分布式数据库
Apache HBase 落地JAVA 实战
Apache HBase 落地 Java 实战主要涉及使用 Java API 来操作 HBase 数据库,包括表的创建、删除、数据的插入、查询等操作。以下是一个基于 Java 的 HBase 实战指南,包括关键步骤和示例代码。
313 23
|
5月前
|
Java C++
做了个Java打包工具,可以双击启动了!
本文介绍了作者日常使用Java和Swing进行开发的经验,以及Java程序分发时遇到的问题,如需要JRE环境。文中列举了几种常见的Java程序打包方法,并对比了各自的优缺点,最后作者结合这些方案,利用Winform开发了一款工具,将Java程序打包成二进制可执行文件,简化了分发流程。
做了个Java打包工具,可以双击启动了!
|
5月前
|
Java Shell Maven
Flink-11 Flink Java 3分钟上手 打包Flink 提交任务至服务器执行 JobSubmit Maven打包Ja配置 maven-shade-plugin
Flink-11 Flink Java 3分钟上手 打包Flink 提交任务至服务器执行 JobSubmit Maven打包Ja配置 maven-shade-plugin
216 4
|
5月前
|
分布式计算 Java 大数据
大数据-147 Apache Kudu 常用 Java API 增删改查
大数据-147 Apache Kudu 常用 Java API 增删改查
64 1

热门文章

最新文章

推荐镜像

更多