KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > util > ZipUtil


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2002 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.util;
15
16 import java.util.*;
17 import java.util.zip.*;
18 import java.util.jar.*;
19 import java.io.*;
20 import java.sql.*;
21
22 /**
23  * Zip File Utilities
24  *
25  * @author Jorg Janke
26  * @version $Id: ZipUtil.java,v 1.1 2003/01/31 04:02:23 jjanke Exp $
27  */

28 public class ZipUtil
29 {
30     /**
31      * Empty Constructor, need to open explicitly.
32      */

33     public ZipUtil()
34     {
35     } // ZipUtil
36

37     /**
38      * Open zip file.
39      * @param fileName zip file name
40      */

41     public ZipUtil(String JavaDoc fileName)
42     {
43         open (fileName);
44     } // ZipUtil
45

46     /**
47      * Open zip file.
48      * @param file zip file
49      */

50     public ZipUtil(File file)
51     {
52         open(file);
53     } // ZipUtil
54

55     private File m_file;
56     private ZipFile m_zipFile;
57
58     /**
59      * Open the Zip File for reading
60      * @param fileName zip file
61      * @return true if opened
62      */

63     public boolean open (String JavaDoc fileName)
64     {
65         if (fileName == null)
66             return false;
67         try
68         {
69             return open (new File(fileName));
70         }
71         catch (Exception JavaDoc ex)
72         {
73             System.err.println("ZipUtil.open - " + ex);
74         }
75         return false;
76     } // open
77

78     /**
79      * Open the Zip File for reading
80      * @param file zip file
81      * @return true if opened
82      */

83     public boolean open (File file)
84     {
85         if (file == null)
86             return false;
87         m_file = file;
88         try
89         {
90             if (file.getName().endsWith("jar"))
91                 m_zipFile = new JarFile (file, false, JarFile.OPEN_READ);
92             else
93                 m_zipFile = new ZipFile (file, ZipFile.OPEN_READ);
94         }
95         catch (IOException ex)
96         {
97             System.err.println("ZipUtil.open - " + ex);
98             m_zipFile = null;
99             return false;
100         }
101         return true;
102     } // open
103

104     /**
105      * Close Zip File
106      */

107     public void close()
108     {
109         try
110         {
111             if (m_zipFile != null)
112                 m_zipFile.close();
113         }
114         catch (IOException ex)
115         {
116             System.err.println("ZipUtil.close - " + ex);
117         }
118         m_zipFile = null;
119     } // close
120

121     /**
122      * Is the Zip File Open
123      * @return true if yes
124      */

125     public boolean isOpen()
126     {
127         return m_zipFile != null;
128     } // isOpen
129

130     /**
131      * Is it a Jar
132      * @return true if yes
133      */

134     public boolean isJar()
135     {
136         return (m_zipFile != null && m_zipFile instanceof JarFile);
137     } // isJar
138

139     /**
140      * Get it as Jar if it is a Jar
141      * @return jar or null if not a jar
142      */

143     public JarFile getJar()
144     {
145         if (m_zipFile != null && m_zipFile instanceof JarFile)
146             return (JarFile)m_zipFile;
147         return null;
148     } // getJar
149

150     /**
151      * String Representation
152      * @return info
153      */

154     public String JavaDoc toString()
155     {
156         if (m_zipFile != null)
157             return m_zipFile.toString();
158         return "ZipUtil";
159     } // toString
160

161     /*************************************************************************/
162
163     /**
164      * Get Content as sorted String Array
165      * @return content
166      */

167     public String JavaDoc[] getContent()
168     {
169         if (!isOpen())
170             return null;
171         Enumeration e = m_zipFile.entries();
172         ArrayList list = new ArrayList();
173         while (e.hasMoreElements())
174             list.add(e.nextElement());
175         // return sorted array
176
String JavaDoc[] retValue = new String JavaDoc[list.size()];
177         for (int i = 0; i < retValue.length; i++)
178             retValue[i] = ((ZipEntry)list.get(i)).getName();
179         Arrays.sort(retValue);
180         return retValue;
181     } // getContent
182

183     /**
184      * Get ZipEntries as Enumeration
185      * @return entries
186      */

187     public Enumeration entries()
188     {
189         if (!isOpen())
190             return null;
191         return m_zipFile.entries();
192     } // entries
193

194     /**
195      * Get Zip Entry
196      * @param name entry name
197      * @return ZipEntry or null if not found
198      */

199     public ZipEntry getEntry (String JavaDoc name)
200     {
201         if (!isOpen())
202             return null;
203         return m_zipFile.getEntry(name);
204     } // getEntry
205

206     /**
207      * Get File Info
208      * @param name name
209      * @return time and size
210      */

211     public String JavaDoc getEntryInfo (String JavaDoc name)
212     {
213         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(name);
214         ZipEntry e = getEntry(name);
215         if (e == null)
216             sb.append(": -");
217         else
218         {
219             Timestamp ts = new Timestamp(e.getTime());
220             sb.append(": ").append(ts).append(" - ").append(e.getSize());
221         }
222         return sb.toString();
223     } // getEntryInfo
224

225     /**
226      * Get Manifest if a Jar
227      * @return Manifest if exists or null
228      */

229     public Manifest getManifest()
230     {
231         try
232         {
233             JarFile jar = getJar();
234             if (jar != null)
235                 return jar.getManifest();
236         }
237         catch (IOException ex)
238         {
239             System.err.println("ZipUtil.getManifest - " + ex);
240         }
241         return null;
242     } // getManifest
243

244
245     /*************************************************************************/
246
247     /**
248      * Get Zip Entry
249      * @param fileName zip/jar file
250      * @param entryName entry
251      * @return ZipEntry
252      */

253     static public ZipEntry getEntry (String JavaDoc fileName, String JavaDoc entryName)
254     {
255         if (fileName == null || entryName == null)
256             return null;
257         // File
258
File file = new File(fileName);
259         if (!file.exists())
260         {
261             String JavaDoc fn = getFullName(fileName);
262             if (fn == null)
263                 return null; // file not found
264
file = new File(fn);
265         }
266         ZipUtil zu = new ZipUtil (file);
267         if (!zu.isOpen())
268             return null;
269         // Entry
270
ZipEntry retValue = zu.getEntry(entryName);
271         if (retValue == null)
272         {
273             Enumeration e = zu.entries();
274             while (e.hasMoreElements())
275             {
276                 ZipEntry entry = (ZipEntry)e.nextElement();
277                 if (entry.getName().indexOf(entryName) != -1)
278                 {
279                     retValue = entry;
280                     break;
281                 }
282             }
283         }
284         zu.close();
285         return retValue;
286     } // getEntry
287

288     /**
289      * Get Jar File
290      * @param fileName zip/jar file
291      * @return Jar
292      */

293     static public JarFile getJar (String JavaDoc fileName)
294     {
295         if (fileName == null)
296             return null;
297         // File
298
File file = new File(fileName);
299         if (!file.exists())
300         {
301             String JavaDoc fn = getFullName(fileName);
302             if (fn == null)
303                 return null; // file not found
304
file = new File(fn);
305         }
306         ZipUtil zu = new ZipUtil (file);
307         return zu.getJar();
308     } // getJar
309

310     /**
311      * Get Manifest
312      * @param fileName zip/jar file
313      * @return Manifest or null
314      */

315     static public Manifest getManifest (String JavaDoc fileName)
316     {
317         if (fileName == null)
318             return null;
319         JarFile jar = getJar (fileName);
320         if (jar == null)
321             return null;
322         try
323         {
324             return jar.getManifest();
325         }
326         catch (IOException ex)
327         {
328             System.err.println("ZipUtil.getManifest - " + ex);
329         }
330         return null;
331     } // getManifest
332

333     /**
334      * Get Manifest
335      * @param fileName jar file
336      * @param jarEntry jar entry
337      * @return Manifest
338      */

339     static public JarEntry getJarEntry (String JavaDoc fileName, String JavaDoc jarEntry)
340     {
341         if (fileName == null)
342             return null;
343         JarFile jar = getJar (fileName);
344         if (jar == null)
345             return null;
346         return jar.getJarEntry(jarEntry);
347     } // getManifest
348

349     /**
350      * Dump Manifest to
351      * @param fileName zip/jar file
352      * @return Manifest
353      */

354     static public void dumpManifest (String JavaDoc fileName)
355     {
356         Manifest mf = getManifest (fileName);
357         if (mf == null)
358         {
359             System.out.println("No Jar file: " + fileName);
360             return;
361         }
362         //
363
System.out.println(mf.getEntries());
364     } // dumpManifest
365

366     /**
367      * Get Zip Entry time
368      * @param fileName zip file
369      * @param entryName entry
370      * @return Time as String or null
371      */

372     static public String JavaDoc getEntryTime (String JavaDoc fileName, String JavaDoc entryName)
373     {
374         ZipEntry entry = getEntry(fileName, entryName);
375         if (entry == null)
376             return null;
377         Timestamp ts = new Timestamp (entry.getTime());
378         return ts.toString();
379     } // getEntryTime
380

381     /**
382      * Get Fill name of jarfile in path
383      * @param jarFile name
384      * @return full name or null if not found
385      */

386     static public String JavaDoc getFullName (String JavaDoc jarFile)
387     {
388         String JavaDoc path = System.getProperty("java.class.path");
389         String JavaDoc[] pathEntries = path.split(System.getProperty("path.separator"));
390         for (int i = 0; i < pathEntries.length; i++)
391         {
392         // System.out.println(pathEntries[i]);
393
if (pathEntries[i].indexOf(jarFile) != -1)
394                 return pathEntries[i];
395         }
396         path = System.getProperty("sun.boot.class.path");
397         pathEntries = path.split(System.getProperty("path.separator"));
398         for (int i = 0; i < pathEntries.length; i++)
399         {
400         // System.out.println(pathEntries[i]);
401
if (pathEntries[i].indexOf(jarFile) != -1)
402                 return pathEntries[i];
403         }
404         return null;
405     } // getFullName
406

407 } // ZipUtil
Popular Tags