1 17 package org.apache.cocoon.components.source.impl; 18 19 import java.io.ByteArrayInputStream ; 20 import java.io.ByteArrayOutputStream ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.util.zip.ZipEntry ; 24 import java.util.zip.ZipInputStream ; 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 38 public class ZipSource extends AbstractLogEnabled implements Source { 39 40 Source archive; 41 String documentName; 42 43 public ZipSource(Source archive, String 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 zipStream = null; 53 ZipEntry document = null; 54 boolean found = false; 55 try { 56 zipStream = new ZipInputStream (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 ioe) { 68 return false; 69 } finally { 70 try { 71 zipStream.close(); 72 } catch (IOException ioe) { 73 this.getLogger().error("Error while closing ZipInputStream: " + this.documentName); 74 } 75 } 76 return found; 77 } 78 79 public InputStream getInputStream() 80 throws IOException , SourceNotFoundException { 81 82 ZipInputStream zipStream = 83 new ZipInputStream (this.archive.getInputStream()); 84 ZipEntry 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 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 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 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 new ByteArrayInputStream (baos.toByteArray()); 121 } 122 123 public String getURI() { 124 return this.archive.getURI() + "/" + this.documentName; 125 } 126 127 public String 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 getMimeType() { 139 String 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 |