1 14 package org.compiere.util; 15 16 import java.util.*; 17 import java.util.zip.*; 18 import java.util.jar.*; 19 import java.io.*; 20 import java.sql.*; 21 22 28 public class ZipUtil 29 { 30 33 public ZipUtil() 34 { 35 } 37 41 public ZipUtil(String fileName) 42 { 43 open (fileName); 44 } 46 50 public ZipUtil(File file) 51 { 52 open(file); 53 } 55 private File m_file; 56 private ZipFile m_zipFile; 57 58 63 public boolean open (String fileName) 64 { 65 if (fileName == null) 66 return false; 67 try 68 { 69 return open (new File(fileName)); 70 } 71 catch (Exception ex) 72 { 73 System.err.println("ZipUtil.open - " + ex); 74 } 75 return false; 76 } 78 83 public boolean open (File file) 84 { 85 if (file == null) 86 return false; 87 m_file = file; 88 try 89 { 90 if (file.getName().endsWith("jar")) 91 m_zipFile = new JarFile (file, false, JarFile.OPEN_READ); 92 else 93 m_zipFile = new ZipFile (file, ZipFile.OPEN_READ); 94 } 95 catch (IOException ex) 96 { 97 System.err.println("ZipUtil.open - " + ex); 98 m_zipFile = null; 99 return false; 100 } 101 return true; 102 } 104 107 public void close() 108 { 109 try 110 { 111 if (m_zipFile != null) 112 m_zipFile.close(); 113 } 114 catch (IOException ex) 115 { 116 System.err.println("ZipUtil.close - " + ex); 117 } 118 m_zipFile = null; 119 } 121 125 public boolean isOpen() 126 { 127 return m_zipFile != null; 128 } 130 134 public boolean isJar() 135 { 136 return (m_zipFile != null && m_zipFile instanceof JarFile); 137 } 139 143 public JarFile getJar() 144 { 145 if (m_zipFile != null && m_zipFile instanceof JarFile) 146 return (JarFile)m_zipFile; 147 return null; 148 } 150 154 public String toString() 155 { 156 if (m_zipFile != null) 157 return m_zipFile.toString(); 158 return "ZipUtil"; 159 } 161 162 163 167 public String [] getContent() 168 { 169 if (!isOpen()) 170 return null; 171 Enumeration e = m_zipFile.entries(); 172 ArrayList list = new ArrayList(); 173 while (e.hasMoreElements()) 174 list.add(e.nextElement()); 175 String [] retValue = new String [list.size()]; 177 for (int i = 0; i < retValue.length; i++) 178 retValue[i] = ((ZipEntry)list.get(i)).getName(); 179 Arrays.sort(retValue); 180 return retValue; 181 } 183 187 public Enumeration entries() 188 { 189 if (!isOpen()) 190 return null; 191 return m_zipFile.entries(); 192 } 194 199 public ZipEntry getEntry (String name) 200 { 201 if (!isOpen()) 202 return null; 203 return m_zipFile.getEntry(name); 204 } 206 211 public String getEntryInfo (String name) 212 { 213 StringBuffer sb = new StringBuffer (name); 214 ZipEntry e = getEntry(name); 215 if (e == null) 216 sb.append(": -"); 217 else 218 { 219 Timestamp ts = new Timestamp(e.getTime()); 220 sb.append(": ").append(ts).append(" - ").append(e.getSize()); 221 } 222 return sb.toString(); 223 } 225 229 public Manifest getManifest() 230 { 231 try 232 { 233 JarFile jar = getJar(); 234 if (jar != null) 235 return jar.getManifest(); 236 } 237 catch (IOException ex) 238 { 239 System.err.println("ZipUtil.getManifest - " + ex); 240 } 241 return null; 242 } 244 245 246 247 253 static public ZipEntry getEntry (String fileName, String entryName) 254 { 255 if (fileName == null || entryName == null) 256 return null; 257 File file = new File(fileName); 259 if (!file.exists()) 260 { 261 String fn = getFullName(fileName); 262 if (fn == null) 263 return null; file = new File(fn); 265 } 266 ZipUtil zu = new ZipUtil (file); 267 if (!zu.isOpen()) 268 return null; 269 ZipEntry retValue = zu.getEntry(entryName); 271 if (retValue == null) 272 { 273 Enumeration e = zu.entries(); 274 while (e.hasMoreElements()) 275 { 276 ZipEntry entry = (ZipEntry)e.nextElement(); 277 if (entry.getName().indexOf(entryName) != -1) 278 { 279 retValue = entry; 280 break; 281 } 282 } 283 } 284 zu.close(); 285 return retValue; 286 } 288 293 static public JarFile getJar (String fileName) 294 { 295 if (fileName == null) 296 return null; 297 File file = new File(fileName); 299 if (!file.exists()) 300 { 301 String fn = getFullName(fileName); 302 if (fn == null) 303 return null; file = new File(fn); 305 } 306 ZipUtil zu = new ZipUtil (file); 307 return zu.getJar(); 308 } 310 315 static public Manifest getManifest (String fileName) 316 { 317 if (fileName == null) 318 return null; 319 JarFile jar = getJar (fileName); 320 if (jar == null) 321 return null; 322 try 323 { 324 return jar.getManifest(); 325 } 326 catch (IOException ex) 327 { 328 System.err.println("ZipUtil.getManifest - " + ex); 329 } 330 return null; 331 } 333 339 static public JarEntry getJarEntry (String fileName, String jarEntry) 340 { 341 if (fileName == null) 342 return null; 343 JarFile jar = getJar (fileName); 344 if (jar == null) 345 return null; 346 return jar.getJarEntry(jarEntry); 347 } 349 354 static public void dumpManifest (String fileName) 355 { 356 Manifest mf = getManifest (fileName); 357 if (mf == null) 358 { 359 System.out.println("No Jar file: " + fileName); 360 return; 361 } 362 System.out.println(mf.getEntries()); 364 } 366 372 static public String getEntryTime (String fileName, String entryName) 373 { 374 ZipEntry entry = getEntry(fileName, entryName); 375 if (entry == null) 376 return null; 377 Timestamp ts = new Timestamp (entry.getTime()); 378 return ts.toString(); 379 } 381 386 static public String getFullName (String jarFile) 387 { 388 String path = System.getProperty("java.class.path"); 389 String [] pathEntries = path.split(System.getProperty("path.separator")); 390 for (int i = 0; i < pathEntries.length; i++) 391 { 392 if (pathEntries[i].indexOf(jarFile) != -1) 394 return pathEntries[i]; 395 } 396 path = System.getProperty("sun.boot.class.path"); 397 pathEntries = path.split(System.getProperty("path.separator")); 398 for (int i = 0; i < pathEntries.length; i++) 399 { 400 if (pathEntries[i].indexOf(jarFile) != -1) 402 return pathEntries[i]; 403 } 404 return null; 405 } 407 } | Popular Tags |