环境

操作系统:win7
java:jdk7
第三方包:commons-compress-1.14.jar

需求

不管是文件夹还是常规文件,实现基本的打包压缩。

思路:
①先把需要压缩的文件,打包成.tar文件。
②使用gzip把刚刚打包的.tar文件进行压缩(tar.gz)


打包代码

http://commons.apache.org/proper/commons-compress/examples.html

官网:

TarArchiveEntry entry = new TarArchiveEntry(name);
entry.setSize(size);
tarOutput.putArchiveEntry(entry);
tarOutput.write(contentOfEntry);
tarOutput.closeArchiveEntry();

官网给的代码很简单,也就是一个思路。

我的完整代码:

/*** 归档* @param entry* @throws IOException* @author yutao* @return * @date 2017年5月27日下午1:48:23*/private static String archive(String entry) throws IOException {File file = new File(entry);TarArchiveOutputStream tos = new TarArchiveOutputStream(new FileOutputStream(file.getAbsolutePath() + ".tar"));String base = file.getName();if(file.isDirectory()){archiveDir(file, tos, base);}else{archiveHandle(tos, file, base);}tos.close();return file.getAbsolutePath() + ".tar";}/*** 递归处理,准备好路径* @param file* @param tos* @param base * @throws IOException* @author yutao* @date 2017年5月27日下午1:48:40*/private static void archiveDir(File file, TarArchiveOutputStream tos, String basePath) throws IOException {File[] listFiles = file.listFiles();for(File fi : listFiles){if(fi.isDirectory()){archiveDir(fi, tos, basePath + File.separator + fi.getName());}else{archiveHandle(tos, fi, basePath);}}}/*** 具体归档处理(文件)* @param tos* @param fi* @param base* @throws IOException* @author yutao* @date 2017年5月27日下午1:48:56*/private static void archiveHandle(TarArchiveOutputStream tos, File fi, String basePath) throws IOException {TarArchiveEntry tEntry = new TarArchiveEntry(basePath + File.separator + fi.getName());tEntry.setSize(fi.length());tos.putArchiveEntry(tEntry);BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fi));byte[] buffer = new byte[1024];int read = -1;while((read = bis.read(buffer)) != -1){tos.write(buffer, 0 , read);}bis.close();tos.closeArchiveEntry();//这里必须写,否则会失败}

上面代码使用方法:只需要调archive(文件或目录路径)

archive方法里面主要调了两个方法archiveDirarchiveHandle,返回的是最后打包文件名的全路径。
archiveDir方法是个递归的方法。目的就是为了把文件夹中里的所有文件都遍历出来。其实更确切的说是把文件相对路径(相对于打包目录,我这里就是test目录)通过递归拼接好,为真正的打包写入操作做准备。
archiveHandle方法,是真正的打包方法。参数为:1、打包输出流2、需要打包的文件3、打包文件里的路径(之前拼接好的路径)。
tos.closeArchiveEntry()这里一定要注意;官网api解释为:
http://commons.apache.org/proper/commons-compress/javadocs/api-release/index.html

所有包含数据的entry都必须调用此方法。
原因是为了满足缓存区基于记录的写入,我们必须缓冲写入流里的数据。因此可能有数据片段仍然被组装(我认为应该是肯能还有数据在缓存区里),所以必须在该条目(entry)关闭之前写入输出流,并写入下个条目。

压缩

这里使用的是gzip进行压缩,该压缩最适合单一文件的压缩,这也是为什么要先打包成tar文件的原因。

我的压缩代码:

/*** 把tar包压缩成gz* @param path* @throws IOException* @author yutao* @return * @date 2017年5月27日下午2:08:37*/public static String compressArchive(String path) throws IOException{BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path));GzipCompressorOutputStream gcos = new GzipCompressorOutputStream(new BufferedOutputStream(new FileOutputStream(path + ".gz")));byte[] buffer = new byte[1024];int read = -1;while((read = bis.read(buffer)) != -1){gcos.write(buffer, 0, read);}gcos.close();bis.close();return path + ".gz";}

该方法很简单,没什么好说的。

使用方法:调用compressArchive(需要解压文件的路径)

解压

①先解压tar.gztar
②再对tar进行解压,并删除tar文件。

解压gz文件

官网代码:
http://commons.apache.org/proper/commons-compress/examples.html

InputStream fin = Files.newInputStream(Paths.get("archive.tar.gz"));
BufferedInputStream in = new BufferedInputStream(fin);
OutputStream out = Files.newOutputStream(Paths.get("archive.tar"));
GZipCompressorInputStream gzIn = new GZipCompressorInputStream(in);
final byte[] buffer = new byte[buffersize];
int n = 0;
while (-1 != (n = gzIn.read(buffer))) {out.write(buffer, 0, n);
}
out.close();
gzIn.close();

我的代码:

/*** 解压* @param archive* @author yutao* @throws IOException * @date 2017年5月27日下午4:03:29*/private static void unCompressArchiveGz(String archive) throws IOException {File file = new File(archive);BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));String fileName = file.getName().substring(0, file.getName().lastIndexOf("."));String finalName = file.getParent() + File.separator + fileName;BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(finalName));GzipCompressorInputStream gcis = new GzipCompressorInputStream(bis);byte[] buffer = new byte[1024];int read = -1;while((read = gcis.read(buffer)) != -1){bos.write(buffer, 0, read);}gcis.close();bos.close();unCompressTar(finalName);}

解压tar

官网代码:
http://commons.apache.org/proper/commons-compress/examples.html

