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