Java压缩类库的使用-3.Apache Ant中的打包、压缩类库

简介:

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

  这里需要关注的是BZIP2格式,经过测试,总是无法正确压缩,原因未知,而apache commons bzip2格式的文件压缩正常。(来源:http://blog.csdn.net/inkfish)

Ant 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 java.util.Enumeration; import org.apache.commons.io.IOUtils; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipFile; import org.apache.tools.zip.ZipOutputStream; public class AntZipCompress extends Compress { @Override protected void doCompress(File srcFile, File destFile) throws IOException { ZipOutputStream zout = null; InputStream is = null; try { is = new BufferedInputStream(new FileInputStream(srcFile), bufferLen); zout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(destFile), bufferLen)); zout.putNextEntry(new ZipEntry(srcFile.getName())); IOUtils.copy(is, zout); zout.closeEntry(); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(zout); } } @Override protected void doDecompress(File srcFile, File destDir) throws IOException { ZipFile zipFile = new ZipFile(srcFile); try { @SuppressWarnings("unchecked") Enumeration<ZipEntry> enums = zipFile.getEntries(); while (enums.hasMoreElements()) { ZipEntry entry = enums.nextElement(); InputStream is = new BufferedInputStream(zipFile.getInputStream(entry), bufferLen); OutputStream os = null; try { os = new BufferedOutputStream(new FileOutputStream(new File(destDir, entry.getName())), bufferLen); IOUtils.copy(is, os); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(os); } } } finally { ZipFile.closeQuietly(zipFile); } } }

Ant BZIP压缩:(来源: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.io.FilenameUtils; import org.apache.commons.io.IOUtils; import org.apache.tools.bzip2.CBZip2InputStream; import org.apache.tools.bzip2.CBZip2OutputStream; public class AntBzip2Compress 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 CBZip2OutputStream(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 CBZip2InputStream(new BufferedInputStream(new FileInputStream(srcFile), bufferLen)); os = new BufferedOutputStream(new FileOutputStream(destFile), bufferLen); IOUtils.copy(is, os); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(os); } } }

Ant 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.io.IOUtils; import org.apache.tools.tar.TarEntry; import org.apache.tools.tar.TarInputStream; import org.apache.tools.tar.TarOutputStream; public class AntTarCompress extends Compress { @Override protected void doCompress(File srcFile, File destFile) throws IOException { TarOutputStream out = null; InputStream is = null; try { is = new BufferedInputStream(new FileInputStream(srcFile), bufferLen); out = new TarOutputStream(new BufferedOutputStream(new FileOutputStream(destFile), bufferLen)); TarEntry entry = new TarEntry(srcFile.getName()); entry.setSize(srcFile.length()); out.putNextEntry(entry); IOUtils.copy(is, out); out.closeEntry(); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(out); } } @Override protected void doDecompress(File srcFile, File destDir) throws IOException { TarInputStream is = null; try { is = new TarInputStream(new BufferedInputStream(new FileInputStream(srcFile), bufferLen)); TarEntry entry = null; while ((entry = is.getNextEntry()) != null) { if (entry.isDirectory()) { File directory = new File(destDir, entry.getName()); directory.mkdirs(); } else { BufferedOutputStream bos = null; try { byte[] buffer = new byte[1024 * 512];//512k buffer bos = new BufferedOutputStream(new FileOutputStream( new File(destDir, entry.getName())), buffer.length); int len; long size = entry.getSize(); while (size > 0) { if (size < buffer.length) { 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)

目录
相关文章
|
13天前
|
分布式计算 大数据 Java
大数据-86 Spark 集群 WordCount 用 Scala & Java 调用Spark 编译并打包上传运行 梦开始的地方
大数据-86 Spark 集群 WordCount 用 Scala & Java 调用Spark 编译并打包上传运行 梦开始的地方
13 1
大数据-86 Spark 集群 WordCount 用 Scala & Java 调用Spark 编译并打包上传运行 梦开始的地方
|
2天前
|
Java Maven 数据安全/隐私保护
如何实现Java打包程序的加密代码混淆,避免被反编译?
【10月更文挑战第15天】如何实现Java打包程序的加密代码混淆,避免被反编译?
12 2
|
9天前
|
Java C++
做了个Java打包工具,可以双击启动了!
本文介绍了作者日常使用Java和Swing进行开发的经验,以及Java程序分发时遇到的问题,如需要JRE环境。文中列举了几种常见的Java程序打包方法,并对比了各自的优缺点,最后作者结合这些方案,利用Winform开发了一款工具,将Java程序打包成二进制可执行文件,简化了分发流程。
做了个Java打包工具,可以双击启动了!
|
15天前
|
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
54 4
|
17天前
|
安全 Java Android开发
JavaWeb解压缩漏洞之ZipSlip与Zip炸弹
JavaWeb解压缩漏洞之ZipSlip与Zip炸弹
20 5
|
27天前
|
安全 Java Android开发
JavaWeb解压缩漏洞之ZipSlip与Zip炸弹
JavaWeb解压缩漏洞之ZipSlip与Zip炸弹
62 2
|
1月前
|
算法 Java
Java 压缩文件
在Java中压缩文件是一个常见的需求,通常可以通过使用Java自带的`java.util.zip`包来实现。这个包提供了`ZipOutputStream`类来创建ZIP格式的压缩文件。以下是一个简单的示例,展示了如何将多个文件压缩到一个ZIP文件中。 ### 示例:将多个文件压缩到一个ZIP文件中 ```java import java.io.*; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class ZipFilesExample { public static vo
|
2月前
|
消息中间件 Java Kafka
【Azure 事件中心】在微软云中国区 (Mooncake) 上实验以Apache Kafka协议方式发送/接受Event Hubs消息 (Java版)
【Azure 事件中心】在微软云中国区 (Mooncake) 上实验以Apache Kafka协议方式发送/接受Event Hubs消息 (Java版)
|
2月前
|
Java
Java SpringBoot 7z 压缩、解压
Java SpringBoot 7z 压缩、解压
62 1
|
2月前
|
XML Java Maven
Java 中的 Maven 和 Ant 的区别
【8月更文挑战第22天】
110 0

推荐镜像

更多