Java压缩类库的使用-2.JDK中的打包、压缩类库

简介:

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

  这里忽略了jar,因为jar实质上属于zip压缩。(来源:http://blog.csdn.net/inkfish)

JDK ZLIB压缩:(来源: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.zip.DeflaterOutputStream; import java.util.zip.InflaterInputStream; import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.IOUtils; public class JdkZLIBCompress 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 DeflaterOutputStream(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 InflaterInputStream(new BufferedInputStream(new FileInputStream(srcFile), bufferLen)); os = new BufferedOutputStream(new FileOutputStream(destFile), bufferLen); IOUtils.copy(is, os); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(os); } } }

JDK 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.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; import org.apache.commons.io.IOUtils; public class JdkZipCompress 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 { ZipInputStream is = null; try { is = new ZipInputStream(new BufferedInputStream(new FileInputStream(srcFile), bufferLen)); ZipEntry entry = null; while ((entry = is.getNextEntry()) != null) { if (entry.isDirectory()) { File directory = new File(destDir, entry.getName()); directory.mkdirs(); is.closeEntry(); } else { OutputStream os = null; try { os = new BufferedOutputStream( new FileOutputStream(new File(destDir, entry.getName())), bufferLen); IOUtils.copy(is, os); } finally { IOUtils.closeQuietly(os); } is.closeEntry(); } } } finally { IOUtils.closeQuietly(is); } } }

JDK 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 java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.IOUtils; public class JdkGZIPCompress 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 GZIPOutputStream(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 GZIPInputStream(new BufferedInputStream(new FileInputStream(srcFile), bufferLen)); os = new BufferedOutputStream(new FileOutputStream(destFile), bufferLen); IOUtils.copy(is, os); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(os); } } }

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

目录
相关文章
|
2月前
|
Cloud Native Java 编译器
Java生态系统的进化:从JDK 1.0到今天
Java生态系统的进化:从JDK 1.0到今天
|
2月前
|
存储 Java 文件存储
如何用 Java 压缩 ZIP 文件?
【2月更文挑战第21天】
37 1
|
2月前
|
Java 编译器 测试技术
滚雪球学Java(03):你知道JDK、JRE和JVM的不同吗?看这里就够了!
【2月更文挑战第12天】🏆本文收录于「滚雪球学Java」专栏,专业攻坚指数级提升,助你一臂之力,带你早日登顶🚀,欢迎大家关注&&收藏!持续更新中,up!up!up!!
108 4
|
1天前
|
Java 编译器 对象存储
java一分钟之Java入门:认识JDK与JVM
【5月更文挑战第7天】本文介绍了Java编程的基础——JDK和JVM。JDK是包含编译器、运行时环境、类库等的开发工具包,而JVM是Java平台的核心,负责执行字节码并实现跨平台运行。常见问题包括版本不匹配、环境变量配置错误、内存溢出和线程死锁。解决办法包括选择合适JDK版本、正确配置环境变量、调整JVM内存参数和避免线程死锁。通过代码示例展示了JVM内存管理和基本Java程序结构,帮助初学者更好地理解JDK和JVM在Java编程中的作用。
13 0
|
4天前
|
Oracle Java 关系型数据库
windows 下 win11 JDK17安装与环境变量的配置(配置简单详细,包含IJ中java文件如何使用命令运行)
本文介绍了Windows 11中安装JDK 17的步骤,包括从官方网站下载JDK、配置环境变量以及验证安装是否成功。首先,下载JDK 17的安装文件,如果没有Oracle账户,可以直接解压缩文件到指定目录。接着,配置系统环境变量,新建`JAVA_HOME`变量指向JDK安装路径,并在`Path`变量中添加。然后,通过命令行(cmd)验证安装,分别输入`java -version`和`javac -version`检查版本信息。最后,作者分享了如何在任意位置运行Java代码,包括在IntelliJ IDEA(IJ)中创建的Java文件,只需去掉包声明,就可以通过命令行直接运行。
|
6天前
|
Java
如何解决使用若依前后端分离打包部署到服务器上后主包无法找到从包中的文件的问题?如何在 Java 代码中访问 jar 包中的资源文件?
如何解决使用若依前后端分离打包部署到服务器上后主包无法找到从包中的文件的问题?如何在 Java 代码中访问 jar 包中的资源文件?
37 0
|
9天前
|
小程序 Java 程序员
【Java探索之旅】我与Java的初相识(二):程序结构与运行关系和JDK,JRE,JVM的关系
【Java探索之旅】我与Java的初相识(二):程序结构与运行关系和JDK,JRE,JVM的关系
26 0
|
11天前
|
Java Windows
java——安装JDK及配置解决常见问题
java——安装JDK及配置解决常见问题
|
14天前
|
Java Linux 开发工具
Linux 配置 Java/JDK 环境
Linux 配置 Java/JDK 环境
22 0
|
22天前
|
Oracle Java 关系型数据库
Java 开发者必备:JDK 版本详解与选择策略(含安装与验证)
Oracle Java SE 支持路线图显示,JDK 8(LTS)支持至2030年,非LTS版本如9-11每6个月发布且支持有限。JDK 11(LTS)支持至2032年,而JDK 17及以上版本现在提供免费商用许可。LTS版本提供长达8年的支持,每2年发布一次。Oracle JDK与OpenJDK有多个社区和公司构建版本,如Adoptium、Amazon Corretto和Azul Zulu,它们在许可证、商业支持和更新方面有所不同。个人选择JDK时,可考虑稳定性、LTS、第三方兼容性和提供商支持。
56 0