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