激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - 如何基于java實(shí)現(xiàn)解壓ZIP TAR等文件

如何基于java實(shí)現(xiàn)解壓ZIP TAR等文件

2020-07-29 13:45張志勇- Java教程

這篇文章主要介紹了如何基于java實(shí)現(xiàn)解壓ZIP TAR等文件,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

  java實(shí)現(xiàn)對(duì)常用.ZIP , .TAR, .TAR.BZ2, .BZ2 ,.TAR.GZ ,.GZ格式文件的解壓。

  首先需要引入maven依賴,這里使用的是Apache的壓縮工具包c(diǎn)ommon-compress,改工具包支持解壓、壓縮,此代碼中我列舉出一個(gè)zip的壓縮示例,其他格式的只需切換改格式對(duì)應(yīng)的流即可。

對(duì)于RAR格式文件的解壓,目前該工具包還不支持,希望大家做過的可以多多交流。

?
1
2
3
4
<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-compress</artifactId>
  <version>1.19</version></dependency>
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
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實(shí)現(xiàn)常見文件格式的解壓與壓縮
 *         支持.ZIP .TAR .TAR.BZ2 .BZ2 .TAR.GZ .GZ
 *         其他格式compress包也支持,在此基礎(chǔ)上開發(fā)即可
 *         另外壓縮文件只寫了ZIP壓縮的方法zipCompression,其他的格式類似,換成對(duì)應(yīng)的文件流即可。
 *   暫不支持RAR壓縮格式,RAR可以用junrar的工具包,但是有缺陷:
 *   其一:如果壓縮文件中有中文名字的文件夾,解壓以后文件夾名字是亂碼,但是不影響文件夾里面的文件;
 *   其二:最新 WinRar 壓縮產(chǎn)生的 .rar 文件可能會(huì)無法解壓。
 *   缺陷原因:rar 有版權(quán),有些東西沒有公開,對(duì)解壓有一些限制,即使其他解壓包也可能有問題,但是建議嘗試。
 * @date :2020/7/1 20:44
 */
public class CompressionFileUtil {
  /**
   * @param filePath 需要解壓的zip文件的完成路徑。
   * @param unzipPath 解壓過后生成文件的存放路徑
   * @description: 對(duì)zip文件進(jìn)行解壓。
   * @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());
          //自動(dòng)添加File.separator文件路徑的分隔符,根據(jù)系統(tǒng)判斷是\\還是/
          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 多個(gè)文件的絕對(duì)路徑,是一個(gè)數(shù)組。
   * @param zipFilePath  生成的壓縮文件的位置,包括生成的文件名,如D:\zip\test.zip
   * @description: 將多個(gè)文件壓縮成ZIP壓縮包。
   * @return: boolean
   * @author: ZZY
   * @time: 2020/7/2 14:42
   */
  public static boolean zipCompression(String[] filesPathArray, String zipFilePath) throws Exception {
    System.out.println("開始?jí)嚎sZIP文件");
    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);
          // 第二個(gè)參數(shù)如果是文件全路徑名,那么壓縮時(shí)也會(huì)將路徑文件夾也縮進(jìn)去;
          // 我們只壓縮目標(biāo)文件,而不壓縮該文件所處位置的相關(guān)文件夾,所以這里我們用file.getName()
          System.out.println("開始?jí)嚎s..." + 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");
 
  }
 
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://www.cnblogs.com/zhangzhiyong-/p/13233920.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美成年人视频 | 中文字幕www.| 中国av免费在线观看 | 亚洲国产资源 | 亚欧在线免费观看 | 红桃一区 | 国产无遮挡一区二区三区毛片日本 | 中国国语毛片免费观看视频 | 4480午夜| 国产无区一区二区三麻豆 | 99国产精品欲a | 狠狠操夜夜爱 | 91高清完整版在线观看 | 毛片大全| 免费久久久久 | 一本色道久久综合狠狠躁篇适合什么人看 | 九九热视频免费 | 毛片免费视频网站 | 午夜激情视频免费 | 成人亚洲 | 久久久久久久久久综合 | 欧美精品久久久久久久久久 | 日韩男人的天堂 | 蜜桃一本色道久久综合亚洲精品冫 | 久久久久久久久久久久久九 | 黄色免费小视频网站 | 久久免费视频5 | 成人久久久精品乱码一区二区三区 | 久久久久一本一区二区青青蜜月 | 一级电影免费 | 成人在线观看一区二区三区 | 成人在线观看免费爱爱 | 欧美一级毛片大片免费播放 | 热99在线视频 | 日产精品久久久一区二区福利 | 黄色免费网站在线播放 | 欧美黄色大片免费观看 | 中午字幕无线码一区2020 | av老司机久久 | 日韩精品中文字幕在线播放 | 国产在线中文 |