commit | author | age
|
e369b9
|
1 |
/* |
c45b52
|
2 |
fm - File management class library |
e369b9
|
3 |
Copyright (C) 2024 Ulrich Hilger |
U |
4 |
|
|
5 |
This program is free software: you can redistribute it and/or modify |
|
6 |
it under the terms of the GNU Affero General Public License as |
|
7 |
published by the Free Software Foundation, either version 3 of the |
|
8 |
License, or (at your option) any later version. |
|
9 |
|
|
10 |
This program is distributed in the hope that it will be useful, |
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
GNU Affero General Public License for more details. |
|
14 |
|
|
15 |
You should have received a copy of the GNU Affero General Public License |
|
16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
17 |
*/ |
|
18 |
package de.uhilger.fm; |
|
19 |
|
|
20 |
import java.io.File; |
|
21 |
import java.io.FileOutputStream; |
|
22 |
import java.io.IOException; |
|
23 |
import java.io.InputStream; |
|
24 |
import java.util.Enumeration; |
|
25 |
import java.util.zip.ZipEntry; |
|
26 |
import java.util.zip.ZipFile; |
|
27 |
|
|
28 |
/** |
7e5166
|
29 |
* Entpacken von Dateien |
e369b9
|
30 |
* |
U |
31 |
* @author Ulrich Hilger, 15. Januar 2024 |
|
32 |
*/ |
|
33 |
public class Inflator { |
|
34 |
|
7e5166
|
35 |
/** |
U |
36 |
* Eine Zip-Datei entpacken |
|
37 |
* |
|
38 |
* @param fName Name der Zip-Datei |
|
39 |
* @param base absoluter Pfad des Ablageortes der Zip-Datei |
|
40 |
* @return 'ok', wenn erfolgreich oder Fehlermeldung, wenn nicht |
|
41 |
*/ |
|
42 |
public String extractZipfile(String fName, /*String relPath, */String base) { |
e369b9
|
43 |
//logger.fine("fName: " + fName + ", relPath: " + relPath); |
U |
44 |
String result = null; |
7e5166
|
45 |
// if (!relPath.startsWith(".")) { |
e369b9
|
46 |
try { |
U |
47 |
//File targetDir = new File(fileBase, relPath); |
|
48 |
//File targetDir = getTargetDir(relPath); |
|
49 |
File archive = new File(base, fName); |
|
50 |
if (extract(archive)) { |
|
51 |
result = "ok"; |
|
52 |
} else { |
|
53 |
result = "error while extracting"; |
|
54 |
} |
|
55 |
} catch (Exception ex) { |
|
56 |
result = ex.getLocalizedMessage(); |
|
57 |
//logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
58 |
} |
7e5166
|
59 |
// } else { |
U |
60 |
// result = "Falsche relative Pfadangabe."; |
|
61 |
// } |
e369b9
|
62 |
return result; |
U |
63 |
} |
|
64 |
|
|
65 |
/** |
7e5166
|
66 |
* Eine Datei in den Ordner entpacken, in dem sie liegt |
e369b9
|
67 |
* |
7e5166
|
68 |
* @param archive die zu entpackende Zip-Datei |
U |
69 |
* @throws Exception wenn etwas schief geht |
e369b9
|
70 |
*/ |
U |
71 |
private boolean extract(File archive) throws Exception { |
|
72 |
ZipFile zipfile = new ZipFile(archive); |
|
73 |
Enumeration en = zipfile.entries(); |
|
74 |
while (en.hasMoreElements()) { |
|
75 |
ZipEntry zipentry = (ZipEntry) en.nextElement(); |
|
76 |
unzip(zipfile, zipentry, archive.getParent()); |
|
77 |
} |
|
78 |
zipfile.close(); |
|
79 |
return true; |
|
80 |
} |
|
81 |
|
|
82 |
/** |
|
83 |
* unzip a given entry of a given zip file to a given location |
7e5166
|
84 |
* Einen Eintrag in einer ZIP-Datei an einen gegebenen Ausgabeort extrahieren |
e369b9
|
85 |
* |
7e5166
|
86 |
* @param zipfile die Zip-Datei, aus der ein zu entpackender Eintrag gelesen werden soll |
U |
87 |
* @param zipentry der Eintrag, der entpackt werden soll |
|
88 |
* @param destPath der Pfad zum Ausgabeort |
|
89 |
* @throws IOException wenn etwas schief geht |
e369b9
|
90 |
*/ |
U |
91 |
private void unzip(ZipFile zipfile, ZipEntry zipentry, String destPath) throws IOException { |
|
92 |
byte buf[] = new byte[1024]; |
|
93 |
InputStream is = zipfile.getInputStream(zipentry); |
|
94 |
String outFileName = destPath + File.separator + zipentry.getName(); |
|
95 |
File file = new File(outFileName); |
|
96 |
if (!zipentry.isDirectory()) { |
|
97 |
file.getParentFile().mkdirs(); |
|
98 |
if (!file.exists()) { |
|
99 |
file.createNewFile(); |
|
100 |
} |
|
101 |
FileOutputStream fos = new FileOutputStream(file); |
|
102 |
int i = is.read(buf, 0, 1024); |
|
103 |
while (i > -1) { |
|
104 |
fos.write(buf, 0, i); |
|
105 |
i = is.read(buf, 0, 1024); |
|
106 |
} |
|
107 |
fos.close(); |
|
108 |
is.close(); |
|
109 |
} else { |
|
110 |
file.mkdirs(); |
|
111 |
} |
|
112 |
} |
|
113 |
|
|
114 |
} |