KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > resources > ZipResource


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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  */

18 package org.apache.tools.ant.types.resources;
19
20 import java.io.File JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.FilterInputStream JavaDoc;
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 /**
36  * A Resource representation of an entry in a zipfile.
37  * @since Ant 1.7
38  */

39 public class ZipResource extends ArchiveResource {
40
41     private String JavaDoc encoding;
42
43     /**
44      * Default constructor.
45      */

46     public ZipResource() {
47     }
48
49     /**
50      * Construct a ZipResource representing the specified
51      * entry in the specified zipfile.
52      * @param z the zipfile as File.
53      * @param enc the encoding used for filenames.
54      * @param e the ZipEntry.
55      */

56     public ZipResource(File JavaDoc z, String JavaDoc enc, ZipEntry e) {
57         super(z, true);
58         setEncoding(enc);
59         setEntry(e);
60     }
61
62     /**
63      * Set the zipfile that holds this ZipResource.
64      * @param z the zipfile as a File.
65      */

66     public void setZipfile(File JavaDoc z) {
67         setArchive(z);
68     }
69
70     /**
71      * Get the zipfile that holds this ZipResource.
72      * @return the zipfile as a File.
73      */

74     public File JavaDoc getZipfile() {
75         FileResource r = (FileResource) getArchive();
76         return r.getFile();
77     }
78
79     /**
80      * Sets the archive that holds this as a single element Resource
81      * collection.
82      * @param a the archive as a single element Resource collection.
83      */

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     /**
92      * Set the encoding to use with the zipfile.
93      * @param enc the String encoding.
94      */

95     public void setEncoding(String JavaDoc enc) {
96         checkAttributesAllowed();
97         encoding = enc;
98     }
99
100     /**
101      * Get the encoding to use with the zipfile.
102      * @return String encoding.
103      */

104     public String JavaDoc getEncoding() {
105         return isReference()
106             ? ((ZipResource) getCheckedRef()).getEncoding() : encoding;
107     }
108
109     /**
110      * Overrides the super version.
111      * @param r the Reference to set.
112      */

113     public void setRefid(Reference r) {
114         if (encoding != null) {
115             throw tooManyAttributes();
116         }
117         super.setRefid(r);
118     }
119
120     /**
121      * Return an InputStream for reading the contents of this Resource.
122      * @return an InputStream object.
123      * @throws IOException if the zip file cannot be opened,
124      * or the entry cannot be read.
125      */

126     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
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 JavaDoc(z.getInputStream(ze)) {
138             public void close() throws IOException JavaDoc {
139                 FileUtils.close(in);
140                 z.close();
141             }
142             protected void finalize() throws Throwable JavaDoc {
143                 try {
144                     close();
145                 } finally {
146                     super.finalize();
147                 }
148             }
149         };
150     }
151
152     /**
153      * Get an OutputStream for the Resource.
154      * @return an OutputStream to which content can be written.
155      * @throws IOException if unable to provide the content of this
156      * Resource as a stream.
157      * @throws UnsupportedOperationException if OutputStreams are not
158      * supported for this Resource type.
159      */

160     public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
161         if (isReference()) {
162             return ((Resource) getCheckedRef()).getOutputStream();
163         }
164         throw new UnsupportedOperationException JavaDoc(
165             "Use the zip task for zip output.");
166     }
167
168     /**
169      * fetches information from the named entry inside the archive.
170      */

171     protected void fetchEntry() {
172         ZipFile z = null;
173         try {
174             z = new ZipFile(getZipfile(), getEncoding());
175             setEntry(z.getEntry(getName()));
176         } catch (IOException JavaDoc 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 JavaDoc e) {
184                     //?
185
}
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