001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Charles Reich
027: */
028:
029: package com.caucho.quercus.lib.zip;
030:
031: import com.caucho.quercus.*;
032: import com.caucho.quercus.annotation.Optional;
033: import com.caucho.quercus.annotation.ReturnNullAsFalse;
034: import com.caucho.quercus.env.StringValue;
035: import com.caucho.quercus.env.Env;
036: import com.caucho.quercus.lib.file.BinaryInput;
037: import com.caucho.util.L10N;
038: import com.caucho.vfs.TempBuffer;
039:
040: import java.io.*;
041: import java.util.logging.Logger;
042: import java.util.zip.ZipEntry;
043:
044: public class QuercusZipEntry {
045: private static final Logger log = Logger
046: .getLogger(QuercusZipEntry.class.getName());
047: private static final L10N L = new L10N(QuercusZipEntry.class);
048:
049: private final ZipEntry _entry;
050: private final BinaryInput _binaryInput;
051: private final long _position;
052:
053: private ZipEntryInputStream _in;
054:
055: public QuercusZipEntry(ZipEntry zipEntry, BinaryInput binaryInput,
056: long position) {
057: _entry = zipEntry;
058: _binaryInput = binaryInput;
059: _position = position;
060: }
061:
062: /**
063: * Returns the file name.
064: */
065: public String zip_entry_name() {
066: return _entry.getName();
067: }
068:
069: /**
070: * Returns the file's uncompressed size.
071: */
072: public long zip_entry_filesize() {
073: return _entry.getSize();
074: }
075:
076: /**
077: * Opens this zip entry for reading.
078: */
079: public boolean zip_entry_open(Env env, ZipDirectory directory) {
080: try {
081: // php/1u07.qa
082: if (_in != null)
083: return true;
084:
085: _in = new ZipEntryInputStream(_binaryInput.openCopy(),
086: _position);
087:
088: return true;
089:
090: } catch (IOException e) {
091: env.warning(L.l(e.toString()));
092:
093: return false;
094: }
095: }
096:
097: /**
098: * Closes the zip entry.
099: */
100: public boolean zip_entry_close() throws IOException {
101: if (_in == null)
102: return false;
103:
104: ZipEntryInputStream in = _in;
105: _in = null;
106:
107: in.close();
108:
109: return true;
110: }
111:
112: /**
113: * Reads and decompresses entry's compressed data.
114: *
115: * @return decompressed BinaryValue or FALSE on error
116: */
117: @ReturnNullAsFalse
118: public StringValue zip_entry_read(Env env, @Optional("1024")
119: int length) {
120: if (_in == null)
121: return null;
122:
123: StringValue bb = env.createBinaryBuilder();
124:
125: bb.appendReadAll((InputStream) _in, length);
126:
127: return bb;
128: /*
129: if (bb.length() > 0)
130: return bb;
131: else
132: return null;
133: */
134: }
135:
136: /**
137: * Returns the size of the compressed data.
138: *
139: * @return -1, or compressed size
140: */
141: public long zip_entry_compressedsize() {
142: if (_entry == null)
143: return -1;
144:
145: return _entry.getCompressedSize();
146: }
147:
148: /**
149: * Only "deflate" and "store" methods are supported.
150: *
151: * @return the compression method used for this entry
152: */
153: public String zip_entry_compressionmethod() {
154: if (_entry == null)
155: return "";
156:
157: Integer method = _entry.getMethod();
158:
159: switch (method) {
160: case java.util.zip.ZipEntry.DEFLATED:
161: return "deflated";
162: case java.util.zip.ZipEntry.STORED:
163: return "stored";
164: default:
165: return method.toString();
166: }
167: }
168:
169: public String toString() {
170: return "QuercusZipEntry[" + _entry.getName() + "]";
171: }
172: }
|