KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > util > ZipUtil


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 Fossil E-Commerce, http://www.fossilec.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: ZipUtil.java 510 2006-06-02 12:18:30Z ofabre $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.util;
23
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.net.URI JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.zip.ZipEntry JavaDoc;
30 import java.util.zip.ZipException JavaDoc;
31 import java.util.zip.ZipFile JavaDoc;
32
33 import org.objectweb.petals.PetalsException;
34
35 /**
36  * Contains utilities methods in relation to Zip operations.
37  *
38  * @version $Rev: 510 $ $Date: 2006-06-02 14:18:30 +0200 (ven., 02 juin 2006) $
39  * @since Petals 1.0
40  * @author <a HREF="mailto:rmarins@fossilec.com">Rafael Marins</a>
41  * @author ddesjardins - eBMWebsourcing
42  */

43 public final class ZipUtil {
44
45     /**
46      * Do not construct.
47      */

48     private ZipUtil() {
49         super();
50     }
51
52     /**
53      * Explode a zip into a directory
54      *
55      * @param zipFile
56      * zip file
57      * @param aDirectory
58      * directory where the zip file will be extracted
59      * @throws PetalsException
60      * error during exploding the Zip archive into the directory.
61      */

62     public static void explodeIntoDirectory(ZipFile JavaDoc zipFile, File JavaDoc aDirectory)
63         throws PetalsException {
64         if (zipFile == null || aDirectory == null) {
65             throw new IllegalArgumentException JavaDoc(
66                     "The zip file or directory must not be null.");
67         } else if (!aDirectory.exists() || !aDirectory.isDirectory()) {
68             throw new IllegalArgumentException JavaDoc(
69                     "The given file is not a directory or doesn't exist.");
70         }
71         Enumeration JavaDoc entries = zipFile.entries();
72         while (entries.hasMoreElements()) {
73             ZipEntry JavaDoc anEntry = (ZipEntry JavaDoc) entries.nextElement();
74             File JavaDoc unpackFile = new File JavaDoc(aDirectory, anEntry.getName());
75             File JavaDoc parentDir = unpackFile.getParentFile();
76             if (!parentDir.exists() && !parentDir.mkdirs()) {
77                 throw new PetalsException(
78                         "Failed to explode zip into directory: "
79                                 + anEntry.getName());
80             }
81             if (!anEntry.isDirectory()) {
82                 InputStream JavaDoc zipStream = null;
83                 try {
84                     zipStream = zipFile.getInputStream(anEntry);
85                     NioUtil.copyStreamToFile(zipStream, unpackFile);
86                 } catch (IOException JavaDoc ioe) {
87                     throw new PetalsException(
88                             "Failed to unpack a Zip file entry: "
89                                     + anEntry.getName(), ioe);
90                 } finally {
91                     if (zipStream != null) {
92                         try {
93                             zipStream.close();
94                         } catch (IOException JavaDoc e) {
95                             // do nothing
96
}
97                     }
98                 }
99             }
100         }
101     }
102
103     /**
104      * See you must delete the temp file after using.
105      *
106      * @param zipFile
107      * @param entryName
108      * @return a temp File read from the Zip entry, or <code>null</code> if no
109      * entry was found.
110      */

111     public static File JavaDoc getEntryAsTemp(ZipFile JavaDoc zipFile, String JavaDoc entryName) {
112         if (zipFile == null) {
113             throw new IllegalArgumentException JavaDoc("Invalid null ZipFile.");
114         } else if (entryName == null || "".equals(entryName)) {
115             throw new IllegalArgumentException JavaDoc(
116                     "Invalid Zip entry was specified: null or empty.");
117         }
118         File JavaDoc tempFile = null;
119         InputStream JavaDoc zipStream = null;
120         try {
121             ZipEntry JavaDoc theEntry = null;
122             if (entryName.indexOf("/") > -1) {
123                 theEntry = zipFile.getEntry(entryName);
124                 if (theEntry == null) {
125                     theEntry = zipFile.getEntry(entryName.replace("/", "\\"));
126                     if (theEntry == null) {
127                         return null;
128                     }
129                 }
130             } else {
131                 theEntry = zipFile.getEntry(entryName);
132                 if (theEntry == null) {
133                     theEntry = zipFile.getEntry(entryName.replace("\\", "/"));
134                     if (theEntry == null) {
135                         return null;
136                     }
137                 }
138             }
139             zipStream = zipFile.getInputStream(theEntry);
140             tempFile = File.createTempFile("petals-", "-zip.tmp");
141             NioUtil.copyStreamToFile(zipStream, tempFile);
142         } catch (IOException JavaDoc e) {
143             // do nothing else, but return null
144
return null;
145         } finally {
146             try {
147                 if (zipStream != null) {
148                     zipStream.close();
149                 }
150             } catch (IOException JavaDoc ioe) {
151                 // do nothing
152
}
153         }
154         return tempFile;
155     }
156
157     /**
158      * Test if a zip file has a specific entry
159      *
160      * @param archive
161      * zip file
162      * @param entryName
163      * entry name
164      * @return true if the entry is in the zip file
165      */

166     public static boolean hasEntry(ZipFile JavaDoc archive, String JavaDoc entryName) {
167         ZipEntry JavaDoc theEntry = archive.getEntry(entryName);
168         return theEntry != null;
169     }
170
171     /**
172      * Load a zip file from a URI
173      *
174      * @param archiveURI
175      * uri of the zip file
176      * @return zip file
177      * @throws PetalsException
178      * when problems
179      */

180     public static ZipFile JavaDoc openZipFile(URI JavaDoc archiveURI) throws PetalsException {
181         ZipFile JavaDoc zipArchive = null;
182         String JavaDoc msg;
183         try {
184             File JavaDoc archiveFile = new File JavaDoc(archiveURI);
185             zipArchive = new ZipFile JavaDoc(archiveFile);
186         } catch (ZipException JavaDoc ze) {
187             msg = "Error opening Zip file. Location: " + archiveURI.toString();
188             throw new PetalsException(msg, ze);
189         } catch (IOException JavaDoc ioe) {
190             msg = "Unexpected I/O exception handling Zip file. Location : "
191                     + archiveURI.toString();
192             throw new PetalsException(msg, ioe);
193         }
194         return zipArchive;
195     }
196
197 }
198
Popular Tags