comparison gnv-artifacts/src/main/java/de/intevation/gnv/utils/FileUtils.java @ 480:211cad2fb5ba

Rebased "Horizonalschnitte" to own state class to break from the not well fitting TimeSeriesOutputState. gnv-artifacts/trunk@552 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 17 Jan 2010 12:22:56 +0000
parents
children eedad2ddad14
comparison
equal deleted inserted replaced
479:d47b478e662b 480:211cad2fb5ba
1 package de.intevation.gnv.utils;
2
3 import java.io.BufferedInputStream;
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.OutputStream;
9
10 import java.util.Stack;
11
12 import java.util.zip.ZipEntry;
13 import java.util.zip.ZipOutputStream;
14
15 /**
16 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
17 */
18 public final class FileUtils
19 {
20 private FileUtils() {
21 }
22
23 public final static boolean deleteRecursive(File file) {
24
25 if (file == null) {
26 return false;
27 }
28
29 if (file.isDirectory()) {
30 File [] files = file.listFiles();
31 if (files != null) {
32 for (File sub: files) {
33 if (!deleteRecursive(sub)) {
34 return false;
35 }
36 }
37 }
38 }
39
40 return file.delete();
41 }
42
43 public static void createZipArchive(
44 File file,
45 OutputStream outputStream
46 )
47 throws IOException
48 {
49 ZipOutputStream out = new ZipOutputStream(outputStream);
50
51 if (file.isFile()) {
52 copyFileToZip("", file, out);
53 }
54 else if (file.isDirectory()) {
55
56 Stack<PrefixDir> stack = new Stack<PrefixDir>();
57 stack.push(new PrefixDir(file.getName() + "/", file));
58
59 while (!stack.isEmpty()) {
60 PrefixDir pd = stack.pop();
61
62 ZipEntry dirEntry = new ZipEntry(pd.prefix);
63 out.putNextEntry(dirEntry);
64 out.closeEntry();
65
66 File [] files = pd.dir.listFiles();
67 if (files != null) {
68 for (File sub: files) {
69 if (sub.isDirectory()) {
70 stack.push(new PrefixDir(
71 pd.prefix + sub.getName() + "/",
72 sub));
73 }
74 else if (sub.isFile()) {
75 copyFileToZip(pd.prefix, sub, out);
76 }
77 }
78 }
79 }
80 }
81
82 out.finish();
83 }
84
85 private static final class PrefixDir {
86
87 String prefix;
88 File dir;
89
90 public PrefixDir(String prefix, File dir) {
91 this.prefix = prefix;
92 this.dir = dir;
93 }
94
95 } // class PrefixDir
96
97 private static void copyFileToZip(
98 String prefix,
99 File file,
100 ZipOutputStream out
101 )
102 throws IOException
103 {
104 String entryName = prefix + file.getName();
105 ZipEntry entry = new ZipEntry(entryName);
106 out.putNextEntry(entry);
107 InputStream in = null;
108 try {
109 in =
110 new BufferedInputStream(
111 new FileInputStream(file), 20*1024);
112
113 byte [] buf = new byte[2048];
114
115 int r;
116 while ((r = in.read(buf)) > 0) {
117 out.write(buf, 0, r);
118 }
119 }
120 finally {
121 if (in != null) {
122 try { in.close(); }
123 catch (IOException ioe) {}
124 }
125 }
126 out.closeEntry();
127 }
128 }
129 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org