KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > lib > zip > ZipModule


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Charles Reich
27  */

28
29 package com.caucho.quercus.lib.zip;
30
31 import com.caucho.quercus.QuercusModuleException;
32 import com.caucho.quercus.annotation.NotNull;
33 import com.caucho.quercus.annotation.Optional;
34 import com.caucho.quercus.annotation.ReturnNullAsFalse;
35 import com.caucho.quercus.env.BooleanValue;
36 import com.caucho.quercus.env.Env;
37 import com.caucho.quercus.env.LongValue;
38 import com.caucho.quercus.env.StringValueImpl;
39 import com.caucho.quercus.env.Value;
40 import com.caucho.quercus.lib.file.BinaryInput;
41 import com.caucho.quercus.lib.file.BinaryStream;
42 import com.caucho.quercus.lib.file.FileModule;
43 import com.caucho.quercus.module.AbstractQuercusModule;
44 import com.caucho.util.L10N;
45
46 import java.io.IOException JavaDoc;
47 import java.util.logging.Level JavaDoc;
48 import java.util.logging.Logger JavaDoc;
49
50 /**
51  * PHP Zip
52  */

53 public class ZipModule extends AbstractQuercusModule {
54   private static final Logger JavaDoc log =
55                                 Logger.getLogger(ZipModule.class.getName());
56   private static final L10N L = new L10N(ZipModule.class);
57
58   public String JavaDoc []getLoadedExtensions()
59   {
60     return new String JavaDoc[] { "zip" };
61   }
62
63   /**
64    * Opens stream to read zip entries.
65    * Since we're only reading, fopen mode is always "rb".
66    */

67   @ReturnNullAsFalse
68   public ZipDirectory zip_open(Env env,
69                                String JavaDoc filename)
70   {
71     if (filename == null || filename == "")
72       return null;
73
74     BinaryStream s = FileModule.fopen(env, filename, "rb", false, null);
75
76     if (s == null)
77       return null;
78
79     return new ZipDirectory((BinaryInput)s);
80   }
81
82   /**
83    * Reads an entry's metadata from the zip stream.
84    * It appears PHP's zip_read also does a zip_entry_open.
85    */

86   @ReturnNullAsFalse
87   public QuercusZipEntry zip_read(Env env,
88                                   @NotNull ZipDirectory directory)
89   {
90     try {
91       if (directory == null)
92         return null;
93
94       QuercusZipEntry qze = directory.zip_read();
95       zip_entry_open(env, directory, qze, "rb");
96
97       return qze;
98
99     } catch (IOException JavaDoc e) {
100       throw new QuercusModuleException(e);
101     }
102   }
103
104   /**
105    * Returns the file name.
106    *
107    * @param env
108    * @param zipEntry
109    * @return false if zipEntry is null
110    */

111   public Value zip_entry_name(Env env,
112                               @NotNull QuercusZipEntry entry)
113   {
114     if (entry == null)
115       return BooleanValue.FALSE;
116
117     return new StringValueImpl(entry.zip_entry_name());
118   }
119
120   /**
121    * Returns the file's uncompressed size.
122    *
123    * @param zipEntry
124    * @return false if zipEntry is null
125    */

126   public Value zip_entry_filesize(@NotNull QuercusZipEntry entry)
127   {
128     if (entry == null)
129       return BooleanValue.FALSE;
130
131     return new LongValue(entry.zip_entry_filesize());
132   }
133
134   /**
135    * Closes the file.
136    */

137   public boolean zip_close(@NotNull ZipDirectory directory)
138   {
139     if (directory == null)
140       return false;
141     return directory.zip_close();
142   }
143
144   /**
145    * Opens entry for decompression.
146    *
147    * @param file
148    * @param entry
149    * @param mode ignored - always "rb" from fopen()
150    * @return true on success or false on failure
151    */

152   public boolean zip_entry_open(Env env,
153                                 @NotNull ZipDirectory directory,
154                                 @NotNull QuercusZipEntry entry,
155                                 @Optional String JavaDoc mode)
156   {
157     if ((directory == null) || (entry == null))
158       return false;
159
160     return entry.zip_entry_open(env, directory);
161   }
162
163   /**
164    * Closes this entry's stream.
165    *
166    * @param entry
167    * @return true if successful, else false;
168    */

169   public boolean zip_entry_close(Env env,
170                                  @NotNull QuercusZipEntry entry)
171   {
172     try {
173       if (entry == null)
174         return false;
175       return entry.zip_entry_close();
176
177     } catch (IOException JavaDoc e) {
178         env.warning(L.l(e.toString()));
179         log.log(Level.FINE, e.toString(), e);
180         return false;
181     }
182   }
183
184   /**
185    * Reads and decompresses entry's compressed data.
186    *
187    * @param entry
188    * @param length
189    * @return false or decompressed BinaryValue
190    */

191   public Value zip_entry_read(Env env,
192                               @NotNull QuercusZipEntry entry,
193                               @Optional("1024") int length)
194   {
195     if (entry == null)
196       return BooleanValue.FALSE;
197
198     return entry.zip_entry_read(env, length);
199   }
200
201   /**
202    * Returns the compression method used for this entry.
203    * Only "deflate" and "store" are supported.
204    *
205    * @param entry
206    * @return empty string, stored or deflated
207    */

208   public String JavaDoc zip_entry_compressionmethod(@NotNull QuercusZipEntry entry)
209   {
210     if (entry == null)
211       return "";
212
213     return entry.zip_entry_compressionmethod();
214   }
215
216   /**
217    * Returns the size of the compressed data.
218    *
219    * @param entry
220    * @return -1, or compressed size
221    */

222   public Value zip_entry_compressedsize(@NotNull QuercusZipEntry entry)
223   {
224     if (entry == null)
225       return new LongValue(-1);
226
227     return entry.zip_entry_compressedsize();
228   }
229 }
230
Popular Tags