KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > utils > jar > JahiaJarHandler


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12
//
13
//
14
// JahiaJarHandler (Deprecated !!! use zip utils)
15
//
16
// NK 15.01.2001
17
//
18
//
19

20
21 package org.jahia.utils.jar;
22
23
24 import java.io.BufferedInputStream JavaDoc;
25 import java.io.BufferedOutputStream JavaDoc;
26 import java.io.File JavaDoc;
27 import java.io.FileInputStream JavaDoc;
28 import java.io.FileOutputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.io.OutputStream JavaDoc;
32 import java.util.jar.JarFile JavaDoc;
33 import java.util.zip.ZipEntry JavaDoc;
34 import java.util.zip.ZipFile JavaDoc;
35 import java.util.zip.ZipInputStream JavaDoc;
36
37 import org.jahia.exceptions.JahiaException;
38 import org.jahia.utils.JahiaConsole;
39
40 /**
41  * A Wrapper to handle some manipulations on a .jar file
42  * (Deprecated !!! use zip utils)
43  * @author Khue ng
44  */

45 public class JahiaJarHandler {
46
47    /** The full path to the file **/
48    private String JavaDoc m_FilePath;
49    /** The JarFile object **/
50    private JarFile JavaDoc m_JarFile;
51
52    /**
53     * Constructor
54     *
55     * @param (String) path, the full path to the file
56     * @exception IOException
57     */

58    public JahiaJarHandler ( String JavaDoc path ) throws IOException JavaDoc {
59
60       m_FilePath = path;
61       File JavaDoc f = new File JavaDoc(path);
62       try {
63
64          m_JarFile = new JarFile JavaDoc(f);
65
66       } catch ( IOException JavaDoc ioe ) {
67          JahiaConsole.println("JahiaJarHandler","JahiaJarHandler IOException occurred " + f.getName() );
68          throw new IOException JavaDoc ("JahiaJarHandler, IOException occurred ");
69       } catch ( java.lang.NullPointerException JavaDoc e ) {
70          JahiaConsole.println("JahiaJarHandler","JahiaJarHandler NullPointerException " + f.getName() );
71          throw new IOException JavaDoc ("JahiaJarHandler, NullPointerException occurred ");
72       }
73
74       if ( m_JarFile == null ) {
75
76          throw new IOException JavaDoc ("JahiaJarHandler, referred file is null");
77       }
78
79    }
80
81
82    /**
83     * Decompresses the file in it's current location
84     *
85     */

86    public void unzip() throws JahiaException {
87
88       try {
89
90          File JavaDoc f = new File JavaDoc(m_FilePath);
91
92          JahiaConsole.println("JahiaJarHandler"," Start Decompressing " + f.getName() );
93
94          String JavaDoc parentPath = f.getParent() + File.separator;
95          String JavaDoc path = null;
96          File JavaDoc fileItem = null;
97
98          FileInputStream JavaDoc fis = new FileInputStream JavaDoc(m_FilePath);
99          BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(fis);
100          ZipInputStream JavaDoc zis = new ZipInputStream JavaDoc(bis);
101          ZipFile JavaDoc zf = new ZipFile JavaDoc(m_FilePath);
102          ZipEntry JavaDoc ze = null;
103          String JavaDoc zeName = null;
104
105          try{
106
107             while ( (ze = zis.getNextEntry()) != null ){
108                zeName = ze.getName();
109                path = parentPath + genPathFile(zeName);
110                if ( ze.isDirectory() ){
111                   File JavaDoc fo = new File JavaDoc(path);
112                   fo.mkdirs();
113                } else {
114                   File JavaDoc fo = new File JavaDoc(path);
115                   copyStream(zis,new FileOutputStream JavaDoc(fo));
116                }
117             }
118          } finally {
119
120             // Important !!!
121
zf.close();
122             fis.close();
123             zis.close();
124             bis.close();
125          }
126
127
128          JahiaConsole.println("JahiaJarHandler"," Decompressing " + f.getName() + " done ! ");
129
130       } catch ( IOException JavaDoc ioe ) {
131
132          JahiaConsole.println("JahiaJarHandler"," fail unzipping " + ioe.getMessage() );
133
134          throw new JahiaException ("JahiaJarHandler", "faile processing unzip",
135                                     JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY);
136
137       }
138
139    }
140
141
142    /**
143     * Decompress the file in a gived folder
144     *
145     * @param (String) path
146     */

147    public void unzip(String JavaDoc path) throws JahiaException {
148
149       try {
150
151          File JavaDoc f = new File JavaDoc(m_FilePath);
152
153          JahiaConsole.println("JahiaJarHandler"," Start Decompressing " + f.getName() );
154
155          String JavaDoc destPath = null;
156          File JavaDoc fileItem = null;
157
158          FileInputStream JavaDoc fis = new FileInputStream JavaDoc(m_FilePath);
159          BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(fis);
160          ZipInputStream JavaDoc zis = new ZipInputStream JavaDoc(bis);
161          ZipFile JavaDoc zf = new ZipFile JavaDoc(m_FilePath);
162          ZipEntry JavaDoc ze = null;
163          String JavaDoc zeName = null;
164
165          try {
166
167             while ( (ze = zis.getNextEntry()) != null ){
168                zeName = ze.getName();
169                destPath = path + File.separator + genPathFile(zeName);
170                if ( ze.isDirectory() ){
171                   File JavaDoc fo = new File JavaDoc(destPath);
172                   fo.mkdirs();
173                   fo = null;
174                } else {
175                   File JavaDoc fo = new File JavaDoc(destPath);
176                   FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(fo);
177                   copyStream(zis,fos);
178                   fos.close();
179                   fo = null;
180                }
181             }
182          } finally {
183
184             // Important !!!
185
zf.close();
186             fis.close();
187             zis.close();
188             bis.close();
189          }
190
191          JahiaConsole.println("JahiaJarHandler"," Decompressing " + f.getName() + " done ! ");
192
193       } catch ( IOException JavaDoc ioe ) {
194
195          JahiaConsole.println("JahiaJarHandler"," fail unzipping " + ioe.getMessage() );
196
197          throw new JahiaException ("JahiaJarHandler", "faile processing unzip",
198                                     JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY);
199
200       }
201
202    }
203
204
205    /**
206     * Extract an entry of file type in the jar file
207     * Return a File Object reference to the uncompressed file
208     *
209     * @param (String) entryName, the entry name
210     * @return (File) fo, a File Handler to the file ( It's a temporary file )
211     */

212    public File JavaDoc extractFile( String JavaDoc entryName ) throws IOException JavaDoc {
213
214       File JavaDoc tmpFile = null;
215
216
217       // Create a temporary file and write the content of the file in it
218
ZipEntry JavaDoc entry = m_JarFile.getEntry(entryName);
219
220       if ( (entry != null) && !entry.isDirectory() ) {
221
222          InputStream JavaDoc ins = m_JarFile.getInputStream(entry);
223
224          if ( ins != null ){
225             File JavaDoc f = new File JavaDoc("");
226             tmpFile = File.createTempFile("tmpfile","");
227             if ( tmpFile == null || !tmpFile.canWrite() ){
228
229                throw new IOException JavaDoc ("extractFile error creating temporary file");
230
231             }
232
233             copyStream(ins,new FileOutputStream JavaDoc(tmpFile));
234
235          }
236       } else {
237          JahiaConsole.println("JahiaJarHandler", "extractFile(entry), " + entryName + " is null or a directory " );
238          throw new IOException JavaDoc ("extractFileEntry cannot find an entry file of name " + entryName);
239       }
240
241       return tmpFile;
242
243    }
244
245
246    /**
247     * Extract an entry in a gived folder. If this entry is a directory,
248     * all its contents are extracted too.
249     *
250     * @param (String) entryName, the name of an entry in the jar
251     * @param (String) destPath, the path to the destination folder
252     */

253    public void extractEntry( String JavaDoc entryName,
254                              String JavaDoc destPath ) throws JahiaException {
255
256       try {
257
258          ZipEntry JavaDoc entry = m_JarFile.getEntry(entryName);
259
260          if ( entry == null ){
261             StringBuffer JavaDoc strBuf = new StringBuffer JavaDoc(1024);
262             strBuf.append(" extractEntry(), cannot find entry ");
263             strBuf.append(entryName);
264             strBuf.append(" in the jar file ");
265             JahiaConsole.println("JahiaJarHandler", strBuf.toString());
266
267             throw new JahiaException ("JahiaJarHandler", strBuf.toString(),
268                                     JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY);
269
270          }
271
272          File JavaDoc destDir = new File JavaDoc(destPath);
273          if ( destDir == null || !destDir.isDirectory() || !destDir.canWrite() ){
274
275             JahiaConsole.println("JahiaJarHandler"," extractEntry(), cannot access to the destination dir ");
276
277             throw new JahiaException ("JahiaJarHandler", " extractEntry(), cannot access to the destination dir ",
278                                     JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY);
279          }
280
281
282          File JavaDoc f = new File JavaDoc(m_FilePath);
283
284          JahiaConsole.println("JahiaJarHandler"," Start extractEntry(entryName,path) Decompressing entry " + entryName );
285
286          String JavaDoc path = null;
287          File JavaDoc fileItem = null;
288
289          FileInputStream JavaDoc fis = new FileInputStream JavaDoc(m_FilePath);
290          BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(fis);
291          ZipInputStream JavaDoc zis = new ZipInputStream JavaDoc(bis);
292          ZipFile JavaDoc zf = new ZipFile JavaDoc(m_FilePath);
293          ZipEntry JavaDoc ze = null;
294          String JavaDoc zeName = null;
295
296          while ( (ze = zis.getNextEntry()) != null && !ze.getName().equals(entryName) ) {
297             // loop until the requested entry
298
JahiaConsole.println("JahiaJarHandler","extractEntry(extryName, path), bypass " + ze.getName() );
299          }
300
301          try{
302
303             while ( ze != null ){
304                zeName = ze.getName();
305                path = destPath + File.separator + genPathFile(zeName);
306                if ( ze.isDirectory() ){
307                   File JavaDoc fo = new File JavaDoc(path);
308                   fo.mkdirs();
309                } else {
310                   File JavaDoc fo = new File JavaDoc(path);
311                   copyStream(zis,new FileOutputStream JavaDoc(fo));
312                }
313
314                ze = zis.getNextEntry();
315             }
316
317          } finally {
318
319             // Important !!!
320
zf.close();
321             fis.close();
322             zis.close();
323             bis.close();
324          }
325
326
327          JahiaConsole.println("JahiaJarHandler"," Decompressing " + f.getName() + " done ! ");
328
329       } catch ( IOException JavaDoc ioe ) {
330
331          JahiaConsole.println("JahiaJarHandler"," fail unzipping " + ioe.getMessage() );
332
333          throw new JahiaException ("JahiaJarHandler", "faile processing unzip",
334                                     JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY);
335
336       }
337
338    }
339
340
341    /**
342     * Return an entry in the jar file of the gived name or null if not found
343     *
344     * @param (String) entryName the entry name
345     * @return (ZipEntry) the entry
346     */

347    public ZipEntry JavaDoc getEntry(String JavaDoc entryName){
348
349       return m_JarFile.getEntry(entryName);
350
351    }
352
353
354    /**
355     * Check if an entry is an directory or not
356     *
357     * @param (String) entryName the entry name
358     * @return (boolean) true if the entry exists and is a a directory
359     */

360    public boolean isDirectory(String JavaDoc entryName){
361       return (( m_JarFile.getEntry(entryName) != null) && m_JarFile.getEntry(entryName).isDirectory());
362    }
363
364    /**
365     * Check if an entry exist or not
366     *
367     * @param (String) entryName the entry name
368     * @return (boolean) true if exist
369     */

370    public boolean entryExists(String JavaDoc entryName){
371
372       if ( m_JarFile.getEntry(entryName) != null ){
373          return true;
374       }
375       return false;
376
377    }
378
379
380    /**
381     * Close the Zip file. Important to close the JarFile object
382     * to be able to delete it from disk.
383     *
384     */

385    public void closeJarFile(){
386
387       try {
388          m_JarFile.close();
389       } catch ( IOException JavaDoc e ) {
390          JahiaConsole.println("JahiaJarHandler","cannot close jar file");
391          // cannot close file
392
}
393
394    }
395
396
397    /**
398     * Generates a path file for a gived entry name
399     * Parses "/" char and replaces them with File.separator char
400     *
401     */

402    protected String JavaDoc genPathFile(String JavaDoc entryName){
403
404       StringBuffer JavaDoc sb = new StringBuffer JavaDoc(entryName.length());
405       for ( int i= 0; i< entryName.length() ; i++ ){
406          if ( entryName.charAt(i) == '/' ){
407             sb.append(File.separator);
408          } else {
409             sb.append(entryName.charAt(i));
410          }
411       }
412
413       return ( sb.toString() );
414
415    }
416
417
418     /**
419     * Copy an InputStream to an OutPutStream
420     *
421     * @param ins An InputStream.
422     * @param outs An OutputStream.
423     * @exception IOException.
424     */

425    protected void copyStream( InputStream JavaDoc ins,
426                             OutputStream JavaDoc outs)
427        throws IOException JavaDoc
428    {
429       int bufferSize = 1024;
430       byte[] writeBuffer = new byte[bufferSize];
431
432       BufferedOutputStream JavaDoc bos =
433          new BufferedOutputStream JavaDoc(outs, bufferSize);
434       int bufferRead;
435       while((bufferRead = ins.read(writeBuffer)) != -1)
436          bos.write(writeBuffer,0,bufferRead);
437       bos.flush();
438       bos.close();
439    }
440
441
442
443
444
445 } // End Class JahiaJarHandler
446
Popular Tags