commit | author | age
|
020a97
|
1 |
package de.uhilger.httpserver.cm; |
U |
2 |
|
|
3 |
import java.io.File; |
|
4 |
import java.io.FileOutputStream; |
|
5 |
import java.io.IOException; |
|
6 |
import java.io.InputStream; |
|
7 |
import java.util.Enumeration; |
|
8 |
import java.util.zip.ZipEntry; |
|
9 |
import java.util.zip.ZipFile; |
|
10 |
|
|
11 |
/** |
|
12 |
* |
|
13 |
* @author ulli |
|
14 |
*/ |
|
15 |
public class Unzipper { |
|
16 |
/* --------- ZIP entpacken ---------------- */ |
|
17 |
|
|
18 |
public String extractZipfile(String fName, String relPath, String base) { |
|
19 |
//logger.fine("fName: " + fName + ", relPath: " + relPath); |
|
20 |
String result = null; |
|
21 |
if (!relPath.startsWith(".")) { |
|
22 |
try { |
|
23 |
//File targetDir = new File(fileBase, relPath); |
|
24 |
//File targetDir = getTargetDir(relPath); |
|
25 |
File archive = new File(base, fName); |
|
26 |
if(extract(archive)) { |
|
27 |
result = "ok"; |
|
28 |
} else { |
|
29 |
result = "error while extracting"; |
|
30 |
} |
|
31 |
} catch(Exception ex) { |
|
32 |
result = ex.getLocalizedMessage(); |
|
33 |
//logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
34 |
} |
|
35 |
} else { |
|
36 |
result = "Falsche relative Pfadangabe."; |
|
37 |
} |
|
38 |
return result; |
|
39 |
} |
|
40 |
|
|
41 |
/** |
|
42 |
* extract a given ZIP archive to the folder respective archive resides in |
|
43 |
* @param archive the archive to extract |
|
44 |
* @throws Exception |
|
45 |
*/ |
|
46 |
private boolean extract(File archive) throws Exception { |
|
47 |
ZipFile zipfile = new ZipFile(archive); |
|
48 |
Enumeration en = zipfile.entries(); |
|
49 |
while(en.hasMoreElements()) { |
|
50 |
ZipEntry zipentry = (ZipEntry) en.nextElement(); |
|
51 |
unzip(zipfile, zipentry, archive.getParent()); |
|
52 |
} |
|
53 |
zipfile.close(); |
|
54 |
return true; |
|
55 |
} |
|
56 |
|
|
57 |
/** |
|
58 |
* unzip a given entry of a given zip file to a given location |
|
59 |
* @param zipfile the zip file to read an entry from |
|
60 |
* @param zipentry the zip entry to read |
|
61 |
* @param destPath the path to the destination location for the extracted content |
|
62 |
* @throws IOException |
|
63 |
*/ |
|
64 |
private void unzip(ZipFile zipfile, ZipEntry zipentry, String destPath) throws IOException { |
|
65 |
byte buf[] = new byte[1024]; |
|
66 |
InputStream is = zipfile.getInputStream(zipentry); |
|
67 |
String outFileName = destPath + File.separator + zipentry.getName(); |
|
68 |
File file = new File(outFileName); |
|
69 |
if(!zipentry.isDirectory()) { |
|
70 |
file.getParentFile().mkdirs(); |
|
71 |
if(!file.exists()) |
|
72 |
file.createNewFile(); |
|
73 |
FileOutputStream fos = new FileOutputStream(file); |
|
74 |
int i = is.read(buf, 0, 1024); |
|
75 |
while(i > -1) { |
|
76 |
fos.write(buf, 0, i); |
|
77 |
i = is.read(buf, 0, 1024); |
|
78 |
} |
|
79 |
fos.close(); |
|
80 |
is.close(); |
|
81 |
} else { |
|
82 |
file.mkdirs(); |
|
83 |
} |
|
84 |
} |
|
85 |
|
|
86 |
|
|
87 |
} |