001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.lenya.cms.cocoon.source;
018:
019: //package org.apache.cocoon.components.source.impl;
020:
021: import java.io.ByteArrayInputStream;
022: import java.io.ByteArrayOutputStream;
023: import java.io.IOException;
024: import java.io.InputStream;
025: import java.util.zip.ZipEntry;
026: import java.util.zip.ZipInputStream;
027:
028: import org.apache.avalon.framework.logger.AbstractLogEnabled;
029: import org.apache.cocoon.util.MIMEUtils;
030: import org.apache.excalibur.source.Source;
031: import org.apache.excalibur.source.SourceNotFoundException;
032: import org.apache.excalibur.source.SourceValidity;
033:
034: /**
035: * @author <a href="http://apache.org/~reinhard">Reinhard Poetz</a>
036: * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
037: * @version CVS $Id: ZipSource.java 30932 2004-07-29 17:35:38Z vgritsenko $
038: * @since 2.1.4
039: */
040: public class ZipSource extends AbstractLogEnabled implements Source {
041:
042: Source archive;
043: String documentName;
044:
045: /**
046: * Ctor.
047: * @param archive The archive source.
048: * @param fileName The filename.
049: */
050: public ZipSource(Source archive, String fileName) {
051: this .archive = archive;
052: this .documentName = fileName;
053: }
054:
055: public boolean exists() {
056: if (!this .archive.exists()) {
057: return false;
058: }
059: ZipInputStream zipStream = null;
060: ZipEntry document = null;
061: boolean found = false;
062: try {
063: zipStream = new ZipInputStream(this .archive
064: .getInputStream());
065: do {
066: document = zipStream.getNextEntry();
067: if (document != null) {
068: if (document.getName().equals(this .documentName)) {
069: found = true;
070: } else {
071: zipStream.closeEntry();
072: }
073: }
074: } while (document != null && found == false);
075: } catch (IOException ioe) {
076: return false;
077: } finally {
078: try {
079: zipStream.close();
080: } catch (IOException ioe) {
081: this .getLogger().error(
082: "Error while closing ZipInputStream: "
083: + this .documentName);
084: }
085: }
086: return found;
087: }
088:
089: public InputStream getInputStream() throws IOException,
090: SourceNotFoundException {
091:
092: ZipInputStream zipStream = new ZipInputStream(this .archive
093: .getInputStream());
094: ZipEntry document = null;
095: boolean found = false;
096: do {
097: document = zipStream.getNextEntry();
098: if (document != null) {
099: if (document.getName().equals(this .documentName)) {
100: found = true;
101: } else {
102: // go to next entry
103: zipStream.closeEntry();
104: }
105: }
106: } while (document != null && found == false);
107:
108: if (document == null) {
109: throw new SourceNotFoundException("The document "
110: + documentName + " is not in the archive "
111: + this .archive.getURI());
112: }
113:
114: // now we will extract the document and write it into a byte array
115: ByteArrayOutputStream baos = new ByteArrayOutputStream();
116: byte[] buffer = new byte[8192];
117: int length = -1;
118: while (zipStream.available() > 0) {
119: length = zipStream.read(buffer, 0, 8192);
120: if (length > 0) {
121: baos.write(buffer, 0, length);
122: }
123: }
124: zipStream.close();
125: baos.flush();
126:
127: // return an input stream
128: return new ByteArrayInputStream(baos.toByteArray());
129: }
130:
131: public String getURI() {
132: return this .archive.getURI() + "/" + this .documentName;
133: }
134:
135: public String getScheme() {
136: return ZipSourceFactory.ZIP_SOURCE_SCHEME;
137: }
138:
139: public SourceValidity getValidity() {
140: return this .archive.getValidity();
141: }
142:
143: public void refresh() {
144: }
145:
146: public String getMimeType() {
147: String ext = this .documentName.substring(this .documentName
148: .lastIndexOf("."));
149: return MIMEUtils.getMIMEType(ext);
150: }
151:
152: public long getContentLength() {
153: return -1;
154: }
155:
156: public long getLastModified() {
157: return this.archive.getLastModified();
158: }
159:
160: }
|