annotate gnv-artifacts/src/main/java/de/intevation/gnv/utils/FileUtils.java @ 1116:2d393d79c097

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

http://dive4elements.wald.intevation.org