KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > source > impl > ZipSource


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation or its licensors,
3  * as applicable.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.cocoon.components.source.impl;
18
19 import java.io.ByteArrayInputStream JavaDoc;
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.util.zip.ZipEntry JavaDoc;
24 import java.util.zip.ZipInputStream JavaDoc;
25
26 import org.apache.avalon.framework.logger.AbstractLogEnabled;
27 import org.apache.cocoon.util.MIMEUtils;
28 import org.apache.excalibur.source.Source;
29 import org.apache.excalibur.source.SourceNotFoundException;
30 import org.apache.excalibur.source.SourceValidity;
31
32 /**
33   * ATTENTION: The ZIP protocol is also part of Cocoon 2.1.4 (scratchpad). When
34   * Forrest uses this version or higher this file can be removed!!! (RP)
35   *
36   * @since 2.1.4
37   */

38 public class ZipSource extends AbstractLogEnabled implements Source {
39
40     Source archive;
41     String JavaDoc documentName;
42
43     public ZipSource(Source archive, String JavaDoc fileName) {
44         this.archive = archive;
45         this.documentName = fileName;
46     }
47
48     public boolean exists() {
49         if(!this.archive.exists()) {
50             return false;
51         }
52         ZipInputStream JavaDoc zipStream = null;
53         ZipEntry JavaDoc document = null;
54         boolean found = false;
55         try {
56             zipStream = new ZipInputStream JavaDoc(this.archive.getInputStream());
57             do {
58                 document = zipStream.getNextEntry();
59                 if (document != null) {
60                     if (document.getName().equals(this.documentName)) {
61                         found = true;
62                     } else {
63                         zipStream.closeEntry();
64                     }
65                 }
66             } while (document != null && found == false);
67         } catch(IOException JavaDoc ioe) {
68             return false;
69         } finally {
70             try {
71                 zipStream.close();
72             } catch (IOException JavaDoc ioe) {
73                 this.getLogger().error("Error while closing ZipInputStream: " + this.documentName);
74             }
75         }
76         return found;
77     }
78     
79     public InputStream JavaDoc getInputStream()
80         throws IOException JavaDoc, SourceNotFoundException {
81
82         ZipInputStream JavaDoc zipStream =
83             new ZipInputStream JavaDoc(this.archive.getInputStream());
84         ZipEntry JavaDoc document = null;
85         boolean found = false;
86         do {
87             document = zipStream.getNextEntry();
88             if (document != null) {
89                 if (document.getName().equals(this.documentName)) {
90                     found = true;
91                 } else {
92                     // go to next entry
93
zipStream.closeEntry();
94                 }
95             }
96         } while (document != null && found == false);
97
98         if (document == null) {
99             throw new SourceNotFoundException(
100                 "The document "
101                     + documentName
102                     + " is not in the archive "
103                     + this.archive.getURI());
104         }
105
106         // now we will extract the document and write it into a byte array
107
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
108         byte[] buffer = new byte[8192];
109         int length = -1;
110         while (zipStream.available() > 0) {
111             length = zipStream.read(buffer, 0, 8192);
112             if (length > 0) {
113                 baos.write(buffer, 0, length);
114             }
115         }
116         zipStream.close();
117         baos.flush();
118
119         // return an input stream
120
return new ByteArrayInputStream JavaDoc(baos.toByteArray());
121     }
122
123     public String JavaDoc getURI() {
124         return this.archive.getURI() + "/" + this.documentName;
125     }
126
127     public String JavaDoc getScheme() {
128         return ZipSourceFactory.ZIP_SOURCE_SCHEME;
129     }
130
131     public SourceValidity getValidity() {
132         return this.archive.getValidity();
133     }
134
135     public void refresh() {
136     }
137
138     public String JavaDoc getMimeType() {
139         String JavaDoc ext = this.documentName.substring( this.documentName.lastIndexOf(".") );
140         return MIMEUtils.getMIMEType( ext );
141     }
142
143     public long getContentLength() {
144         return -1;
145     }
146
147     public long getLastModified() {
148         return this.archive.getLastModified();
149     }
150
151 }
152
Popular Tags