一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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

服務器之家 - 編程語言 - Java教程 - java ant包中的org.apache.tools.zip實現壓縮和解壓縮實例詳解

java ant包中的org.apache.tools.zip實現壓縮和解壓縮實例詳解

2020-09-13 12:13小俠客-2 Java教程

這篇文章主要介紹了java ant包中的org.apache.tools.zip實現壓縮和解壓縮實例詳解的相關資料,需要的朋友可以參考下

java ant包中的org.apache.tools.zip實現壓縮和解壓縮實例詳解

其實apache中的ant包(請自行GOOGLE之ant.jar)中有一個更好的類,已經支持中文了,我們就不重復制造輪子了,拿來用吧,
這里最主要的功能是實現了 可以指定多個文件 到同一個壓縮包的功能

用org.apache.tools.zip壓縮/解壓縮zip文件的例子,用來解決中文亂碼問題。

實例代碼:

?
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import Java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.Deflater;
 
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
 
/**
 * 功能:使用Apache Ant里提供的org.apache.tools.zip實現zip壓縮和解壓 (支持中文文件名)
 * 解決了由于java.util.zip包不支持漢字的問題。 使用java.util.zip包時,當zip文件中有名字為中文的文件時,
 * 就會出現異常:"Exception in thread "main " java.lang.IllegalArgumentException at
 * java.util.zip.ZipInputStream.getUTF8String(ZipInputStream.java:285)
 *
 * @author 夏明龍 E-mail:郵箱
 * @version 創建時間:2013-3-22 上午10:40:21 類說明:
 */
public class AntZipUtil {
 private static List list = new ArrayList();
 
 private static List listFile(String path) {
 File file = new File(path);
 String[] array = null;
 String sTemp = "";
 
 if (!file.isDirectory()) {
  return null;
 }
 array = file.list();
 if (array.length > 0) {
  for (int i = 0; i < array.length; i++) {
  sTemp = path + array[i];
  file = new File(sTemp);
  if (file.isDirectory()) {
   listFile(sTemp + "/");
  } else
   list.add(sTemp);
  }
 } else {
  return null;
 }
 
 return list;
 }
 
