KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > vfs > ZipVFileAccessor


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.vfs;
9
10 import java.io.InputStream JavaDoc;
11 import java.util.zip.ZipFile JavaDoc;
12 import java.util.zip.ZipEntry JavaDoc;
13
14 /**
15  * Accessor for Zip file VFS.
16  *
17  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
18  */

19 public final class ZipVFileAccessor
20     implements VFileAccessor
21 {
22     /**
23      * Get size of resource in bytes.
24      *
25      * @param resource the resource designator
26      * @return the size of resource in bytes
27      */

28     public long getSize( final VFile file, final Object JavaDoc resource )
29     {
30         try
31         {
32             final ZipFile JavaDoc zipFile = (ZipFile JavaDoc)resource;
33             final String JavaDoc name = file.toString().substring( 1 );
34             final ZipEntry JavaDoc entry = zipFile.getEntry( name );
35             return entry.getSize();
36         }
37         catch( final Exception JavaDoc e )
38         {
39             return -1;
40         }
41     }
42
43     /**
44      * Get InputStream for resource.
45      *
46      * @param resource the resource designator
47      * @return the input stream
48      * @exception IOException if an error occurs
49      */

50     public InputStream JavaDoc getInputStream( final VFile file, final Object JavaDoc resource )
51     {
52         try
53         {
54             final ZipFile JavaDoc zipFile = (ZipFile JavaDoc)resource;
55             final String JavaDoc name = file.toString().substring( 1 );
56             final ZipEntry JavaDoc entry = zipFile.getEntry( name );
57             return zipFile.getInputStream( entry );
58         }
59         catch( final Exception JavaDoc e )
60         {
61             return null;
62         }
63     }
64 }
65
Popular Tags