KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > common > util > zip > 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 jar files that contain EJB(s). Used *only* in this package by the EJBImporter class
26  */

27
28 package com.sun.enterprise.tools.common.util.zip;
29
30 import java.io.File JavaDoc;
31 import java.io.FileInputStream JavaDoc;
32 import java.io.FileOutputStream JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.FileNotFoundException JavaDoc;
35 import java.io.InputStream JavaDoc;
36 import java.io.PrintStream JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.zip.ZipEntry JavaDoc;
40 import java.util.zip.ZipInputStream JavaDoc;
41
42 import com.sun.enterprise.tools.common.util.diagnostics.Reporter;
43 import com.sun.enterprise.tools.common.util.Assertion;
44 import com.sun.enterprise.tools.common.util.ContainerHelper;
45
46 public class ZipFile
47 {
48     public ZipFile(String JavaDoc zipFilename, String JavaDoc explodeDirName) throws ZipFileException
49     {
50         assertIt(zipFilename);
51         assertIt(explodeDirName);
52
53         this.zipFilename = zipFilename;
54         this.explodeDirName = explodeDirName;
55
56         try
57         {
58             checkZipFile();
59                         zipStream = new ZipInputStream JavaDoc(new FileInputStream JavaDoc(zipFile));
60                         //this(zipStream, explodeDirName);
61
checkExplodeDir();
62         }
63         catch(Exception JavaDoc f)
64         {
65             throw new ZipFileException(f);
66         }
67     }
68         
69         
70         public ZipFile(InputStream JavaDoc inStream, String JavaDoc explodeDirName) throws ZipFileException
71     {
72         //assertIt(zipFilename);
73
assertIt(explodeDirName);
74
75         //this.zipFilename = zipFilename;
76
this.explodeDirName = explodeDirName;
77
78         try
79         {
80             //checkZipFile();
81
zipStream = new ZipInputStream JavaDoc(inStream);
82             checkExplodeDir();
83         }
84         catch(Assertion.Failure f)
85         {
86             throw new ZipFileException(f);
87         }
88     }
89         
90     //////////////////////////////////////////////////////////////////////////////////////////////////////////////
91

92     public String JavaDoc toString()
93     {
94         String JavaDoc s = "Zip File Name: " + zipFilename + "\n";//NOI18N
95
//s += "***** File Contents *********\n";//NOI18N
96
//s += ContainerHelper.toOneString(getFileNames());
97

98         return s;
99     }
100
101     //////////////////////////////////////////////////////////////////////////////////////////////////////////////
102

103     /*public String[] getFileNames()
104     {
105         Reporter.assertIt(files); //NOI18N
106         
107         return ContainerHelper.toStringArray(files);
108     }*/

109
110     //////////////////////////////////////////////////////////////////////////////////////////////////////////////
111

112     public String JavaDoc[] explode() throws ZipFileException
113     {
114         ArrayList JavaDoc explodedFiles = new ArrayList JavaDoc();
115
116         //createDirs();
117

118         ZipInputStream JavaDoc zin = null;
119
120         // OK -- at this point, we have a good zip file, and a list of all the files in the zip file.
121
// We've created all the subdirectories needed to explode the files into.
122
// let's get busy...
123

124         try
125         {
126             zin = zipStream; // new ZipInputStream(new FileInputStream(zipFile));
127
ZipEntry JavaDoc ze;
128
129             while( (ze = zin.getNextEntry()) != null )
130             {
131                 String JavaDoc filename = ze.getName();
132                 
133                 if(isManifest(filename))
134                 {
135                     continue; // don't bother with manifest file...
136
}
137                                     
138                                 File JavaDoc fullpath = new File JavaDoc(explodeDir, filename);
139                                 File JavaDoc newDir = fullpath.getParentFile();
140             
141                                 if(newDir.mkdirs())
142                                 { // note: it returns false if dir already exists...
143
Reporter.verbose("Created new directory: " + newDir);//NOI18N
144
}
145
146                                 if(fullpath.delete()) // wipe-out pre-existing files
147
Reporter.info("deleted pre-existing file: " + fullpath); //NOI18N
148

149             //assertIt(newDir.exists() && newDir.isDirectory(), "Couldn't create directory: " + newDir);//NOI18N
150
FileOutputStream JavaDoc os = getOutputStream(filename);
151
152                 if(os == null) // e.g. if we asked to write to a directory instead of a file...
153
continue;
154
155                 int totalBytes = 0;
156
157                 for(int numBytes = zin.read(buffer); numBytes > 0; numBytes = zin.read(buffer))
158                 {
159                     os.write(buffer, 0, numBytes);
160                     totalBytes += numBytes;
161                 }
162                 os.close();
163                 Reporter.verbose("Wrote " + totalBytes + " to " + filename);//NOI18N
164
explodedFiles.add(filename);
165             }
166         }
167         catch(IOException JavaDoc e)
168         {
169             throw new ZipFileException(e);
170         }
171         finally
172         {
173             Reporter.verbose("Closing zin...");//NOI18N
174
try
175             {
176                 zin.close();
177             }
178             catch(IOException JavaDoc e)
179             {
180                 throw new ZipFileException("Got an exception while trying to close Jar input stream: " + e);//NOI18N
181
}
182         }
183         Reporter.info("Successfully Exploded " + zipFilename + " to " + explodeDirName);//NOI18N
184
return ContainerHelper.toStringArray(explodedFiles);
185     }
186
187     /***********************************************************************
188     /******************************** Private ******************************
189     /***********************************************************************/

190
191     private void createFileNameList() throws ZipFileException
192     {
193         ZipInputStream JavaDoc zin;
194
195         assertIt( (files != null) ? false : true, "createFileNameList() called a second time. Should only be called once and only once!" );//NOI18N
196
files = new ArrayList JavaDoc();
197         zin = null;
198         try
199         {
200             ZipEntry JavaDoc ze;
201
202             zin = zipStream; // new ZipInputStream( new FileInputStream( zipFile ) );
203
while( (ze = zin.getNextEntry()) != null )
204             {
205                 String JavaDoc name = ze.getName();
206                 zin.closeEntry();
207                 files.add(name);
208             }
209             zin.close();
210         }
211         catch( IOException JavaDoc e)
212         {
213             Reporter.error(e + " " + zipFile);//NOI18N
214
throw new ZipFileException(e);
215         }
216     }
217
218     /////////////////////////////////////////////////////////////////////////////////////////
219

220     private void checkZipFile() throws ZipFileException
221     {
222         assertIt(zipFilename);
223         zipFile = new File JavaDoc( zipFilename );
224         assertIt( zipFile.exists(), "zipFile (" + zipFilename + ") doesn't exist" );//NOI18N
225
assertIt( !zipFile.isDirectory(), "zipFile (" + zipFilename + ") is actually a directory!" );//NOI18N
226
//createFileNameList();
227
}
228
229     /////////////////////////////////////////////////////////////////////////////////////////
230

231     private void checkExplodeDir() throws ZipFileException
232     {
233         File JavaDoc dir;
234
235         assertIt(explodeDirName);
236         explodeDir = new File JavaDoc(explodeDirName);
237         
238         assertIt(explodeDir.exists(), "Target Directory doesn't exist: " + explodeDirName );//NOI18N
239
assertIt(explodeDir.isDirectory(), "Target Directory isn't a directory: " + explodeDirName );//NOI18N
240
assertIt(explodeDir.canWrite(), "Can't write to Target Directory: " + explodeDirName );//NOI18N
241
}
242
243     /////////////////////////////////////////////////////////////////////////////////////////
244

245     private static boolean isSpecial(String JavaDoc filename)
246     {
247         return filename.toUpperCase().startsWith(specialDir.toUpperCase());
248     }
249
250     /////////////////////////////////////////////////////////////////////////////////////////
251

252     private void createDirs() throws ZipFileException
253     {
254         // go through the array of filenames from the zip -- and create the required directory
255
// structure in the explode directory...
256

257         Assertion.check(explodeDir, "Programmer Error -- need to setup explodeDir");//NOI18N
258
Iterator JavaDoc iter = files.iterator();
259         
260         while(iter.hasNext())
261         {
262             String JavaDoc fname = (String JavaDoc) iter.next();
263             File JavaDoc fullpath = new File JavaDoc(explodeDir, fname);
264             File JavaDoc newDir = fullpath.getParentFile();
265             
266             if(newDir.mkdirs())
267             { // note: it returns false if dir already exists...
268
Reporter.verbose("Created new directory: " + newDir);//NOI18N
269
}
270
271             if(fullpath.delete()) // wipe-out pre-existing files
272
Reporter.info("deleted pre-existing file: " + fullpath); //NOI18N
273

274             assertIt(newDir.exists() && newDir.isDirectory(), "Couldn't create directory: " + newDir);//NOI18N
275
}
276     }
277
278     /////////////////////////////////////////////////////////////////////////////////////////
279

280     private FileOutputStream JavaDoc getOutputStream(String JavaDoc filename) throws ZipFileException
281     {
282         Assertion.check(explodeDir, "Programmer Error -- need to setup explodeDir");//NOI18N
283
File JavaDoc f = new File JavaDoc(explodeDir, filename);
284
285         if(f.isDirectory())
286         {
287             Reporter.warn("Weird! A directory is listed as an entry in the jar file -- skipping...");//NOI18N
288
return null;
289         }
290
291         try
292         {
293             return new FileOutputStream JavaDoc(f);
294         }
295         catch(FileNotFoundException JavaDoc e)
296         {
297             throw new ZipFileException(e);
298         }
299         catch(IOException JavaDoc e)
300         {
301             throw new ZipFileException(e);
302         }
303     }
304
305     /////////////////////////////////////////////////////////////////////////////////////////
306

307     private boolean isManifest(String JavaDoc filename)
308     {
309         if(filename.toLowerCase().endsWith("manifest.mf"))//NOI18N
310
return false;
311         
312         return false;
313     }
314
315     /////////////////////////////////////////////////////////////////////////////////////////
316
//////////// //////////////////////////
317
//////////// Internal Error-Checking Stuff //////////////////////////
318
//////////// //////////////////////////
319
/////////////////////////////////////////////////////////////////////////////////////////
320

321     private static void pr(String JavaDoc s)
322     {
323         System.out.println( s ); //NOI18N
324
}
325
326     /////////////////////////////////////////////////////////////////////////////////////////
327

328     private static void assertIt(String JavaDoc s) throws ZipFileException
329     {
330         if( s == null || s.length() < 0 )
331             throw new ZipFileException();
332         else
333             return;
334     }
335
336     /////////////////////////////////////////////////////////////////////////////////////////
337

338     private static void assertIt(String JavaDoc s, String JavaDoc mesg) throws ZipFileException
339     {
340         if( s == null || s.length() < 0 )
341             throw new ZipFileException( mesg );
342         else
343             return;
344     }
345
346     /////////////////////////////////////////////////////////////////////////////////////////
347

348     private static void assertIt(boolean b) throws ZipFileException
349     {
350         if( !b )
351             throw new ZipFileException();
352         else
353             return;
354     }
355
356     /////////////////////////////////////////////////////////////////////////////////////////
357

358     private static void assertIt(boolean b, String JavaDoc mesg) throws ZipFileException
359     {
360         if( !b )
361             throw new ZipFileException( mesg );
362         else
363             return;
364     }
365
366     /////////////////////////////////////////////////////////////////////////////////////////
367

368     public static void main(String JavaDoc[] String_1darray1)
369     {
370         try
371         {
372             ZipFile zip = new ZipFile("D:\\test\\AccessorTestEnterpriseBean.jar", "D:/test/zipOut");//NOI18N
373

374             pr("" + zip);//NOI18N
375
zip.explode();
376         }
377         catch(ZipFileException e)
378         {
379             pr("ZipFileException: " + e);//NOI18N
380
}
381     }
382
383     /////////////////////////////////////////////////////////////////////////////////////////
384

385     private String JavaDoc zipFilename = null;
386     private String JavaDoc explodeDirName = null;
387     private File JavaDoc zipFile = null;
388     private File JavaDoc explodeDir = null;
389     private ArrayList JavaDoc files = null;
390     private static final String JavaDoc specialDir = "META-INF/";//NOI18N
391
private byte[] buffer = new byte[16384];
392         private ZipInputStream JavaDoc zipStream = null;
393 }
394
Popular Tags