 public static void zip(String needtozipfilepath, String zipfilepath){
 try {
  byte[] b = new byte[512];
 
  File needtozipfile = new File(needtozipfilepath);
 
  if (!needtozipfile.exists()) {
  System.err.println("指定的要壓縮的文件或目錄不存在.");
  return;
  }
 
  String zipFile = zipfilepath;
  File targetFile = new File(zipFile.substring(0, zipFile.indexOf("\\") + 1));
 
  if (!targetFile.exists()) {
  System.out.println("指定的目標文件或目錄不存在.");
  return;
  }
 
  String filepath = needtozipfilepath;
  List fileList = listFile(filepath);
  FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
  CheckedOutputStream cs = new CheckedOutputStream(fileOutputStream,new CRC32());
  ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(cs));
 
  for (int i = 0; i < fileList.size(); i++) {
  InputStream in = new FileInputStream((String) fileList.get(i));
  String fileName = ((String) fileList.get(i)).replace(File.separatorChar, '/');
  fileName = fileName.substring(fileName.indexOf("/") + 1);
  ZipEntry e = new ZipEntry(fileName);
  out.putNextEntry(e);
  int len = 0;
  while ((len = in.read(b)) != -1) {
   out.write(b, 0, len);
  }
  out.closeEntry();
  }
  out.close();
 } catch (Exception e) {
  e.printStackTrace();
 }
 }
 
 // ///////////////////////////////////////
 /**
 * 壓縮文件 或者 文件夾
 *
 * @param baseDirName
 *      壓縮的根目錄
 * @param fileName
 *      根目錄下待壓縮的文件或文件夾名
 * @param targetFileName
 *      目標ZIP 文件 星號 "*" 表示壓縮根目錄下的全部文件
 *
 */
 public static boolean zip(String baseDirName, String[] fileNames,
  String targetFileName, String encoding) {
 boolean flag = false;
 try {
  // 判斷 "壓縮的根目錄"是否存在! 是否是一個文件夾!
  File baseDir = new File(baseDirName);
  if (!baseDir.exists() || (!baseDir.isDirectory())) {
  System.err.println("壓縮失敗! 根目錄不存在: " + baseDirName);
  return false;
  }
 
  // 得到這個 "壓縮的根目錄" 的絕對路徑
  String baseDirPath = baseDir.getAbsolutePath();
 
  // 由這個 "目標 ZIP 文件" 文件名得到一個 壓縮對象 ZipOutputStream
  File targetFile = new File(targetFileName);
  ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
   targetFile));
  // 中文有亂碼,引進下面的改造類
  // CnZipOutputStream out = new CnZipOutputStream(new
  // FileOutputStream(targetFile),encoding);
 
  // 設置壓縮編碼Apache Ant有個包專門處理ZIP文件,可以指定文件名的編碼方式。由此可以解決問題。例如:用
  // org.apache.tools.zip.ZipOutputStream代替java.util.zip.ZipOutputStream。ZipOutputStream
  // out = .....; out.setEncoding("GBK");
  // out.setEncoding("GBK");//設置為GBK后在windows下就不會亂碼了,如果要放到Linux或者Unix下就不要設置了
  out.setEncoding(encoding);
 
  // "*" 表示壓縮包括根目錄 baseDirName 在內的全部文件 到 targetFileName文件下
  if (fileNames.equals("*")) {
  AntZipUtil.dirToZip(baseDirPath, baseDir, out);
  } else {
  File[] files = new File[fileNames.length];
  for (int i = 0; i < files.length; i++) {
   // 根據 parent 抽象路徑名和 child 路徑名字符串創建一個新 File 實例。
   files[i] = new File(baseDir, fileNames[i]);
  }
  if (files[0].isFile()) {
   // 調用本類的一個靜態方法 壓縮一個文件
   // CompressUtil.fileToZip(baseDirPath, file, out);
   AntZipUtil.filesToZip(baseDirPath, files, out);
  }
 
  }
  out.close();
  // System.out.println("壓縮成功! 目標文件名為: " + targetFileName);
  flag = true;
 } catch (FileNotFoundException e) {
  e.printStackTrace();
 } catch (IOException e) {
  e.printStackTrace();
 }
 return flag;
 }
 
 /**
 * 將文件壓縮到Zip 輸出流
 *
 * @param baseDirPath
 *      根目錄路徑
 * @param file
 *      要壓縮的文件
 * @param out
 *      輸出流
 * @throws IOException
 */
 private static void fileToZip(String baseDirPath, File file,
  ZipOutputStream out) throws IOException {
 //
 FileInputStream in = null;
 org.apache.tools.zip.ZipEntry entry = null;
 // 創建復制緩沖區 1024*4 = 4K
 byte[] buffer = new byte[1024 * 4];
 int bytes_read = 0;
 if (file.isFile()) {
  in = new FileInputStream(file);
  // 根據 parent 路徑名字符串和 child 路徑名字符串創建一個新 File 實例
  String zipFileName = getEntryName(baseDirPath, file);
  entry = new org.apache.tools.zip.ZipEntry(zipFileName);
  // "壓縮文件" 對象加入 "要壓縮的文件" 對象
  out.putNextEntry(entry);
  // 現在是把 "要壓縮的文件" 對象中的內容寫入到 "壓縮文件" 對象
  while ((bytes_read = in.read(buffer)) != -1) {
  out.write(buffer, 0, bytes_read);
  }
  out.closeEntry();
  in.close();
  // System.out.println("添加文件" + file.getAbsolutePath()+ "被添加到 ZIP
  // 文件中!");
 }
 }
 
 /**
 * 多個文件目錄壓縮到Zip 輸出流
 *
 * @param baseDirPath
 * @param files
 * @param out
 * @throws IOException
 */
 @SuppressWarnings("unused")
 private static void filesToZip(String baseDirPath, File[] files,
  ZipOutputStream out) throws IOException {
 // 遍歷所有的文件 一個一個地壓縮
 for (int i = 0; i < files.length; i++) {
  File file = files[i];
  if (file.isFile()) {
  // 調用本類的一個靜態方法 壓縮一個文件
  AntZipUtil.fileToZip(baseDirPath, file, out);
  } else {
  /*
   * 這是一個文件夾 所以要再次得到它下面的所有的文件 這里是自己調用自己..............遞歸..........
   */
  AntZipUtil.dirToZip(baseDirPath, file, out);
  }
 }
 }
 
 /**
 * 將文件目錄壓縮到Zip 輸出流
 *
 * @param baseDirPath
 * @param dir
 * @param out
 * @throws IOException
 */
 private static void dirToZip(String baseDirPath, File dir,
  ZipOutputStream out) throws IOException {
 // 得到一個文件列表 (本目錄下的所有文件對象集合)
 File[] files = dir.listFiles();
 // 要是這個文件集合數組的長度為 0 , 也就證明了這是一個空的文件夾,雖然沒有再循環遍歷它的必要,但是也要把這個空文件夾也壓縮到目標文件中去
 if (files.length == 0) {
  // 根據 parent 路徑名字符串和 child 路徑名字符串創建一個新 File 實例
  String zipFileName = getEntryName(baseDirPath, dir);
  org.apache.tools.zip.ZipEntry entry = new org.apache.tools.zip.ZipEntry(
   zipFileName);
  out.putNextEntry(entry);
  out.closeEntry();
 } else {
  // 遍歷所有的文件 一個一個地壓縮
  for (int i = 0; i < files.length; i++) {
  File file = files[i];
  if (file.isFile()) {
   // 調用本類的一個靜態方法 壓縮一個文件
   AntZipUtil.fileToZip(baseDirPath, file, out);
  } else {
   /*
   * 這是一個文件夾 所以要再次得到它下面的所有的文件
   * 這里是自己調用自己..............遞歸..........
   */
   AntZipUtil.dirToZip(baseDirPath, file, out);
  }
  }
 }
 }
 
 /**
 * 獲取 待壓縮文件在 ZIP 文件中的 entry的名字,即相對于根目錄的相對路徑名
 *
 * @param baseDirPath
 *      根目錄
 * @param file
 * @return
 */
 private static String getEntryName(String baseDirPath, File file) {
 /**
  * 改變 baseDirPath 的形式 把 "C:/temp" 變成 "C:/temp/"
  */
 if (!baseDirPath.endsWith(File.separator)) {
  baseDirPath += File.separator;
 }
 String filePath = file.getAbsolutePath();
 /**
  * 測試此抽象路徑名表示的文件是否是一個目錄。 要是這個文件對象是一個目錄 則也要變成 后面帶 "/" 這個文件對象類似于
  * "C:/temp/人體寫真/1.jpg" 要是這個文件是一個文件夾 則也要變成 后面帶 "/"
  * 因為你要是不這樣做,它也會被壓縮到目標文件中 但是卻不能正解顯示 也就是說操作系統不能正確識別它的文件類型(是文件還是文件夾)
  */
 if (file.isDirectory()) {
  filePath += "/";
 }
 int index = filePath.indexOf(baseDirPath);
 return filePath.substring(index + baseDirPath.length());
 }
 
 // //////////////////////////解壓縮////////////////////////////////////////
 /**
 * 調用org.apache.tools.zip實現解壓縮,支持目錄嵌套和中文名
 * 也可以使用java.util.zip不過如果是中文的話,解壓縮的時候文件名字會是亂碼。原因是解壓縮軟件的編碼格式跟java.util.zip.ZipInputStream的編碼字符集(固定是UTF-8)不同
 *
 * @param zipFileName
 *      要解壓縮的文件
 * @param outputDirectory
 *      要解壓到的目錄
 * @throws Exception
 */
 public static boolean unZip(String zipFileName, String outputDirectory) {
 boolean flag = false;
 try {
  org.apache.tools.zip.ZipFile zipFile = new org.apache.tools.zip.ZipFile(
   zipFileName);
  java.util.Enumeration e = zipFile.getEntries();
  org.apache.tools.zip.ZipEntry zipEntry = null;
  createDirectory(outputDirectory, "");
  while (e.hasMoreElements()) {
  zipEntry = (org.apache.tools.zip.ZipEntry) e.nextElement();
  // System.out.println("unziping " + zipEntry.getName());
  if (zipEntry.isDirectory()) {
   String name = zipEntry.getName();
   name = name.substring(0, name.length() - 1);
   File f = new File(outputDirectory + File.separator + name);
   f.mkdir();
   System.out.println("創建目錄:" + outputDirectory
    + File.separator + name);
  } else {
   String fileName = zipEntry.getName();
   fileName = fileName.replace('\\', '/');
   // System.out.println("測試文件1:" +fileName);
   if (fileName.indexOf("/") != -1) {
   createDirectory(outputDirectory, fileName.substring(0,
    fileName.lastIndexOf("/")));
   fileName = fileName.substring(
    fileName.lastIndexOf("/") + 1, fileName
     .length());
   }
 
   File f = new File(outputDirectory + File.separator
    + zipEntry.getName());
 
   f.createNewFile();
   InputStream in = zipFile.getInputStream(zipEntry);
   FileOutputStream out = new FileOutputStream(f);
 
   byte[] by = new byte[1024];
   int c;
   while ((c = in.read(by)) != -1) {
   out.write(by, 0, c);
   }
   out.close();
   in.close();
  }
  flag = true;
  }
 } catch (Exception ex) {
  ex.printStackTrace();
 }
 return flag;
 }
 
 /**
 * 創建目錄
 *
 * @param directory
 *      父目錄
 * @param subDirectory
 *      子目錄
 */
 private static void createDirectory(String directory, String subDirectory) {
 String dir[];
 File fl = new File(directory);
 try {
  if (subDirectory == "" && fl.exists() != true)
  fl.mkdir();
  else if (subDirectory != "") {
  dir = subDirectory.replace('\\', '/').split("/");
  for (int i = 0; i < dir.length; i++) {
   File subFile = new File(directory + File.separator + dir[i]);
   if (subFile.exists() == false)
   subFile.mkdir();
   directory += File.separator + dir[i];
  }
  }
 } catch (Exception ex) {
  System.out.println(ex.getMessage());
 }
 }
 
 // /////////////////////////////////////
 
 public static void main(String[] temp) {
 // 壓縮
 String baseDirName = "C:\\";
 String[] fileNames = { "中文1.doc", "中文2.doc" };
 String zipFileName = "c:\\中文.zip";
 // 壓縮多個指定的文件 到ZIP
  System.out.println(AntZipUtil.zip(baseDirName, fileNames,zipFileName,"GBK"));
 
 //壓縮一個文件夾 到ZIP
 String sourcePath = "c:\\test\\";
 String zipFilePath = "c:\\中文2.zip";
 AntZipUtil.zip(sourcePath, zipFilePath);
 
 //解壓縮
 //System.out.println(AntZipUtil.unZip("c:\\中文.zip", "c:\\中文"));
 }
 
}

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

