KickJava   Java API By Example, From Geeks To Geeks.

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


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.annotation.Optional;
32 import com.caucho.quercus.env.BinaryBuilderValue;
33 import com.caucho.quercus.env.BooleanValue;
34 import com.caucho.quercus.env.Env;
35 import com.caucho.quercus.env.LongValue;
36 import com.caucho.quercus.env.Value;
37 import com.caucho.util.L10N;
38 import com.caucho.vfs.TempBuffer;
39
40 import java.io.IOException JavaDoc;
41 import java.io.InputStream JavaDoc;
42 import java.util.logging.Level JavaDoc;
43 import java.util.logging.Logger JavaDoc;
44 import java.util.zip.ZipEntry JavaDoc;
45
46 public class QuercusZipEntry {
47   private static final Logger JavaDoc log =
48                              Logger.getLogger(QuercusZipEntry.class.getName());
49   private static final L10N L = new L10N(QuercusZipEntry.class);
50
51   private InputStream JavaDoc _in;
52   private long _position;
53   private ZipEntry JavaDoc _entry;
54
55   public QuercusZipEntry(long position, ZipEntry JavaDoc zipEntry)
56   {
57     _position = position;
58     _entry = zipEntry;
59   }
60
61   /**
62    * Returns the file name.
63    */

64   public String JavaDoc zip_entry_name()
65   {
66     return _entry.getName();
67   }
68
69   /**
70    * Returns the file's uncompressed size.
71    */

72   public long zip_entry_filesize()
73   {
74     return _entry.getSize();
75   }
76
77   /**
78    * Opens this zip entry for reading.
79    */

80   public boolean zip_entry_open(Env env, ZipDirectory directory)
81   {
82     try {
83       // php/1u07.qa
84
if (_in != null)
85         return true;
86
87       _in = directory.openInputStream(this);
88       return true;
89
90     } catch (IOException JavaDoc e) {
91       env.warning(L.l(e.toString()));
92       log.log(Level.FINE, e.toString(), e);
93       return false;
94     }
95   }
96
97   /**
98    * Closes the zip entry.
99    */

100   public boolean zip_entry_close()
101     throws IOException JavaDoc
102   {
103     if (_in == null)
104       return false;
105
106     _in.close();
107     return true;
108   }
109
110   /**
111    * Reads and decompresses entry's compressed data.
112    *
113    * @param entry
114    * @param length
115    * @return decompressed BinaryValue or FALSE on error
116    */

117   public Value zip_entry_read(Env env,
118                                 @Optional("1024") int length)
119   {
120     if (_in == null)
121       return BooleanValue.FALSE;
122
123     BinaryBuilderValue bbv = new BinaryBuilderValue();
124     TempBuffer tb = TempBuffer.allocate();
125     byte[] buffer = tb.getBuffer();
126
127     int sublen;
128     try {
129       while (length > 0) {
130         sublen = _in.read(buffer, 0, Math.min(length, buffer.length));
131         if (sublen <= 0)
132           break;
133         bbv.append(buffer, 0, sublen);
134         length -= sublen;
135       }
136       if (bbv.length() < 0)
137         return BooleanValue.FALSE;
138
139       return bbv;
140
141     } catch (IOException JavaDoc e) {
142       env.warning(L.l(e.toString()));
143       log.log(Level.FINE, e.toString(), e);
144       return BooleanValue.FALSE;
145     } finally {
146       TempBuffer.free(tb);
147     }
148   }
149
150   /**
151    * Returns the size of the compressed data.
152    *
153    * @return -1, or compressed size
154    */

155   public Value zip_entry_compressedsize()
156   {
157     if (_entry == null)
158       return new LongValue(-1);
159
160     return new LongValue(_entry.getCompressedSize());
161   }
162
163   /**
164    * Only "deflate" and "store" methods are supported.
165    *
166    * @return the compression method used for this entry
167    */

168   public String JavaDoc zip_entry_compressionmethod()
169   {
170     if (_entry == null)
171       return "";
172
173     Integer JavaDoc method = _entry.getMethod();
174
175     switch(method) {
176       case java.util.zip.ZipEntry.DEFLATED:
177         return "deflated";
178       case java.util.zip.ZipEntry.STORED:
179         return "stored";
180       default:
181         return method.toString();
182     }
183   }
184
185   /**
186    * Returns the position fo this entry in the stream.
187    */

188   public long getPosition()
189   {
190     return _position;
191   }
192
193   public ZipEntry JavaDoc getZipEntry()
194   {
195     return _entry;
196   }
197
198   public String JavaDoc toString()
199   {
200     return "QuercusZipEntry[]";
201   }
202 }
203
Popular Tags