TarArchiveEntry entry = tarInput.getNextTarEntry();
byte[] content = new byte[entry.getSize()];
LOOP UNTIL entry.getSize() HAS BEEN READ {tarInput.read(content, offset, content.length - offset);
}

我的代码:

/*** 解压tar* @param finalName* @author yutao* @throws IOException * @date 2017年5月27日下午4:34:41*/private static void unCompressTar(String finalName) throws IOException {File file = new File(finalName);String parentPath = file.getParent();TarArchiveInputStream tais = new TarArchiveInputStream(new FileInputStream(file));TarArchiveEntry tarArchiveEntry = null;while((tarArchiveEntry = tais.getNextTarEntry()) != null){String name = tarArchiveEntry.getName();File tarFile = new File(parentPath, name);if(!tarFile.getParentFile().exists()){tarFile.getParentFile().mkdirs();}BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(tarFile));int read = -1;byte[] buffer = new byte[1024];while((read = tais.read(buffer)) != -1){bos.write(buffer, 0, read);}bos.close();}tais.close();file.delete();//删除tar文件}

如何使用上面的代码

public static void main(String[] args) throws IOException {String entry = "C:\\Users\\yutao\\Desktop\\pageage\\test";//需要压缩的文件夹String archive = archive(entry);//生成tar包String path = compressArchive(archive);//生成gz包//      unCompressArchiveGz("C:\\Users\\yutao\\Desktop\\pageage\\test.tar.gz");//解压}

参考地址:
http://snowolf.iteye.com/blog/648652

java 压缩/解压【tar.gz】相关推荐

  1. 2020-10-21 ubuntu 打包解包压缩解压tar gz bz2 tar.Z tgz rar lha格式

    ubuntu 打包解包压缩解压tar gz bz2 tar.Z tgz rar lha格式 一.tar 格式 解包:tar xvf FileName.tar 打包:tar cvf FileName.t ...

  2. win系统压缩/解压.tar.gz 文件的方法

    一.将文件压缩成.tar.gz 文件 进入到被压缩文件的文件夹,点击右键打开Windows PoweShell,输入下面命令: tar -cvzf file_name.tar.gz "pat ...

  3. java中解压tar.gz文件

    在开发中我们经常需要对gz文件进行解压缩,在java中解压gz文件还是比较繁琐的,为此写了一个工具类方便需要的时候可以直接拿过来用.代码如下: package com.eggsl.utils;impo ...

  4. linux下文件夹压缩解压.tar , .gz , .tar.gz , .bz2 , .tar.bz2 , .bz , .tar.bz , .zip , .rar

    .tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) ---------------------- ...

  5. Linux压缩解压tar.gz和zip包命令汇总

    1.tar包和gz包 tar包和gz包是两个不同的文件包,有三种不同后缀..tar .gz .tar.gz tar包:使用tar命令,打包文件或者文件夹,只打包,不压缩 gz包:使用gzip命令,只压 ...

  6. linux解压tar.gz文件,linux tar.gz压缩解压命令详解

    linux tar.gz命令是一个常见的文件解压缩命令,那么它具体用法是怎样的呢?下面由学习啦小编为大家整理了linux tar.gz命令的相关知识,希望对大家有帮助! 1.linux tar.gz压 ...

  7. linux关于压缩解压tar包

    tar -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个 ...

  8. java 压缩、解压缩 tar.gz

    引入依赖 <dependency><groupId>org.apache.commons</groupId><artifactId>commons-co ...

  9. php tar.gz文件,PHP解压tar.gz格式文件的方法,_PHP教程

    PHP解压tar.gz格式文件的方法, 本文实例讲述了PHP解压tar.gz格式文件的方法.分享给大家供大家参考,具体如下: 1.运用php自带压缩与归档扩展(phar) $phar = new Ph ...

最新文章

  1. 疫情严重!国内互联网公司上班时间汇总!
  2. 嵌入式Linux入门经典笔记
  3. ubuntu下eclipse中键盘失灵
  4. 1.8 分割字符串(spilt())
  5. spring 框架(一)
  6. 设置VS2008 快捷键 快速注释
  7. easyui结合java,Spring+SpringMVC+MyBatis+easyUI整合基础篇(二)牛刀小试
  8. 单片机的C语言应用程序设计实训教程,单片机的c语言程序设计实训
  9. MATLAB代码:基于分布式优化的多产消者非合作博弈能量共享
  10. 从turtlesim到贪吃蛇……(补充)
  11. T32用的一个python脚本-替换文件中的文件夹路径字符串
  12. 微软开源 Python 自动化神器 Playwright
  13. 34岁的困境!测试工程师如何突破职业瓶颈?
  14. PAT --- 1072.开学寄语 (20 分)
  15. 数据分析师必须掌握的 十三大数据分析方法论!
  16. 30岁的程序员......
  17. 一款视频剪辑软件--爱剪辑
  18. 一般UI设计要学习的内容都有哪些
  19. echart 地图加阴影效果 四川地图为例
  20. 新微商优品新Mac协议 微商大精灵软件

热门文章

  1. 5道题,教你参破滑动窗口的解法
  2. 证券公司需在主办存管银行开立法人资金交收过渡账户用
  3. Java中Iterator类的详细介绍
  4. vivo 容器集群监控系统架构与实践
  5. Entity Framework — ( Database First )
  6. 中国特色的免费游戏:下流下贱下作!
  7. theme vscode 护眼_vs code 护眼设置
  8. 【torch】torch.pairwise_distance分析
  9. 移动端开发、各种兼容问题、响应式布局开发、移动端和PC端开发的不同
  10. Arduino的串口监视器