1 18 package org.apache.tools.ant.types.resources; 19 20 import java.io.File ; 21 import java.io.InputStream ; 22 import java.io.OutputStream ; 23 import java.io.IOException ; 24 import java.io.FilterInputStream ; 25 26 import org.apache.tools.ant.Project; 27 import org.apache.tools.ant.BuildException; 28 import org.apache.tools.ant.types.Resource; 29 import org.apache.tools.ant.types.ResourceCollection; 30 import org.apache.tools.ant.types.Reference; 31 import org.apache.tools.ant.util.FileUtils; 32 import org.apache.tools.zip.ZipFile; 33 import org.apache.tools.zip.ZipEntry; 34 35 39 public class ZipResource extends ArchiveResource { 40 41 private String encoding; 42 43 46 public ZipResource() { 47 } 48 49 56 public ZipResource(File z, String enc, ZipEntry e) { 57 super(z, true); 58 setEncoding(enc); 59 setEntry(e); 60 } 61 62 66 public void setZipfile(File z) { 67 setArchive(z); 68 } 69 70 74 public File getZipfile() { 75 FileResource r = (FileResource) getArchive(); 76 return r.getFile(); 77 } 78 79 84 public void addConfigured(ResourceCollection a) { 85 super.addConfigured(a); 86 if (!a.isFilesystemOnly()) { 87 throw new BuildException("only filesystem resources are supported"); 88 } 89 } 90 91 95 public void setEncoding(String enc) { 96 checkAttributesAllowed(); 97 encoding = enc; 98 } 99 100 104 public String getEncoding() { 105 return isReference() 106 ? ((ZipResource) getCheckedRef()).getEncoding() : encoding; 107 } 108 109 113 public void setRefid(Reference r) { 114 if (encoding != null) { 115 throw tooManyAttributes(); 116 } 117 super.setRefid(r); 118 } 119 120 126 public InputStream getInputStream() throws IOException { 127 if (isReference()) { 128 return ((Resource) getCheckedRef()).getInputStream(); 129 } 130 final ZipFile z = new ZipFile(getZipfile(), getEncoding()); 131 ZipEntry ze = z.getEntry(getName()); 132 if (ze == null) { 133 z.close(); 134 throw new BuildException("no entry " + getName() + " in " 135 + getArchive()); 136 } 137 return new FilterInputStream (z.getInputStream(ze)) { 138 public void close() throws IOException { 139 FileUtils.close(in); 140 z.close(); 141 } 142 protected void finalize() throws Throwable { 143 try { 144 close(); 145 } finally { 146 super.finalize(); 147 } 148 } 149 }; 150 } 151 152 160 public OutputStream getOutputStream() throws IOException { 161 if (isReference()) { 162 return ((Resource) getCheckedRef()).getOutputStream(); 163 } 164 throw new UnsupportedOperationException ( 165 "Use the zip task for zip output."); 166 } 167 168 171 protected void fetchEntry() { 172 ZipFile z = null; 173 try { 174 z = new ZipFile(getZipfile(), getEncoding()); 175 setEntry(z.getEntry(getName())); 176 } catch (IOException e) { 177 log(e.getMessage(), Project.MSG_DEBUG); 178 throw new BuildException(e); 179 } finally { 180 if (z != null) { 181 try { 182 z.close(); 183 } catch (IOException e) { 184 } 186 } 187 } 188 } 189 190 private void setEntry(ZipEntry e) { 191 if (e == null) { 192 setExists(false); 193 return; 194 } 195 setName(e.getName()); 196 setExists(true); 197 setLastModified(e.getTime()); 198 setDirectory(e.isDirectory()); 199 setSize(e.getSize()); 200 setMode(e.getUnixMode()); 201 } 202 203 } 204 | Popular Tags |