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 |
/** |
|
29 |
* Eine Klasse mit Methoden zum entpacken von Dateien |
|
30 |
* |
|
31 |
* @author Ulrich Hilger, 15. Januar 2024 |
|
32 |
*/ |
|
33 |
public class Inflator { |
|
34 |
|
|
35 |
/* --------- ZIP entpacken ---------------- */ |
|
36 |
|
|
37 |
public String extractZipfile(String fName, String relPath, String base) { |
|
38 |
//logger.fine("fName: " + fName + ", relPath: " + relPath); |
|
39 |
String result = null; |
|
40 |
if (!relPath.startsWith(".")) { |
|
41 |
try { |
|
42 |
//File targetDir = new File(fileBase, relPath); |
|
43 |
//File targetDir = getTargetDir(relPath); |
|
44 |
File archive = new File(base, fName); |
|
45 |
if (extract(archive)) { |
|
46 |
result = "ok"; |
|
47 |
} else { |
|
48 |
result = "error while extracting"; |
|
49 |
} |
|
50 |
} catch (Exception ex) { |
|
51 |
result = ex.getLocalizedMessage(); |
|
52 |
//logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
53 |
} |
|
54 |
} else { |
|
55 |
result = "Falsche relative Pfadangabe."; |
|
56 |
} |
|
57 |
return result; |
|
58 |
} |
|
59 |
|
|
60 |
/** |
|
61 |
* extract a given ZIP archive to the folder respective archive resides in |
|
62 |
* |
|
63 |
* @param archive the archive to extract |
|
64 |
* @throws Exception |
|
65 |
*/ |
|
66 |
private boolean extract(File archive) throws Exception { |
|
67 |
ZipFile zipfile = new ZipFile(archive); |
|
68 |
Enumeration en = zipfile.entries(); |
|
69 |
while (en.hasMoreElements()) { |
|
70 |
ZipEntry zipentry = (ZipEntry) en.nextElement(); |
|
71 |
unzip(zipfile, zipentry, archive.getParent()); |
|
72 |
} |
|
73 |
zipfile.close(); |
|
74 |
return true; |
|
75 |
} |
|
76 |
|
|
77 |
/** |
|
78 |
* unzip a given entry of a given zip file to a given location |
|
79 |
* |
|
80 |
* @param zipfile the zip file to read an entry from |
|
81 |
* @param zipentry the zip entry to read |
|
82 |
* @param destPath the path to the destination location for the extracted content |
|
83 |
* @throws IOException |
|
84 |
*/ |
|
85 |
private void unzip(ZipFile zipfile, ZipEntry zipentry, String destPath) throws IOException { |
|
86 |
byte buf[] = new byte[1024]; |
|
87 |
InputStream is = zipfile.getInputStream(zipentry); |
|
88 |
String outFileName = destPath + File.separator + zipentry.getName(); |
|
89 |
File file = new File(outFileName); |
|
90 |
if (!zipentry.isDirectory()) { |
|
91 |
file.getParentFile().mkdirs(); |
|
92 |
if (!file.exists()) { |
|
93 |
file.createNewFile(); |
|
94 |
} |
|
95 |
FileOutputStream fos = new FileOutputStream(file); |
|
96 |
int i = is.read(buf, 0, 1024); |
|
97 |
while (i > -1) { |
|
98 |
fos.write(buf, 0, i); |
|
99 |
i = is.read(buf, 0, 1024); |
|
100 |
} |
|
101 |
fos.close(); |
|
102 |
is.close(); |
|
103 |
} else { |
|
104 |
file.mkdirs(); |
|
105 |
} |
|
106 |
} |
|
107 |
|
|
108 |
} |