comparison gnv-artifacts/src/main/java/de/intevation/gnv/utils/FileUtils.java @ 875:5e9efdda6894

merged gnv-artifacts/1.0
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:13:56 +0200
parents a645bd23c1c8
children f953c9a559d8
comparison
equal deleted inserted replaced
722:bb3ffe7d719e 875:5e9efdda6894
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 * A helper class to provide some methods for working with files and
17 * directories.
18 *
19 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
20 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
21 */
22 public final class FileUtils
23 {
24 private FileUtils() {
25 }
26
27 /**
28 * Deletes everything in a directory.
29 *
30 * @param dir The directory.
31 */
32 public final static void deleteContent(File dir) {
33 if (dir == null || !dir.isDirectory()) {
34 return;
35 }
36
37 File[] files = dir.listFiles();
38 if (files != null) {
39 for (File file: files) {
40 deleteRecursive(file);
41 }
42 }
43
44 return;
45 }
46
47 /**
48 * Delete <i>file</i> and everything in <i>file</i> if it is a directory.
49 *
50 * @param file The file or directory.
51 * @return true, if deletion was successful - otherwise false.
52 */
53 public final static boolean deleteRecursive(File file) {
54
55 if (file == null) {
56 return false;
57 }
58
59 if (file.isDirectory()) {
60 File [] files = file.listFiles();
61 if (files != null) {
62 for (File sub: files) {
63 if (!deleteRecursive(sub)) {
64 return false;
65 }
66 }
67 }
68 }
69
70 return file.delete();
71 }
72
73 /**
74 * Put the given file or directory into a zip archive.
75 *
76 * @param file The file or directory.
77 * @param outputStream The stream to write the archive to.
78 * @throws IOException if an error occured while zip creation or writing to
79 * output stream.
80 */
81 public static void createZipArchive(
82 File file,
83 OutputStream outputStream
84 )
85 throws IOException
86 {
87 ZipOutputStream out = new ZipOutputStream(outputStream);
88
89 if (file.isFile()) {
90 copyFileToZip("", file, out);
91 }
92 else if (file.isDirectory()) {
93
94 Stack<PrefixDir> stack = new Stack<PrefixDir>();
95 stack.push(new PrefixDir(file.getName() + "/", file));
96
97 while (!stack.isEmpty()) {
98 PrefixDir pd = stack.pop();
99
100 ZipEntry dirEntry = new ZipEntry(pd.prefix);
101 out.putNextEntry(dirEntry);
102 out.closeEntry();
103
104 File [] files = pd.dir.listFiles();
105 if (files != null) {
106 for (File sub: files) {
107 if (sub.isDirectory()) {
108 stack.push(new PrefixDir(
109 pd.prefix + sub.getName() + "/",
110 sub));
111 }
112 else if (sub.isFile()) {
113 copyFileToZip(pd.prefix, sub, out);
114 }
115 }
116 }
117 }
118 }
119
120 out.finish();
121 }
122
123 /**
124 * A class representing a directory with a prefix.
125 */
126 private static final class PrefixDir {
127
128 String prefix;
129 File dir;
130
131 public PrefixDir(String prefix, File dir) {
132 this.prefix = prefix;
133 this.dir = dir;
134 }
135
136 } // class PrefixDir
137
138 /**
139 * Write a file to zip archive.
140 *
141 * @param prefix A prefix.
142 * @param file The file.
143 * @param out The output stream.
144 * @throws IOException if an error occured while writing to zip output
145 * stream.
146 */
147 private static void copyFileToZip(
148 String prefix,
149 File file,
150 ZipOutputStream out
151 )
152 throws IOException
153 {
154 String entryName = prefix + file.getName();
155 ZipEntry entry = new ZipEntry(entryName);
156 out.putNextEntry(entry);
157 InputStream in = null;
158 try {
159 in =
160 new BufferedInputStream(
161 new FileInputStream(file), 20*1024);
162
163 byte [] buf = new byte[2048];
164
165 int r;
166 while ((r = in.read(buf)) > 0) {
167 out.write(buf, 0, r);
168 }
169 }
170 finally {
171 if (in != null) {
172 try { in.close(); }
173 catch (IOException ioe) {}
174 }
175 }
176 out.closeEntry();
177 }
178 }
179 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org