原文鏈接:http://blog.csdn.net/qq_26562641/article/details/51487920

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲国产货青视觉盛宴 | 日本小视频网站 | 国产91影院 | 婷婷久久综合九色综合九七 | 99视频福利 | 免费观看成年肉动漫网站 | 女人把扒开给男人爽的 | 国产传媒在线播放 | 99久久免费国产精品热 | 久久国产乱子伦精品免费不卡 | 四虎国产精品免费入口 | 亚洲精品97福利在线 | 色哟哟哟| 亚洲国产视频网站 | 91日本 | 黄a在线观看 | 18美女光胸光屁屁洗澡 | 亚洲精品久久久久69影院 | 亚洲高清中文字幕精品不卡 | japanese在线看 | 成人在线视频国产 | 毛片www| 日本一区二区免费在线观看 | 特级老女人淫片高清视频 | 国产美女亚洲精品久久久综合 | 韩国三级做爰 | 久久青青草视频在线观 | 99热这里只有精品久久免费 | 日本免费在线观看视频 | 九色PORNY真实丨国产大胸 | xxxx18日本视频xxxxx | 欧美sq| 日本动漫黄网站在线观看 | 特大黑人娇小亚洲女mp4 | 久久99re热在线播放7 | 插入粉嫩| 青青热久久综合网伊人 | 亚洲一卡2卡三卡4卡5卡组 | 99热视| 变态 另类 人妖小说 | 69日本xxxx|