JAVA处理tar.gz结尾的压缩文件 的解决方案

最近接到一个需求,做个进程,涉及去某台服务器上面取tar.gz压缩文件,然后进行解压。

奈何自己没搞过类似的需求。自己很懵逼。最后一番波折写出以下代码。

谨做个人笔记。

知识前提:

1、tar.gz为后缀的文件是一种压缩文件,在Linux和macOS下常见,Linux和macOS都可以直接解压使用这种压缩文件。但是我们是要从Java!扯什么Linux和macOS!!!!

2、自测代码,可以通过自己压缩一个tar.gz压缩包。压缩工具推荐7-zip。先tar压缩,再gzip压缩。

  

 

3、先使用GZIPInputStream读取文件(为什么要先用GZIPInputStream,建议参考tar.gz文件是怎么生成,个人理解。)。生成文件,再进行使用FileInputStreamFileOutputStream文件流进行读文件和写文件。

4、类TarEntry

   隶属于 Apcache org.apache.tools.tar 包的一个类。

   这个类TarEntry表示Tar归档文件中的条目。它由条目的标题和条目的File组成。

   从存档中读取的标头字节创建的TarEntries使用TarEntry(byte [])构造函数实例化。从存档内容中提取或列出存档内容时,将使用这些条目。

   TarEntries只能由名称构成。这使程序员可以手动构造条目。

 

一,处理解tar.gz压缩包.主要方法。

 1 /**
 2      * 处理压缩包并获取文件Name
 3      * @param fileName
 4      * @return
 5      */
 6     private List<String> processSingleZipFile(String zipFileName) throws Exception {
 7         if(log.isInfoEnabled()){
 8             log.info("开始解压["+zipFileName+"]*************");
 9         }
10         List<String> retFiles=null;
11         try {
12             String tarFile = zipFileName.substring(0, zipFileName.indexOf(".gz"));
13             this.unZip(this.localPath + File.separator + zipFileName, this.localPath + File.separator + tarFile);
14              retFiles = this.unTar(this.localPath + File.separator + tarFile, this.localPath + File.separator);
15             // move to his
16             File gzFile = new File(this.localPath+ File.separator + zipFileName);
17             File gzHisFile = new File(this.localPath + File.separator + zipFileName);
18             gzFile.renameTo(gzHisFile);
19             gzFile.delete();//把解压包移到HIS目录
20         } catch (Exception e) {
21             if(log.isErrorEnabled()){
22                 log.error("解压文件"+zipFileName+"失败!", e);
23             }
24         }
25         return retFiles;
26     }
View Code

 

二、逻辑代码一。

 1 /**
 2      * 读取压缩包
 3      *
 4      * @param zipFile
 5      * @param destFile
 6      * @throws Exception
 7      */
 8     protected void unZip(String zipFile, String destFile) throws Exception {
 9         File file = new File(zipFile);
10         FileInputStream fin = new FileInputStream(file);
11         GZIPInputStream gzis = new GZIPInputStream(fin);
12         File zfile = new File(destFile);
13         FileOutputStream fout = new FileOutputStream(zfile);
14         byte[] buf = new byte[1024];
15         int number = gzis.read(buf, 0, buf.length);
16         while (number != -1) {
17             fout.write(buf, 0, number);
18             number = gzis.read(buf, 0, buf.length);
19         }
20         close(gzis);
21         close(fout);
22         close(fin);
23 
24     }
View Code

 

三,逻辑代码二。

 1 /**
 2      * 解压压缩包
 3      *
 4      * @param zipFile
 5      * @param destFile
 6      * @throws Exception
 7      */
 8     protected List<String> unTar(String tarFile, String destDir) throws Exception
 9     {
10 
11         List<String> retFiles = new ArrayList<>();
12         InputStream inputstream = null;
13         OutputStream outputstream = null;
14         TarInputStream zis = null;
15         File file = null;
16         try {
17             file = new File(tarFile);
18             inputstream = new FileInputStream(file);
19             zis = new TarInputStream(inputstream);
20             TarEntry tarEntry;
21             while ((tarEntry = zis.getNextEntry()) != null) {
22                 retFiles.add(tarEntry.getName()); //添加解压缩之后的文件名,类的头目
23                 File tempFile = new File(destDir + tarEntry.getName());
24                 tempFile.createNewFile(); 
25                 outputstream = new FileOutputStream(tempFile);
26                 byte[] buf = new byte[1024*50];
27                 int readsize = zis.read(buf, 0, buf.length);
28                 while (readsize != -1) {
29                     outputstream.write(buf, 0, readsize);
30                     readsize = zis.read(buf, 0, buf.length);
31                 }
32                 outputstream.flush();
33                 outputstream.close();
34             }
35         } catch (Exception e) {
36             log.error("error:", e);
37             throw e;
38         } finally {
39             if (null != outputstream) {
40                 outputstream.flush();
41             }
42             if (null != inputstream) {
43                 inputstream.close();
44             }
45             if (null != zis) {
46                 zis.close();
47             }
48             if (null != outputstream) {
49                 outputstream.close();
50             }
51             if (null != file) {
52                 file.delete();
53             }
54         }
55         return retFiles;
56 
57     }
View Code
posted @ 2020-01-13 20:12  Final_static_杨  阅读(1418)  评论(0编辑  收藏  举报