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