FileDocCategorySizeDatePackage
ZipResource.javaAPI DocApache Ant 1.706112Wed Dec 13 06:16:20 GMT 2006org.apache.tools.ant.types.resources

ZipResource

public class ZipResource extends ArchiveResource
A Resource representation of an entry in a zipfile.
since
Ant 1.7

Fields Summary
private String
encoding
Constructors Summary
public ZipResource()
Default constructor.

    
public ZipResource(File z, String enc, org.apache.tools.zip.ZipEntry e)
Construct a ZipResource representing the specified entry in the specified zipfile.

param
z the zipfile as File.
param
enc the encoding used for filenames.
param
e the ZipEntry.

        super(z, true);
        setEncoding(enc);
        setEntry(e);
    
Methods Summary
public voidaddConfigured(org.apache.tools.ant.types.ResourceCollection a)
Sets the archive that holds this as a single element Resource collection.

param
a the archive as a single element Resource collection.

        super.addConfigured(a);
        if (!a.isFilesystemOnly()) {
            throw new BuildException("only filesystem resources are supported");
        }
    
protected voidfetchEntry()
fetches information from the named entry inside the archive.

        ZipFile z = null;
        try {
            z = new ZipFile(getZipfile(), getEncoding());
            setEntry(z.getEntry(getName()));
        } catch (IOException e) {
            log(e.getMessage(), Project.MSG_DEBUG);
            throw new BuildException(e);
        } finally {
            if (z != null) {
                try {
                    z.close();
                } catch (IOException e) {
                    //?
                }
            }
        }
    
public java.lang.StringgetEncoding()
Get the encoding to use with the zipfile.

return
String encoding.

        return isReference()
            ? ((ZipResource) getCheckedRef()).getEncoding() : encoding;
    
public java.io.InputStreamgetInputStream()
Return an InputStream for reading the contents of this Resource.

return
an InputStream object.
throws
IOException if the zip file cannot be opened, or the entry cannot be read.

        if (isReference()) {
            return ((Resource) getCheckedRef()).getInputStream();
        }
        final ZipFile z = new ZipFile(getZipfile(), getEncoding());
        ZipEntry ze = z.getEntry(getName());
        if (ze == null) {
            z.close();
            throw new BuildException("no entry " + getName() + " in "
                                     + getArchive());
        }
        return new FilterInputStream(z.getInputStream(ze)) {
            public void close() throws IOException {
                FileUtils.close(in);
                z.close();
            }
            protected void finalize() throws Throwable {
                try {
                    close();
                } finally {
                    super.finalize();
                }
            }
        };
    
public java.io.OutputStreamgetOutputStream()
Get an OutputStream for the Resource.

return
an OutputStream to which content can be written.
throws
IOException if unable to provide the content of this Resource as a stream.
throws
UnsupportedOperationException if OutputStreams are not supported for this Resource type.

        if (isReference()) {
            return ((Resource) getCheckedRef()).getOutputStream();
        }
        throw new UnsupportedOperationException(
            "Use the zip task for zip output.");
    
public java.io.FilegetZipfile()
Get the zipfile that holds this ZipResource.

return
the zipfile as a File.

        FileResource r = (FileResource) getArchive();
        return r.getFile();
    
public voidsetEncoding(java.lang.String enc)
Set the encoding to use with the zipfile.

param
enc the String encoding.

        checkAttributesAllowed();
        encoding = enc;
    
private voidsetEntry(org.apache.tools.zip.ZipEntry e)

        if (e == null) {
            setExists(false);
            return;
        }
        setName(e.getName());
        setExists(true);
        setLastModified(e.getTime());
        setDirectory(e.isDirectory());
        setSize(e.getSize());
        setMode(e.getUnixMode());
    
public voidsetRefid(org.apache.tools.ant.types.Reference r)
Overrides the super version.

param
r the Reference to set.

        if (encoding != null) {
            throw tooManyAttributes();
        }
        super.setRefid(r);
    
public voidsetZipfile(java.io.File z)
Set the zipfile that holds this ZipResource.

param
z the zipfile as a File.

        setArchive(z);