1 package org.varienaja.util;
2
3 import java.io.BufferedInputStream;
4 import java.io.BufferedOutputStream;
5 import java.io.File;
6 import java.io.FileInputStream;
7 import java.io.FileOutputStream;
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.io.OutputStream;
11 import java.util.zip.ZipEntry;
12
13 import org.apache.log4j.Logger;
14
15
16
17
18
19
20 public class FileOperations {
21 private static final int BUFFER = 4096;
22 private static final Logger LOG = Logger.getLogger(FileOperations.class);
23
24
25
26
27
28
29
30
31
32 public static String createUniqueFilename(String dstdir,String dst) {
33 File file = new File(dstdir+dst);
34 if (!file.exists()) {
35 return dstdir+dst;
36 } else {
37
38 String ext = "";
39 String fn = null;
40 int i = dst.lastIndexOf(".");
41 if (i<0) {
42 fn = dst;
43 } else {
44 fn = dst.substring(0,i);
45 ext = dst.substring(i);
46 }
47 String uniqueName = "";
48 i=0;
49 while (file.exists()) {
50 i++;
51 uniqueName = dstdir+fn+"("+i+")"+ext;
52 LOG.debug("Clash detected, trying: "+uniqueName);
53 file = new File(uniqueName);
54 }
55 LOG.debug("Unique filename: "+uniqueName);
56 return uniqueName;
57 }
58 }
59
60
61
62
63
64
65
66
67
68
69
70
71 public static String copyFile(String src,String dstdir,String dst) throws IOException {
72 dstdir = translateIllegalDirectoryChars(dstdir);
73 if (dstdir.charAt(dstdir.length()-1)!=File.separatorChar) {
74 dstdir = dstdir + File.separator;
75 }
76 dst = translateIllegalFileChars(dst);
77 try {
78 if (!new File(dstdir).mkdirs()) {
79 throw new IOException("Failed to create directory: "+dstdir);
80 }
81 String result = createUniqueFilename(dstdir,dst);
82
83 FileInputStream source = new FileInputStream(src);
84 FileOutputStream dest = new FileOutputStream(result);
85
86 copyStream(source,dest);
87 dest.close();
88 source.close();
89 return result;
90 } catch (IOException e) {
91 LOG.error("Error copying file: "+e);
92 throw e;
93 }
94 }
95
96
97
98
99
100
101
102
103
104 public static String moveFile(String src,String dstdir,String dst) throws IOException {
105 dstdir = translateIllegalDirectoryChars(dstdir);
106 if (dstdir.charAt(dstdir.length()-1)!=File.separatorChar) {
107 dstdir = dstdir + File.separator;
108 }
109 dst = translateIllegalFileChars(dst);
110 File destDir = new File(dstdir);
111 if(!destDir.exists()) {
112 if (!destDir.mkdirs()) {
113 throw new IOException("Failed to create directory: "+dstdir);
114 }
115 }
116 String result = createUniqueFilename(dstdir,dst);
117
118 File source = new File (src);
119 if (source.renameTo(new File(result))) {
120 return result;
121 } else {
122 throw new IOException("Error moving from: "+src+" to "+result);
123 }
124 }
125
126 public static void copyStream(InputStream in, OutputStream out) {
127 int count;
128 byte data[] = new byte[BUFFER];
129
130 try {
131 BufferedInputStream source = new BufferedInputStream(in);
132 BufferedOutputStream dest = new BufferedOutputStream(out,BUFFER);
133 while ((count = source.read(data,0,BUFFER))!=-1) {
134 dest.write(data,0,count);
135 }
136 dest.flush();
137 } catch (IOException e) {
138 LOG.error("Error copying stream: "+e);
139 }
140 }
141
142
143
144
145
146
147
148
149 public static String tmpFile(InputStream in,String tmpname) throws IOException {
150 File outFile = File.createTempFile(tmpname, null);
151 return streamToFile(in,outFile);
152 }
153
154
155
156
157
158
159
160
161
162 public static String streamToFile(InputStream in,String filename) throws IOException {
163 FileOutputStream out = new FileOutputStream(filename);
164 copyStream(in,out);
165 out.close();
166 return filename;
167 }
168
169
170
171
172
173
174
175
176
177 public static String streamToFile(InputStream in,File file) throws IOException {
178 FileOutputStream out = new FileOutputStream(file);
179 copyStream(in,out);
180 out.close();
181 return file.getAbsolutePath();
182 }
183
184
185
186
187
188
189 public static boolean deleteFile(String filename) {
190 File file = new File(filename);
191 return file.delete();
192 }
193
194
195
196
197
198
199
200
201 public static boolean deleteDir(File dir) {
202 if (dir == null) {
203 return false;
204 }
205
206
207
208
209
210
211 File candir;
212 try {
213 candir = dir.getCanonicalFile();
214 } catch (IOException e) {
215 return false;
216 }
217
218
219
220
221 if (!candir.equals(dir.getAbsoluteFile())) {
222
223
224
225
226 return false;
227 }
228
229
230
231 File[] files = candir.listFiles();
232 if (files != null) {
233 for (int i = 0; i < files.length; i++) {
234 File file = files[i];
235
236
237
238
239
240 boolean deleted = file.delete();
241 if (!deleted) {
242
243
244 if (file.isDirectory())
245 deleteDir(file);
246
247
248 }
249 }
250 }
251
252
253 return dir.delete();
254 }
255
256
257
258
259
260
261
262
263
264 public static String translateIllegalFileChars(String from) {
265
266 if (from==null) return null;
267 return from.replaceAll("\"|<|>|\\||:|\\*|\\?|/|\\\\|^\\.|\\.$|^ | $","_");
268 }
269
270
271
272
273
274
275
276
277
278 public static String translateIllegalDirectoryChars(String from) {
279
280 if (from==null) return null;
281 return from.replaceAll("\"|<|>|\\||\\*|\\?|^\\.|\\.$|^ | $","_");
282 }
283
284
285
286
287
288
289
290
291
292
293 public static int extractToDirectory(TolerantZipInputStream zis, String directory) {
294 int unpackedFiles = 0;
295
296 ZipEntry entry;
297 try {
298 while ((entry = zis.getNextEntry()) != null) {
299 if (entry.isDirectory()) {
300 String destFN = directory + File.separator + entry.getName();
301 LOG.debug("Creating directory: "+destFN);
302 if (!new File(destFN).mkdirs()) {
303 throw new IOException("Failed to create directoy: "+destFN);
304 }
305 } else {
306 String destFN = createUniqueFilename(directory + File.separator,entry.getName());
307 LOG.debug("Extracting to: "+destFN);
308 streamToFile(zis, destFN);
309 unpackedFiles++;
310 }
311 }
312 zis.close();
313 } catch (IllegalArgumentException e) {
314 e.printStackTrace();
315 LOG.error("Error reading zipfile, does it contain non-ascii characters? " +e);
316
317
318
319
320
321
322 } catch (IOException e) {
323 LOG.error(e);
324 }
325
326 return unpackedFiles;
327 }
328
329 }