KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > util > ZipUtil


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.util;
21
22 import java.io.BufferedInputStream JavaDoc;
23 import java.io.BufferedOutputStream JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.FileOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.OutputStream JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.util.zip.ZipEntry JavaDoc;
32 import java.util.zip.ZipFile JavaDoc;
33 import java.util.zip.ZipOutputStream JavaDoc;
34
35 /**
36  * Utility class to perform zip/unzip operations on files and directories.
37  *
38  * @author Andrus Adamchik
39  */

40 public class ZipUtil {
41
42     /**
43      * Constructor for ZipUtil.
44      */

45     public ZipUtil() {
46         super();
47     }
48
49     /**
50       * Unpacks a zip file to the target directory.
51       *
52       * @param zipFile
53       * @param destDir
54       * @throws IOException
55       */

56     public static void unzip(File JavaDoc zipFile, File JavaDoc destDir) throws IOException JavaDoc {
57         ZipFile JavaDoc zip = new ZipFile JavaDoc(zipFile);
58
59         try {
60             Enumeration JavaDoc en = zip.entries();
61             int bufSize = 8 * 1024;
62
63             while (en.hasMoreElements()) {
64                 ZipEntry JavaDoc entry = (ZipEntry JavaDoc) en.nextElement();
65                 File JavaDoc file =
66                     (destDir != null)
67                         ? new File JavaDoc(destDir, entry.getName())
68                         : new File JavaDoc(entry.getName());
69
70                 if (entry.isDirectory()) {
71                     if (!file.mkdirs()) {
72                         throw new IOException JavaDoc(
73                             "Error creating directory: " + file);
74                     }
75                 } else {
76                     File JavaDoc parent = file.getParentFile();
77                     if (parent != null && !parent.exists()) {
78                         if (!parent.mkdirs()) {
79                             throw new IOException JavaDoc(
80                                 "Error creating directory: " + parent);
81                         }
82                     }
83
84                     InputStream JavaDoc in = zip.getInputStream(entry);
85                     try {
86                         OutputStream JavaDoc out =
87                             new BufferedOutputStream JavaDoc(
88                                 new FileOutputStream JavaDoc(file),
89                                 bufSize);
90
91                         try {
92                             Util.copyPipe(in, out, bufSize);
93                         } finally {
94                             out.close();
95                         }
96
97                     } finally {
98                         in.close();
99                     }
100                 }
101             }
102         } finally {
103             zip.close();
104         }
105     }
106
107     /**
108       * Recursively zips a set of root entries into a zipfile, compressing the
109       * contents.
110       *
111       * @param zipFile target zip file.
112       * @param parentDir a directory containing source files to zip.
113       * @param sources an array of files and/or directories to zip.
114       * @param pathSeparator path separator for zip entries.
115       *
116       * @throws IOException
117       */

118     public static void zip(
119         File JavaDoc zipFile,
120         File JavaDoc parentDir,
121         File JavaDoc[] sources,
122         char pathSeparator)
123         throws IOException JavaDoc {
124             
125         String JavaDoc stripPath = (parentDir != null) ? parentDir.getPath() : "";
126         if (stripPath.length() > 0 && !stripPath.endsWith(File.separator)) {
127             stripPath += File.separator;
128         }
129
130         ZipOutputStream JavaDoc out =
131             new ZipOutputStream JavaDoc(new FileOutputStream JavaDoc(zipFile));
132         out.setMethod(ZipOutputStream.DEFLATED);
133
134         try {
135             // something like an Ant directory scanner wouldn't hurt here
136
for (int i = 0; i < sources.length; i++) {
137                 if (!sources[i].exists()) {
138                     throw new IllegalArgumentException JavaDoc(
139                         "File or directory does not exist: " + sources[i]);
140                 }
141
142                 if (sources[i].isDirectory()) {
143                     zipDirectory(out, stripPath, sources[i], pathSeparator);
144                 } else {
145                     zipFile(out, stripPath, sources[i], pathSeparator);
146                 }
147             }
148         } finally {
149             out.close();
150         }
151     }
152
153     /**
154      * Uses code fragments from Jakarta-Ant, Copyright: Apache Software
155      * Foundation.
156      */

157     private static void zipDirectory(
158         ZipOutputStream JavaDoc out,
159         String JavaDoc stripPath,
160         File JavaDoc dir,
161         char pathSeparator)
162         throws IOException JavaDoc {
163
164         String JavaDoc[] entries = dir.list();
165
166         if (entries == null || entries.length == 0) {
167             return;
168         }
169
170         // recurse via entries
171
for (int i = 0; i < entries.length; i++) {
172             File JavaDoc file = new File JavaDoc(dir, entries[i]);
173             if (file.isDirectory()) {
174                 zipDirectory(out, stripPath, file, pathSeparator);
175             } else {
176                 zipFile(out, stripPath, file, pathSeparator);
177             }
178         }
179     }
180
181     /**
182      * Uses code fragments from Jakarta-Ant, Copyright: Apache Software
183      * Foundation.
184      */

185     private static void zipFile(
186         ZipOutputStream JavaDoc out,
187         String JavaDoc stripPath,
188         File JavaDoc file,
189         char pathSeparator)
190         throws IOException JavaDoc {
191         ZipEntry JavaDoc ze =
192             new ZipEntry JavaDoc(processPath(file.getPath(), stripPath, pathSeparator));
193         ze.setTime(file.lastModified());
194         out.putNextEntry(ze);
195
196         byte[] buffer = new byte[8 * 1024];
197         BufferedInputStream JavaDoc in =
198             new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(file), buffer.length);
199
200         try {
201             int count = 0;
202             while ((count = in.read(buffer, 0, buffer.length)) >= 0) {
203                 if (count != 0) {
204                     out.write(buffer, 0, count);
205                 }
206             }
207         } finally {
208             in.close();
209         }
210     }
211
212     private static String JavaDoc processPath(
213         String JavaDoc path,
214         String JavaDoc stripPath,
215         char pathSeparator) {
216         if (!path.startsWith(stripPath)) {
217             throw new IllegalArgumentException JavaDoc(
218                 "Invalid entry: "
219                     + path
220                     + "; expected to start with "
221                     + stripPath);
222         }
223
224         return path.substring(stripPath.length()).replace(
225             File.separatorChar,
226             pathSeparator);
227     }
228 }
229
Popular Tags