001: // ZipFileResource.java
002: // $Id: ZipFileResource.java,v 1.5 2000/08/16 21:37:48 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1998.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigsaw.zip;
007:
008: import java.util.zip.ZipEntry;
009: import java.util.zip.ZipFile;
010:
011: import java.io.File;
012: import java.io.IOException;
013: import java.io.InputStream;
014:
015: import org.w3c.tools.resources.Attribute;
016: import org.w3c.tools.resources.AttributeHolder;
017: import org.w3c.tools.resources.AttributeRegistry;
018: import org.w3c.tools.resources.FileAttribute;
019: import org.w3c.tools.resources.FileResource;
020: import org.w3c.tools.resources.Resource;
021: import org.w3c.tools.resources.StringAttribute;
022:
023: /**
024: * @version $Revision: 1.5 $
025: * @author Benoît Mahé (bmahe@w3.org)
026: */
027: public class ZipFileResource extends FileResource {
028:
029: /**
030: * Attributes index - The filename attribute.
031: */
032: protected static int ATTR_ZIPFILE = -1;
033: /**
034: * Attribute index - The index for our entry path.
035: */
036: protected static int ATTR_ENTRYPATH = -1;
037:
038: static {
039: Attribute a = null;
040: Class cls = null;
041: try {
042: cls = Class.forName("org.w3c.jigsaw.zip.ZipFileResource");
043: } catch (Exception ex) {
044: ex.printStackTrace();
045: System.exit(0);
046: }
047: // The zip file attribute.
048: a = new FileAttribute("zipfile", null, Attribute.COMPUTED);
049: ATTR_ZIPFILE = AttributeRegistry.registerAttribute(cls, a);
050: // the entry path attribute
051: a = new StringAttribute("entrypath", null, Attribute.COMPUTED);
052: ATTR_ENTRYPATH = AttributeRegistry.registerAttribute(cls, a);
053: }
054:
055: /**
056: * Get this zip file.
057: */
058: public synchronized File getFile() {
059: return (File) getValue(ATTR_ZIPFILE, null);
060: }
061:
062: public String getEntryPath() {
063: return getString(ATTR_ENTRYPATH, null);
064: }
065:
066: protected synchronized InputStream getInputStream() {
067: try {
068: ZipFile zipfile = new ZipFile(getFile());
069: ZipEntry zentry = zipfile.getEntry(getEntryPath());
070: return new ZipInputStream(zipfile, zipfile
071: .getInputStream(zentry));
072: } catch (IOException ex) {
073: ex.printStackTrace();
074: return null;
075: }
076: }
077:
078: protected synchronized long getEntrySize() {
079: ZipFile z = null;
080: try {
081: z = new ZipFile(getFile());
082: ZipEntry entry = z.getEntry(getEntryPath());
083: if (entry != null)
084: return entry.getSize();
085: else
086: return 0;
087: } catch (IOException ex) {
088: return 0;
089: } finally {
090: try {
091: z.close();
092: } catch (Exception ex) {
093: }
094: }
095: }
096:
097: protected synchronized boolean hasEntry() {
098: ZipFile z = null;
099: try {
100: z = new ZipFile(getFile());
101: return (z.getEntry(getEntryPath()) != null);
102: } catch (IOException ex) {
103: return false;
104: } finally {
105: try {
106: z.close();
107: } catch (Exception ex) {
108: }
109: }
110: }
111:
112: /**
113: * Update the file related attributes.
114: * The file we serve has changed since the last time we checked it, if
115: * any of the attribute values depend on the file content, this is the
116: * appropriate place to recompute them.
117: */
118:
119: public void updateFileAttributes() {
120: File file = getFile();
121: setValue(ATTR_FILESTAMP, new Long(file.lastModified()));
122: setValue(ATTR_FILE_LENGTH, new Integer((int) getEntrySize()));
123: return;
124: }
125:
126: /**
127: * Is that resource still wrapping an existing file ?
128: * If the underlying file has disappeared <string> and if</strong> the
129: * container directory is extensible, remove the resource.
130: * @return A boolean.
131: */
132:
133: public synchronized boolean verify() {
134: File file = getFile();
135: if (!file.exists())
136: return false;
137: return (hasEntry());
138: }
139:
140: /**
141: * Save the given stream as the underlying file content.
142: * This method preserve the old file version in a <code>~</code> file.
143: * @param in The input stream to use as the resource entity.
144: * @return A boolean, <strong>true</strong> if the resource was just
145: * created, <strong>false</strong> otherwise.
146: * @exception IOException If dumping the content failed.
147: */
148:
149: public synchronized boolean newContent(InputStream in)
150: throws IOException {
151: throw new IOException("Can't modify the content of ZipFile");
152: }
153:
154: }
|