如何基于java实现解压ZIP TAR等文件
更新时间:2020年7月29日 08:43 点击:2096
java实现对常用.ZIP , .TAR, .TAR.BZ2, .BZ2 ,.TAR.GZ ,.GZ格式文件的解压。
首先需要引入maven依赖,这里使用的是Apache的压缩工具包common-compress,改工具包支持解压、压缩,此代码中我列举出一个zip的压缩示例,其他格式的只需切换改格式对应的流即可。
对于RAR格式文件的解压,目前该工具包还不支持,希望大家做过的可以多多交流。
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-compress</artifactId> <version>1.19</version></dependency>
import org.apache.commons.compress.archivers.tar.TarArchiveEntry; import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; 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.compress.compressors.bzip2.BZip2CompressorInputStream; import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; import org.apache.commons.compress.utils.IOUtils; import java.io.*; /** * @author :zhangzhiyong * @description: java实现常见文件格式的解压与压缩 * 支持.ZIP .TAR .TAR.BZ2 .BZ2 .TAR.GZ .GZ * 其他格式compress包也支持,在此基础上开发即可 * 另外压缩文件只写了ZIP压缩的方法zipCompression,其他的格式类似,换成对应的文件流即可。 * 暂不支持RAR压缩格式,RAR可以用junrar的工具包,但是有缺陷: * 其一:如果压缩文件中有中文名字的文件夹,解压以后文件夹名字是乱码,但是不影响文件夹里面的文件; * 其二:最新 WinRar 压缩产生的 .rar 文件可能会无法解压。 * 缺陷原因:rar 有版权,有些东西没有公开,对解压有一些限制,即使其他解压包也可能有问题,但是建议尝试。 * @date :2020/7/1 20:44 */ public class CompressionFileUtil { /** * @param filePath 需要解压的zip文件的完成路径。 * @param unzipPath 解压过后生成文件的存放路径 * @description: 对zip文件进行解压。 * @return: boolean * @author: ZZY * @time: 2020/7/2 14:47 */ public static boolean zipUnCompress(String filePath, String unzipPath) throws IOException { System.out.println("开始解压ZIP.........."); FileInputStream fis = null; ZipArchiveInputStream zis = null; try { File file = new File(filePath); fis = new FileInputStream(file); zis = new ZipArchiveInputStream(fis); ZipArchiveEntry nze = null; while ((nze = zis.getNextZipEntry()) != null) { FileOutputStream os = null; BufferedOutputStream bos = null; try { System.out.println("正在解压....." + nze.getName()); //自动添加File.separator文件路径的分隔符,根据系统判断是\\还是/ String dir = unzipPath + File.separator + nze.getName(); //解压全路径 System.out.println("dir---" + dir); File file1 = null; if (nze.isDirectory()) { file1 = new File(dir); file1.mkdirs(); } else { file1 = new File(dir); os = new FileOutputStream(file1); bos = new BufferedOutputStream(os); /*byte [] bt = new byte[1024]; int len = 0; while((len = zis.read(bt,0,1024)) != -1){ bos.write(bt,0,len); }*/ IOUtils.copy(zis, bos); //作用与上面注释代码一样 } System.out.println("解压完成......"); } catch (FileNotFoundException e) { e.printStackTrace(); return false; } finally { if (bos != null) { bos.close(); } if (os != null) { os.close(); } } } } catch (Exception e) { e.printStackTrace(); return false; } finally { if (zis != null) { zis.close(); } if (fis != null) { fis.close(); } } return true; } /** * @param filesPathArray 多个文件的绝对路径,是一个数组。 * @param zipFilePath 生成的压缩文件的位置,包括生成的文件名,如D:\zip\test.zip * @description: 将多个文件压缩成ZIP压缩包。 * @return: boolean * @author: ZZY * @time: 2020/7/2 14:42 */ public static boolean zipCompression(String[] filesPathArray, String zipFilePath) throws Exception { System.out.println("开始压缩ZIP文件"); ZipArchiveOutputStream zos = null; FileOutputStream fos = null; try { fos = new FileOutputStream(new File(zipFilePath)); zos = new ZipArchiveOutputStream(fos); for (String filePath : filesPathArray) { FileInputStream fis = null; BufferedInputStream bis = null; try { File file = new File(filePath); // 第二个参数如果是文件全路径名,那么压缩时也会将路径文件夹也缩进去; // 我们只压缩目标文件,而不压缩该文件所处位置的相关文件夹,所以这里我们用file.getName() System.out.println("开始压缩..." + file.getName()); ZipArchiveEntry zae = new ZipArchiveEntry(file, file.getName()); zos.putArchiveEntry(zae); fis = new FileInputStream(file); bis = new BufferedInputStream(fis); int count; byte[] bt = new byte[1024]; while ((count = bis.read(bt, 0, 1024)) != -1) { zos.write(bt, 0, count); } } finally { zos.closeArchiveEntry(); if (bis != null) bis.close(); if (fis != null) fis.close(); } } } finally { if (zos != null) zos.close(); if (fos != null) fos.close(); } System.out.println("压缩完成......"); return true; } /** * @param inputStream 每种TAR文件用不同的输入流,unCompress方法中已注明 * @param unTarPath TAR文件解压后的存放路径 * @description: 解压TAR类文件,包括.TAR .TAR.BZ2 .TAR.GZ * @return: void * @author: ZZY * @time: 2020/7/2 17:42 */ public static void unTar(InputStream inputStream, String unTarPath) throws IOException { FileInputStream fis = null; TarArchiveInputStream tis = null; try { tis = new TarArchiveInputStream(inputStream); TarArchiveEntry nte = null; System.out.println("开始解压......"); while ((nte = tis.getNextTarEntry()) != null) { String dir = unTarPath + File.separator + nte.getName(); System.out.println("正在解压......" + dir); FileOutputStream fos = null; BufferedOutputStream bos = null; try { if (nte.isDirectory()) { File file1 = new File(dir); file1.mkdirs(); } else { File file2 = new File(dir); fos = new FileOutputStream(file2); bos = new BufferedOutputStream(fos); IOUtils.copy(tis, bos); } } catch (Exception e) { e.printStackTrace(); } finally { if (bos != null) { bos.close(); } if (fos != null) { fos.close(); } } } System.out.println("解压完成......"); } catch (IOException e) { e.printStackTrace(); } finally { if (tis != null) { tis.close(); } if (fis != null) { fis.close(); } } } public static boolean unCompress(String filePath,String unCompressPath) throws Exception { String fileType = filePath.toUpperCase(); if(fileType.endsWith(".TAR")){ System.out.println("解压的.TAR包"); //.TAR包用一般的FileInputStream流读取 unTar(new FileInputStream(filePath),unCompressPath); } else if(fileType.endsWith(".TAR.GZ")){ System.out.println("解压的.TAR.GZ包"); //.TAR.GZ包要用GzipCompressorInputStream读取 unTar(new GzipCompressorInputStream(new FileInputStream(filePath)),unCompressPath); } else if(fileType.endsWith(".TAR.BZ2")){ System.out.println("解压的.TAR.BZ2包"); unTar(new BZip2CompressorInputStream(new FileInputStream(filePath)),unCompressPath); } else if(fileType.endsWith(".ZIP")){ System.out.println("解压的.ZIP包"); zipUnCompress(filePath,unCompressPath); } else{ System.out.println("暂不支持该种格式文件的解压"); } return true; } public static void main(String[] args) throws Exception { unCompress("D:\\test\\zip\\nginx-1.18.0.rar","D:\\test\\zip"); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。
上一篇: java利用udp实现发送数据
相关文章
- 这篇文章主要介绍了如何利用java语言实现经典《复杂迷宫》游戏,文中采用了swing技术进行了界面化处理,感兴趣的小伙伴可以动手试一试...2022-02-01
java 运行报错has been compiled by a more recent version of the Java Runtime
java 运行报错has been compiled by a more recent version of the Java Runtime (class file version 54.0)...2021-04-01- 这篇文章主要介绍了在java中获取List集合中最大的日期时间操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-08-15
- 这篇文章主要介绍了教你怎么用Java获取国家法定节假日,文中有非常详细的代码示例,对正在学习java的小伙伴们有非常好的帮助,需要的朋友可以参考下...2021-04-23
- 这篇文章主要介绍了Java如何发起http请求的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-31
- 说起C#和Java这两门语言(语法,数据类型 等),个人以为,大概有90%以上的相似,甚至可以认为几乎一样。但是在工作中,我也发现了一些细微的差别...2020-06-25
- 这篇文章主要介绍了Go语言压缩和解压缩tar.gz文件的方法,实例分析了使用Go语言压缩文件与解压文件的技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-05-03
- 这篇文章主要介绍了解决Java处理HTTP请求超时的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-29
- 这篇文章主要介绍了java 判断两个时间段是否重叠的案例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-08-15
java 画pdf用itext调整表格宽度、自定义各个列宽的方法
这篇文章主要介绍了java 画pdf用itext调整表格宽度、自定义各个列宽的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-01-31- 这篇文章主要介绍了超简洁java实现双色球若干注随机号码生成(实例代码),本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-04-02
- 这篇文章主要介绍了Java生成随机姓名、性别和年龄的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-10-01
- 这篇文章主要介绍了java正则表达式判断前端参数修改表中另一个字段的值,需要的朋友可以参考下...2021-05-07
Java使用ScriptEngine动态执行代码(附Java几种动态执行代码比较)
这篇文章主要介绍了Java使用ScriptEngine动态执行代码,并且分享Java几种动态执行代码比较,需要的朋友可以参考下...2021-04-15- 这篇文章主要介绍了Java开发实现人机猜拳游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-08-03
- 这篇文章主要介绍了Java List集合返回值去掉中括号('[ ]')的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-08-29
Java 8 Stream 的终极技巧——Collectors 功能与操作方法详解
这篇文章主要介绍了Java 8 Stream Collectors 功能与操作方法,结合实例形式详细分析了Java 8 Stream Collectors 功能、操作方法及相关注意事项,需要的朋友可以参考下...2020-05-20Java中lombok的@Builder注解的解析与简单使用详解
这篇文章主要介绍了Java中lombok的@Builder注解的解析与简单使用,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-01-06- 下面小编就为大家带来一篇java中String类型变量的赋值问题介绍。小编觉得挺不错的。现在分享给大家,给大家一个参考。...2016-03-28
- 这篇文章主要介绍了Java连接数据库oracle中文乱码解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-05-16