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.FileInputStream; |
|
22 |
import java.io.FileOutputStream; |
|
23 |
import java.io.IOException; |
|
24 |
import java.util.zip.Adler32; |
|
25 |
import java.util.zip.CheckedOutputStream; |
|
26 |
import java.util.zip.ZipEntry; |
|
27 |
import java.util.zip.ZipOutputStream; |
|
28 |
|
|
29 |
/** |
|
30 |
* Eine Klasse mit Methoden zum Packen von Dateien # |
|
31 |
* |
|
32 |
* @author Ulrich Hilger, 15. Januar 2024 |
|
33 |
*/ |
|
34 |
public class Deflator { |
|
35 |
|
|
36 |
/* --------------- Ordner packen ----------------- */ |
|
37 |
/** |
|
38 |
* Einen Ordner packen. |
|
39 |
* |
|
40 |
* Als Ziel wird eine neue Datei mit Dateiendung '.zip' erzeugt, die so heisst wie der Ordner, der |
|
41 |
* gapckt werden soll. Die Datei mit dem gepackten Ordnerinhalt wird in dem Ordner angelegt, der |
|
42 |
* den zu packenden Ordner enthaelt. |
|
43 |
* |
|
44 |
* @param fName Name des zu packenden Ordners |
|
45 |
* @param relPath relativer Pfad zum Ordner, der gepackt werden soll |
|
46 |
* @return die Meldung mit dem Ergebnis. Wenn die Meldung nicht "ok" lautet wurde die ZIP-Datei |
|
47 |
* nicht erzeugt und die Meldung nennt den Grund. |
|
48 |
*/ |
|
49 |
public String packFolder(String fName, String relPath, String base/*, HttpExchange e*/) { |
|
50 |
if (!relPath.startsWith(".")) { |
|
51 |
try { |
|
52 |
//String fName = getFileName(e); |
|
53 |
//logger.fine("fName: " + fName); |
|
54 |
if (fName.endsWith(Const.STR_SLASH)) { |
|
55 |
File dir = new File(base, fName); |
|
56 |
if (dir.isDirectory()) { |
|
57 |
//logger.fine("absPath: " + dir.getAbsolutePath()); |
|
58 |
File parentDir = dir.getParentFile(); |
|
59 |
StringBuilder fname = new StringBuilder(); |
|
60 |
fname.append(dir.getName()); |
|
61 |
fname.append(".zip"); |
|
62 |
File archiveFile = new File(parentDir, fname.toString()); |
|
63 |
pack(dir.getAbsolutePath(), archiveFile.getAbsolutePath()); |
|
64 |
return "ok"; |
|
65 |
} else { |
|
66 |
return "kein Ordner"; |
|
67 |
} |
|
68 |
} else { |
|
69 |
return "kein Ordner"; |
|
70 |
} |
|
71 |
} catch (Exception ex) { |
|
72 |
String result = ex.getLocalizedMessage(); |
|
73 |
//logger.log(Level.SEVERE, result, ex); |
|
74 |
return result; |
|
75 |
} |
|
76 |
} else { |
|
77 |
return "Falsche relative Pfadangabe"; |
|
78 |
} |
|
79 |
} |
|
80 |
|
|
81 |
/** |
|
82 |
* pack the contents of a given folder into a new ZIP compressed archive |
|
83 |
* |
|
84 |
* @param folder absolute path and name of the folder to pack |
|
85 |
* @param archive absolute path and name of the archive to create from the given files |
|
86 |
* @throws Exception |
|
87 |
*/ |
|
88 |
private boolean pack(String folder, String archive) throws Exception { |
|
89 |
File file = new File(archive); |
|
90 |
FileOutputStream fos = new FileOutputStream(file); |
|
91 |
CheckedOutputStream checksum = new CheckedOutputStream(fos, new Adler32()); |
|
92 |
ZipOutputStream zos = new ZipOutputStream(checksum); |
|
93 |
pack(zos, folder, ""); |
|
94 |
zos.flush(); |
|
95 |
zos.finish(); |
|
96 |
zos.close(); |
|
97 |
fos.flush(); |
|
98 |
fos.close(); |
|
99 |
return true; |
|
100 |
} |
|
101 |
|
|
102 |
/** |
|
103 |
* go through the given file structure recursively |
|
104 |
* |
|
105 |
* @param zipFile the ZIP file to write to |
|
106 |
* @param srcDir the directory to pack during this cycle |
|
107 |
* @param subDir the subdirectory to append to names of file entries inside the archive |
|
108 |
* @throws IOException |
|
109 |
*/ |
|
110 |
private void pack(ZipOutputStream zipFile, String srcDir, String subDir) throws IOException { |
|
111 |
File[] files = new File(srcDir).listFiles(); |
|
112 |
for (int i = 0; i < files.length; i++) { |
|
113 |
if (files[i].isDirectory()) { |
|
114 |
pack(zipFile, files[i].getAbsolutePath(), subDir + File.separator + files[i].getName()); |
|
115 |
} else { |
|
116 |
packFile(zipFile, subDir, files[i]); |
|
117 |
} |
|
118 |
} |
|
119 |
} |
|
120 |
|
|
121 |
/** |
|
122 |
* pack a given file |
|
123 |
* |
|
124 |
* @param zipFile the ZIP archive to pack to |
|
125 |
* @param dir the directory name to append to name of file entry inside archive |
|
126 |
* @param file the file to pack |
|
127 |
* @throws IOException |
|
128 |
*/ |
|
129 |
private void packFile(ZipOutputStream zipFile, String dir, File file) throws IOException { |
|
130 |
FileInputStream fileinputstream = new FileInputStream(file); |
|
131 |
byte buf[] = new byte[fileinputstream.available()]; |
|
132 |
fileinputstream.read(buf); |
|
133 |
String dirWithSlashes = dir.replace('\\', '/'); |
|
134 |
//System.out.println("zipping " + dirWithSlashes + "/" + file.getName()); |
|
135 |
ZipEntry ze = new ZipEntry(dirWithSlashes + "/" + file.getName()); |
|
136 |
ze.setMethod(ZipEntry.DEFLATED); |
|
137 |
zipFile.putNextEntry(ze); |
|
138 |
zipFile.write(buf, 0, buf.length); |
|
139 |
zipFile.closeEntry(); |
|
140 |
fileinputstream.close(); |
|
141 |
} |
|
142 |
|
|
143 |
} |