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.QuercusModuleException;
032: import com.caucho.quercus.annotation.NotNull;
033: import com.caucho.quercus.annotation.Optional;
034: import com.caucho.quercus.annotation.ReturnNullAsFalse;
035: import com.caucho.quercus.env.BooleanValue;
036: import com.caucho.quercus.env.Env;
037: import com.caucho.quercus.env.LongValue;
038: import com.caucho.quercus.env.StringValue;
039: import com.caucho.quercus.env.UnicodeValueImpl;
040: import com.caucho.quercus.env.Value;
041: import com.caucho.quercus.lib.file.BinaryInput;
042: import com.caucho.quercus.lib.file.BinaryStream;
043: import com.caucho.quercus.lib.file.FileModule;
044: import com.caucho.quercus.module.AbstractQuercusModule;
045: import com.caucho.util.L10N;
046:
047: import java.io.IOException;
048: import java.util.logging.Level;
049: import java.util.logging.Logger;
050:
051: /**
052: * PHP Zip
053: */
054: public class ZipModule extends AbstractQuercusModule {
055: private static final Logger log = Logger.getLogger(ZipModule.class
056: .getName());
057: private static final L10N L = new L10N(ZipModule.class);
058:
059: public String[] getLoadedExtensions() {
060: return new String[] { "zip" };
061: }
062:
063: /**
064: * Opens stream to read zip entries.
065: * Since we're only reading, fopen mode is always "rb".
066: */
067: @ReturnNullAsFalse
068: public ZipDirectory zip_open(Env env, @NotNull
069: StringValue filename) {
070: if (filename == null || filename.length() == 0)
071: return null;
072:
073: BinaryStream s = FileModule.fopen(env, filename, "rb", false,
074: null);
075:
076: if (s == null)
077: return null;
078:
079: return new ZipDirectory((BinaryInput) s);
080: }
081:
082: /**
083: * Reads an entry's metadata from the zip stream.
084: * It appears PHP's zip_read also does a zip_entry_open.
085: */
086: @ReturnNullAsFalse
087: public QuercusZipEntry zip_read(Env env, @NotNull
088: ZipDirectory directory) {
089: if (directory == null)
090: return null;
091:
092: try {
093: QuercusZipEntry qze = directory.zip_read();
094: zip_entry_open(env, directory, qze, "rb");
095:
096: return qze;
097:
098: } catch (IOException e) {
099: throw new QuercusModuleException(e);
100: }
101: }
102:
103: /**
104: * Returns the file name.
105: *
106: * @return false if zipEntry is null
107: */
108: public Value zip_entry_name(Env env, @NotNull
109: QuercusZipEntry entry) {
110: if (entry == null)
111: return BooleanValue.FALSE;
112:
113: return env.createString(entry.zip_entry_name());
114: }
115:
116: /**
117: * Returns the file's uncompressed size.
118: *
119: * @return false if zipEntry is null
120: */
121: public Value zip_entry_filesize(@NotNull
122: QuercusZipEntry entry) {
123: if (entry == null)
124: return BooleanValue.FALSE;
125:
126: return new LongValue(entry.zip_entry_filesize());
127: }
128:
129: /**
130: * Closes the file.
131: */
132: public boolean zip_close(@NotNull
133: ZipDirectory directory) {
134: if (directory == null)
135: return false;
136:
137: return directory.zip_close();
138: }
139:
140: /**
141: * Opens entry for decompression.
142: *
143: * @return true on success or false on failure
144: */
145: public boolean zip_entry_open(Env env, @NotNull
146: ZipDirectory directory, @NotNull
147: QuercusZipEntry entry, @Optional
148: String mode) {
149: if ((directory == null) || (entry == null))
150: return false;
151:
152: return entry.zip_entry_open(env, directory);
153: }
154:
155: /**
156: * Closes this entry's stream.
157: *
158: * @return true if successful, else false;
159: */
160: public boolean zip_entry_close(Env env, @NotNull
161: QuercusZipEntry entry) {
162: try {
163: if (entry == null)
164: return false;
165:
166: return entry.zip_entry_close();
167:
168: } catch (IOException e) {
169: env.warning(L.l(e.toString()));
170: log.log(Level.FINE, e.toString(), e);
171: return false;
172: }
173: }
174:
175: /**
176: * Reads and decompresses entry's compressed data.
177: *
178: * @return false or decompressed BinaryValue
179: */
180: @ReturnNullAsFalse
181: public StringValue zip_entry_read(Env env, @NotNull
182: QuercusZipEntry entry, @Optional("1024")
183: int length) {
184: if (entry == null)
185: return null;
186:
187: return entry.zip_entry_read(env, length);
188: }
189:
190: /**
191: * Returns the compression method used for this entry.
192: * Only "deflate" and "store" are supported.
193: *
194: * @return empty string, stored or deflated
195: */
196: public String zip_entry_compressionmethod(@NotNull
197: QuercusZipEntry entry) {
198: if (entry == null)
199: return "";
200:
201: return entry.zip_entry_compressionmethod();
202: }
203:
204: /**
205: * Returns the size of the compressed data.
206: *
207: * @return -1, or compressed size
208: */
209: public long zip_entry_compressedsize(@NotNull
210: QuercusZipEntry entry) {
211: if (entry == null)
212: return -1;
213:
214: return entry.zip_entry_compressedsize();
215: }
216: }
|