1 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 ; 41 import java.io.InputStream ; 42 import java.util.logging.Level ; 43 import java.util.logging.Logger ; 44 import java.util.zip.ZipEntry ; 45 46 public class QuercusZipEntry { 47 private static final Logger log = 48 Logger.getLogger(QuercusZipEntry.class.getName()); 49 private static final L10N L = new L10N(QuercusZipEntry.class); 50 51 private InputStream _in; 52 private long _position; 53 private ZipEntry _entry; 54 55 public QuercusZipEntry(long position, ZipEntry zipEntry) 56 { 57 _position = position; 58 _entry = zipEntry; 59 } 60 61 64 public String zip_entry_name() 65 { 66 return _entry.getName(); 67 } 68 69 72 public long zip_entry_filesize() 73 { 74 return _entry.getSize(); 75 } 76 77 80 public boolean zip_entry_open(Env env, ZipDirectory directory) 81 { 82 try { 83 if (_in != null) 85 return true; 86 87 _in = directory.openInputStream(this); 88 return true; 89 90 } catch (IOException e) { 91 env.warning(L.l(e.toString())); 92 log.log(Level.FINE, e.toString(), e); 93 return false; 94 } 95 } 96 97 100 public boolean zip_entry_close() 101 throws IOException  102 { 103 if (_in == null) 104 return false; 105 106 _in.close(); 107 return true; 108 } 109 110 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 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 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 168 public String zip_entry_compressionmethod() 169 { 170 if (_entry == null) 171 return ""; 172 173 Integer 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 188 public long getPosition() 189 { 190 return _position; 191 } 192 193 public ZipEntry getZipEntry() 194 { 195 return _entry; 196 } 197 198 public String toString() 199 { 200 return "QuercusZipEntry[]"; 201 } 202 } 203 | Popular Tags |