comparison artifacts-common/src/main/java/de/intevation/artifacts/common/utils/FileTools.java @ 383:b076c9e9fdfd

Added method to extract zip archives to a specified directory. artifacts/trunk@4293 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Raimund Renkert <raimund.renkert@intevation.de>
date Wed, 25 Apr 2012 07:47:56 +0000
parents c489256a188b
children c40729bfe06d
comparison
equal deleted inserted replaced
382:a94bc2491b41 383:b076c9e9fdfd
12 import java.io.IOException; 12 import java.io.IOException;
13 import java.io.InputStream; 13 import java.io.InputStream;
14 import java.io.FileInputStream; 14 import java.io.FileInputStream;
15 import java.io.FileOutputStream; 15 import java.io.FileOutputStream;
16 import java.io.OutputStream; 16 import java.io.OutputStream;
17 import java.io.BufferedOutputStream;
17 import java.nio.channels.FileChannel; 18 import java.nio.channels.FileChannel;
18 19
19 import java.util.Deque; 20 import java.util.Deque;
20 import java.util.ArrayDeque; 21 import java.util.ArrayDeque;
21 import java.util.List; 22 import java.util.List;
22 import java.util.Set; 23 import java.util.Set;
23 import java.util.HashSet; 24 import java.util.HashSet;
24 import java.util.ArrayList; 25 import java.util.ArrayList;
26 import java.util.Enumeration;
27 import java.util.zip.ZipFile;
25 import java.util.zip.ZipEntry; 28 import java.util.zip.ZipEntry;
26 import java.util.zip.ZipOutputStream; 29 import java.util.zip.ZipOutputStream;
27 30
28 import org.apache.log4j.Logger; 31 import org.apache.log4j.Logger;
29 32
343 } 346 }
344 } 347 }
345 } 348 }
346 349
347 out.finish(); 350 out.finish();
351 }
352
353
354 public static void extractArchive(File archive, File destDir)
355 throws IOException {
356 if (!destDir.exists()) {
357 destDir.mkdir();
358 }
359
360 ZipFile zipFile = new ZipFile(archive);
361 Enumeration entries = zipFile.entries();
362
363 byte[] buffer = new byte[16384];
364 int len;
365 while (entries.hasMoreElements()) {
366 ZipEntry entry = (ZipEntry) entries.nextElement();
367
368 String entryFileName = entry.getName();
369
370 File dir = dir = buildDirectoryHierarchyFor(entryFileName, destDir);
371 if (!dir.exists()) {
372 dir.mkdirs();
373 }
374
375 if (!entry.isDirectory()) {
376 BufferedOutputStream bos = new BufferedOutputStream(
377 new FileOutputStream(new File(destDir, entryFileName)));
378
379 BufferedInputStream bis = new BufferedInputStream(zipFile
380 .getInputStream(entry));
381
382 while ((len = bis.read(buffer)) > 0) {
383 bos.write(buffer, 0, len);
384 }
385
386 bos.flush();
387 bos.close();
388 bis.close();
389 }
390 }
391 zipFile.close();
392 }
393
394 private static File buildDirectoryHierarchyFor(
395 String entryName,
396 File destDir)
397 {
398 int lastIndex = entryName.lastIndexOf('/');
399 String entryFileName = entryName.substring(lastIndex + 1);
400 String internalPathToEntry = entryName.substring(0, lastIndex + 1);
401 return new File(destDir, internalPathToEntry);
348 } 402 }
349 403
350 /** 404 /**
351 * A class representing a directory with a prefix. 405 * A class representing a directory with a prefix.
352 */ 406 */

http://dive4elements.wald.intevation.org