Archiv mit Unterordnern entzippen

  • Antworten:1
Ludwig H.
  • Forum-Beiträge: 3.256

14.06.2011, 12:07:01 via Website

Hi,

falls es mal jemand braucht, hier ein CodeSnippet um Archive (mit Unterordnern) zu entzippen:
1import java.io.BufferedInputStream;
2import java.io.BufferedOutputStream;
3import java.io.File;
4import java.io.FileOutputStream;
5import java.util.Enumeration;
6import java.util.zip.ZipEntry;
7import java.util.zip.ZipFile;
8
9public class ZipArchiveExtractor {
10
11 /**
12 * @param args
13 */
14 public static void main(String[] args) throws Exception {
15 new ZipArchiveExtractor().extractArchive(new File(
16 "s:/tools/eclipse/plugins/com.ibm.icu_3.4.4.1.jar"), new File(
17 "c:/tmp/x"));
18 }
19
20 public void extractArchive(File archive, File destDir) throws Exception {
21 if (!destDir.exists()) {
22 destDir.mkdir();
23 }
24
25 ZipFile zipFile = new ZipFile(archive);
26 Enumeration entries = zipFile.entries();
27
28 byte[] buffer = new byte[16384];
29 int len;
30 while (entries.hasMoreElements()) {
31 ZipEntry entry = (ZipEntry) entries.nextElement();
32
33 String entryFileName = entry.getName();
34
35 File dir = dir = buildDirectoryHierarchyFor(entryFileName, destDir);
36 if (!dir.exists()) {
37 dir.mkdirs();
38 }
39
40 if (!entry.isDirectory()) {
41 BufferedOutputStream bos = new BufferedOutputStream(
42 new FileOutputStream(new File(destDir, entryFileName)));
43
44 BufferedInputStream bis = new BufferedInputStream(zipFile
45 .getInputStream(entry));
46
47 while ((len = bis.read(buffer)) > 0) {
48 bos.write(buffer, 0, len);
49 }
50
51 bos.flush();
52 bos.close();
53 bis.close();
54 }
55 }
56 zipFile.close();
57 }
58
59 private File buildDirectoryHierarchyFor(String entryName, File destDir) {
60 int lastIndex = entryName.lastIndexOf('/');
61 String entryFileName = entryName.substring(lastIndex + 1);
62 String internalPathToEntry = entryName.substring(0, lastIndex + 1);
63 return new File(destDir, internalPathToEntry);
64 }
65}
Quelle

Lukas Trümper

Antworten
Florian Lindner
  • Forum-Beiträge: 15

05.08.2013, 09:05:37 via Website

Hallo,

welche Libraries hast du dafür verwendet. Ich hatte mal Dateien ge- und entpackt mit zip4j.

VG

Antworten