博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
文件解压缩工具类(适用zip,tar,tar.gz)
阅读量:4161 次
发布时间:2019-05-26

本文共 4860 字,大约阅读时间需要 16 分钟。

之前写过的一篇关于解压缩的文章:

pom.xml

org.apache.commons
commons-compress
1.18
org.apache.ant
ant
1.9.7
test
org.apache.ant
ant
1.9.7
compile

文件解压缩工具类

/** * 文件解压缩工具类 */public class FileUnpackUtil {
/** * 解压缩tar以及tar.gz文件 * * @param file tar以及tar.gz文件地址 */ public static void unpackTarGz(File file) {
unpackTarGz(file, file.getParentFile()); } /** * 解压缩tar以及tar.gz文件 * * @param srcFile tar以及tar.gz文件地址 * @param destFile 目标文件保存地址 */ public static void unpackTarGz(File srcFile, File destFile) {
TarInputStream tarIn = null; OutputStream out = null; try {
tarIn = new TarInputStream(new GZIPInputStream(new BufferedInputStream(new FileInputStream(srcFile))), 1024 * 2); createDirectory(destFile, null);//创建输出目录 TarEntry entry = null; while ((entry = tarIn.getNextEntry()) != null) {
if (entry.isDirectory()) {
//是目录 entry.getName(); createDirectory(destFile, entry.getName());//创建空目录 } else {
//是文件 File tmpFile = new File(destFile, entry.getName()); //创建输出目录 createDirectory(tmpFile.getParentFile(), null); out = new FileOutputStream(tmpFile); int length = 0; byte[] b = new byte[2048]; while ((length = tarIn.read(b)) != -1) {
out.write(b, 0, length); } } } } catch (Exception e) {
e.printStackTrace(); } finally {
try {
if (tarIn != null) {
tarIn.close(); } if (out != null) {
out.close(); } } catch (IOException e) {
e.printStackTrace(); } } } /** * 解压缩zip文件 * * @param file zip文件地址 * @param charset 字符编码 window系统中文适用GB2312能解决乱码 */ public static void unpackZip(File file, String charset) {
unpackZip(file, file.getParentFile(), charset); } /** * 解压缩zip文件 * * @param srcFile zip文件地址 * @param destFile 解压缩后存放地址 * @param charset 字符编码 window系统中文适用GB2312能解决乱码 */ public static void unpackZip(File srcFile, File destFile, String charset) {
if (destFile == null) {
destFile = new File(srcFile.getParentFile(), srcFile.getName().substring(0, srcFile.getName().lastIndexOf("."))); } ZipFile zipFile = null; InputStream in = null; OutputStream out = null; try {
Charset decoding = Charset.forName(charset); zipFile = new ZipFile(srcFile, decoding); createDirectory(destFile); Enumeration
enums = zipFile.entries(); while (enums.hasMoreElements()) {
ZipEntry entry = (ZipEntry) enums.nextElement(); if (entry.isDirectory()) {
//是目录 createDirectory(destFile, entry.getName());//创建空目录 } else {
//是文件 File tmpFile = new File(destFile, entry.getName()); if (!tmpFile.getParentFile().exists()) {
tmpFile.getParentFile().mkdirs(); } in = zipFile.getInputStream(entry); out = new BufferedOutputStream(new FileOutputStream(tmpFile)); int len = 0; byte[] bytes = new byte[2048]; while ((len = in.read(bytes)) != -1) {
out.write(bytes, 0, len); } } } } catch (Exception e) {
e.printStackTrace(); } finally {
try {
if (out != null) {
out.close(); } if (in != null) {
in.close(); } } catch (IOException e) {
e.printStackTrace(); } } } private static void createDirectory(File file) {
createDirectory(file, null); } private static void createDirectory(File file, String subDir) {
if (!(subDir == null || subDir.trim().equals(""))) {
//子目录不为空 file = new File(file, subDir); } if (!file.exists()) {
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs(); } file.mkdirs(); } }}

转载地址:http://hzixi.baihongyu.com/

你可能感兴趣的文章
k8s web终端连接工具
查看>>
手绘VS码绘(一):静态图绘制(码绘使用P5.js)
查看>>
链睿和家乐福合作推出下一代零售业隐私保护技术
查看>>
艾默生纪念谷轮™在空调和制冷领域的百年创新成就
查看>>
JavaSE_day14 集合中的Map集合_键值映射关系
查看>>
异常 Java学习Day_15
查看>>
Mysql初始化的命令
查看>>
浅谈HTML
查看>>
css基础
查看>>
Servlet进阶和JSP基础
查看>>
servlet中的cookie和session
查看>>
过滤器及JSP九大隐式对象
查看>>
【Python】学习笔记——-7.0、面向对象编程
查看>>
【Python】学习笔记——-7.2、访问限制
查看>>
【Python】学习笔记——-7.3、继承和多态
查看>>
【Python】学习笔记——-7.5、实例属性和类属性
查看>>
git中文安装教程
查看>>
虚拟机 CentOS7/RedHat7/OracleLinux7 配置静态IP地址 Ping 物理机和互联网
查看>>
Jackson Tree Model Example
查看>>
常用js收集
查看>>