KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > config > backup > util > ZipFile


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /* Byron Nevins, April 2000
25  * ZipFile -- A utility class for exploding archive (zip) files.
26  */

27
28 package com.sun.enterprise.config.backup.util;
29
30 import java.io.*;
31 import java.util.*;
32 import java.util.zip.*;
33 import java.util.jar.JarFile JavaDoc;
34
35 //import com.sun.enterprise.util.io.FileUtils;
36
//import com.sun.enterprise.util.Assertion;
37
//import com.sun.enterprise.util.diagnostics.Reporter;
38

39
40 ///////////////////////////////////////////////////////////////////////////////
41

42 public class ZipFile
43 {
44     public ZipFile(String JavaDoc zipFilename, String JavaDoc explodeDirName) throws ZipFileException
45     {
46         this(new File JavaDoc(zipFilename), new File JavaDoc(explodeDirName));
47     }
48
49     ///////////////////////////////////////////////////////////////////////////
50

51     public ZipFile(File JavaDoc zipFile, File JavaDoc anExplodeDir) throws ZipFileException
52     {
53         //checkZipFile(zipFile);
54
BufferedInputStream bis = null;
55         
56         try
57         {
58             bis = new BufferedInputStream(new FileInputStream(zipFile), BUFFER_SIZE);
59             ctor(bis, anExplodeDir);
60         }
61         catch(Throwable JavaDoc e)
62         {
63             if (bis != null)
64             {
65                 try
66                 {
67                     bis.close();
68                 }
69                 catch (Throwable JavaDoc thr)
70                 {
71                     throw new ZipFileException(thr);
72                 }
73             }
74             
75             throw new ZipFileException(e);
76         }
77     }
78         
79     ///////////////////////////////////////////////////////////////////////////
80

81     public ZipFile(InputStream inStream, File JavaDoc anExplodeDir) throws ZipFileException
82     {
83         ctor(inStream, anExplodeDir);
84     }
85
86     //////////////////////////////////////////////////////////////////////////////////////////////////////////////
87

88     public ArrayList explode() throws ZipFileException
89     {
90         files = new ArrayList();
91         ZipInputStream zin = null;
92
93         try
94         {
95             zin = zipStream; // new ZipInputStream(new FileInputStream(zipFile));
96
ZipEntry ze;
97
98             while( (ze = zin.getNextEntry()) != null )
99             {
100                 String JavaDoc filename = ze.getName();
101                 
102                 /*
103                 if(isManifest(filename))
104                 {
105                     continue; // don't bother with manifest file...
106                 }
107                 */

108                 
109                 File JavaDoc fullpath = null;
110                 
111                 if(isDirectory(filename))
112                 {
113                     // just a directory -- make it and move on...
114
fullpath = new File JavaDoc(explodeDir, filename.substring(0, filename.length() - 1));
115                     
116                     // bnevins. FAT BUG FIX!!! This was mkdir() not mkdirs()
117
// this should be back-ported to appserv-commons
118

119                     fullpath.mkdirs();
120                     continue;
121                 }
122                     
123                 fullpath = new File JavaDoc(explodeDir, filename);
124                 File JavaDoc newDir = fullpath.getParentFile();
125
126                 if(newDir.mkdirs())
127                 { // note: it returns false if dir already exists...
128
//Reporter.verbose("Created new directory: " + newDir);//NOI18N
129
}
130
131                 if(fullpath.delete()) // wipe-out pre-existing files
132
{
133                     //Reporter.info("deleted pre-existing file: " + fullpath); //NOI18N
134
}
135
136                 BufferedOutputStream os = new BufferedOutputStream(getOutputStream(filename), BUFFER_SIZE);
137
138                 if(os == null) // e.g. if we asked to write to a directory instead of a file...
139
continue;
140
141                 int totalBytes = 0;
142
143                 for(int numBytes = zin.read(buffer); numBytes > 0; numBytes = zin.read(buffer))
144                 {
145                     os.write(buffer, 0, numBytes);
146                     totalBytes += numBytes;
147                 }
148                 os.close();
149                 //Reporter.verbose("Wrote " + totalBytes + " to " + filename);//NOI18N
150
files.add(filename);
151             }
152         }
153         catch(IOException e)
154         {
155             throw new ZipFileException(e);
156         }
157         finally
158         {
159             //Reporter.verbose("Closing zin...");//NOI18N
160
try
161             {
162                 zin.close();
163             }
164             catch(IOException e)
165             {
166                 throw new ZipFileException("Got an exception while trying to close Jar input stream: " + e);//NOI18N
167
}
168         }
169         //Reporter.info("Successfully Exploded ZipFile" + " to " + explodeDir.getPath());//NOI18N
170
return files;
171     }
172
173     ///////////////////////////////////////////////////////////////////////////
174

175     public ArrayList getFileList()
176     {
177         return files;
178     }
179     
180     /***********************************************************************
181     /******************************** Private ******************************
182     /***********************************************************************/

183     private static ArrayList doExplode(ZipFile zf) throws ZipFileException
184         {
185             ArrayList finalList = new ArrayList(50);
186             ArrayList zipFileList = new ArrayList();
187             ArrayList tmpList = null;
188             ZipFile tmpZf = null;
189             Iterator itr = null;
190             String JavaDoc fileName = null;
191
192             zipFileList.add(zf);
193             while (zipFileList.size() > 0)
194             {
195                 // get "last" jar to explode
196
tmpZf = (ZipFile)zipFileList.remove(zipFileList.size() - 1);
197                 tmpList = tmpZf.explode();
198
199                 // traverse list of files
200
itr = tmpList.iterator();
201                 while (itr.hasNext())
202                 {
203                     fileName = (String JavaDoc)itr.next();
204                     if ( ! fileName.endsWith(".jar") )
205                     {
206                         // add non-jar file to finalList
207
finalList.add(fileName);
208                     }
209                     else
210                     {
211                         // create ZipFile and add to zipFileList
212
File JavaDoc f = new File JavaDoc(tmpZf.explodeDir, fileName);
213                         ZipFile newZf = new ZipFile(f, tmpZf.explodeDir);
214                         zipFileList.add(newZf);
215                     }
216
217                     if (tmpZf != zf) // don't remove first ZipFile
218
{
219                         tmpZf.explodeDir.delete();
220                     }
221                 }
222             }
223             return finalList;
224         }
225
226     //////////////////////////////////////////////////////////////////////////////////////////////////////////////
227

228     private void ctor(InputStream inStream, File JavaDoc anExplodeDir) throws ZipFileException
229     {
230         insist(anExplodeDir != null);
231         explodeDir = anExplodeDir;
232
233         try
234         {
235             zipStream = new ZipInputStream(inStream);
236             checkExplodeDir();
237         }
238         catch(Throwable JavaDoc t)
239         {
240                     if (zipStream != null) {
241                         try {
242                             zipStream.close();
243                         } catch (Throwable JavaDoc thr) {
244                         }
245                     }
246             throw new ZipFileException(t.toString());
247         }
248     }
249         
250     ///////////////////////////////////////////////////////////////////////////
251

252     private boolean isDirectory(String JavaDoc s)
253     {
254         char c = s.charAt(s.length() - 1);
255         
256         return c== '/' || c == '\\';
257     }
258
259     /////////////////////////////////////////////////////////////////////////////////////////
260

261     private void checkZipFile(File JavaDoc zipFile) throws ZipFileException
262     {
263         insist(zipFile != null);
264         
265         String JavaDoc zipFileName = zipFile.getPath();
266
267         insist( zipFile.exists(), "zipFile (" + zipFileName + ") doesn't exist" );//NOI18N
268
insist( !zipFile.isDirectory(), "zipFile (" + zipFileName + ") is actually a directory!" );//NOI18N
269
}
270
271     /////////////////////////////////////////////////////////////////////////////////////////
272

273     private void checkExplodeDir() throws ZipFileException
274     {
275         String JavaDoc explodeDirName = explodeDir.getPath();
276         
277         // just in case...
278
explodeDir.mkdirs();
279         
280         insist(explodeDir.exists(), "Target Directory doesn't exist: " + explodeDirName );//NOI18N
281
insist(explodeDir.isDirectory(), "Target Directory isn't a directory: " + explodeDirName );//NOI18N
282
insist(explodeDir.canWrite(), "Can't write to Target Directory: " + explodeDirName );//NOI18N
283
}
284
285     /////////////////////////////////////////////////////////////////////////////////////////
286

287     private static boolean isSpecial(String JavaDoc filename)
288     {
289         return filename.toUpperCase().startsWith(specialDir.toUpperCase());
290     }
291
292     /////////////////////////////////////////////////////////////////////////////////////////
293

294     private FileOutputStream getOutputStream(String JavaDoc filename) throws ZipFileException
295     {
296         File JavaDoc f = new File JavaDoc(explodeDir, filename);
297
298         if(f.isDirectory())
299         {
300             //Reporter.warn("Weird! A directory is listed as an entry in the jar file -- skipping...");//NOI18N
301
return null;
302         }
303
304         try
305         {
306             return new FileOutputStream(f);
307         }
308         catch(FileNotFoundException e)
309         {
310             throw new ZipFileException("filename: " + f.getPath() + " " + e);
311         }
312         catch(IOException e)
313         {
314             throw new ZipFileException(e);
315         }
316     }
317
318     /////////////////////////////////////////////////////////////////////////////////////////
319

320     private boolean isManifest(String JavaDoc filename)
321     {
322         if(filename.toLowerCase().endsWith("manifest.mf"))//NOI18N
323
return false;
324         
325         return false;
326     }
327
328     /////////////////////////////////////////////////////////////////////////////////////////
329
//////////// //////////////////////////
330
//////////// Internal Error-Checking Stuff //////////////////////////
331
//////////// //////////////////////////
332
/////////////////////////////////////////////////////////////////////////////////////////
333

334     private static void pr(String JavaDoc s)
335     {
336         System.out.println( s );
337     }
338
339     /////////////////////////////////////////////////////////////////////////////////////////
340

341     private static void insist(String JavaDoc s) throws ZipFileException
342     {
343         if( s == null || s.length() < 0 )
344             throw new ZipFileException();
345         else
346             return;
347     }
348
349     /////////////////////////////////////////////////////////////////////////////////////////
350

351     private static void insist(String JavaDoc s, String JavaDoc mesg) throws ZipFileException
352     {
353         if( s == null || s.length() < 0 )
354             throw new ZipFileException( mesg );
355         else
356             return;
357     }
358
359     /////////////////////////////////////////////////////////////////////////////////////////
360

361     private static void insist(boolean b) throws ZipFileException
362     {
363         if( !b )
364             throw new ZipFileException();
365         else
366             return;
367     }
368
369     /////////////////////////////////////////////////////////////////////////////////////////
370

371     private static void insist(boolean b, String JavaDoc mesg) throws ZipFileException
372     {
373         if( !b )
374             throw new ZipFileException( mesg );
375         else
376             return;
377     }
378
379     /////////////////////////////////////////////////////////////////////////////////////////
380

381         private static final int BUFFER_SIZE = 0x10000; //64k
382
private File JavaDoc explodeDir = null;
383     private ArrayList files = null;
384     private static final String JavaDoc specialDir = "META-INF/";//NOI18N
385
private byte[] buffer = new byte[BUFFER_SIZE];
386     private ZipInputStream zipStream = null;
387 }
388
389 ////////////////
390
Popular Tags