1 20 21 package org.jahia.utils.jar; 22 23 24 import java.io.BufferedInputStream ; 25 import java.io.BufferedOutputStream ; 26 import java.io.File ; 27 import java.io.FileInputStream ; 28 import java.io.FileOutputStream ; 29 import java.io.IOException ; 30 import java.io.InputStream ; 31 import java.io.OutputStream ; 32 import java.util.jar.JarFile ; 33 import java.util.zip.ZipEntry ; 34 import java.util.zip.ZipFile ; 35 import java.util.zip.ZipInputStream ; 36 37 import org.jahia.exceptions.JahiaException; 38 import org.jahia.utils.JahiaConsole; 39 40 45 public class JahiaJarHandler { 46 47 48 private String m_FilePath; 49 50 private JarFile m_JarFile; 51 52 58 public JahiaJarHandler ( String path ) throws IOException { 59 60 m_FilePath = path; 61 File f = new File (path); 62 try { 63 64 m_JarFile = new JarFile (f); 65 66 } catch ( IOException ioe ) { 67 JahiaConsole.println("JahiaJarHandler","JahiaJarHandler IOException occurred " + f.getName() ); 68 throw new IOException ("JahiaJarHandler, IOException occurred "); 69 } catch ( java.lang.NullPointerException e ) { 70 JahiaConsole.println("JahiaJarHandler","JahiaJarHandler NullPointerException " + f.getName() ); 71 throw new IOException ("JahiaJarHandler, NullPointerException occurred "); 72 } 73 74 if ( m_JarFile == null ) { 75 76 throw new IOException ("JahiaJarHandler, referred file is null"); 77 } 78 79 } 80 81 82 86 public void unzip() throws JahiaException { 87 88 try { 89 90 File f = new File (m_FilePath); 91 92 JahiaConsole.println("JahiaJarHandler"," Start Decompressing " + f.getName() ); 93 94 String parentPath = f.getParent() + File.separator; 95 String path = null; 96 File fileItem = null; 97 98 FileInputStream fis = new FileInputStream (m_FilePath); 99 BufferedInputStream bis = new BufferedInputStream (fis); 100 ZipInputStream zis = new ZipInputStream (bis); 101 ZipFile zf = new ZipFile (m_FilePath); 102 ZipEntry ze = null; 103 String zeName = null; 104 105 try{ 106 107 while ( (ze = zis.getNextEntry()) != null ){ 108 zeName = ze.getName(); 109 path = parentPath + genPathFile(zeName); 110 if ( ze.isDirectory() ){ 111 File fo = new File (path); 112 fo.mkdirs(); 113 } else { 114 File fo = new File (path); 115 copyStream(zis,new FileOutputStream (fo)); 116 } 117 } 118 } finally { 119 120 zf.close(); 122 fis.close(); 123 zis.close(); 124 bis.close(); 125 } 126 127 128 JahiaConsole.println("JahiaJarHandler"," Decompressing " + f.getName() + " done ! "); 129 130 } catch ( IOException ioe ) { 131 132 JahiaConsole.println("JahiaJarHandler"," fail unzipping " + ioe.getMessage() ); 133 134 throw new JahiaException ("JahiaJarHandler", "faile processing unzip", 135 JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY); 136 137 } 138 139 } 140 141 142 147 public void unzip(String path) throws JahiaException { 148 149 try { 150 151 File f = new File (m_FilePath); 152 153 JahiaConsole.println("JahiaJarHandler"," Start Decompressing " + f.getName() ); 154 155 String destPath = null; 156 File fileItem = null; 157 158 FileInputStream fis = new FileInputStream (m_FilePath); 159 BufferedInputStream bis = new BufferedInputStream (fis); 160 ZipInputStream zis = new ZipInputStream (bis); 161 ZipFile zf = new ZipFile (m_FilePath); 162 ZipEntry ze = null; 163 String zeName = null; 164 165 try { 166 167 while ( (ze = zis.getNextEntry()) != null ){ 168 zeName = ze.getName(); 169 destPath = path + File.separator + genPathFile(zeName); 170 if ( ze.isDirectory() ){ 171 File fo = new File (destPath); 172 fo.mkdirs(); 173 fo = null; 174 } else { 175 File fo = new File (destPath); 176 FileOutputStream fos = new FileOutputStream (fo); 177 copyStream(zis,fos); 178 fos.close(); 179 fo = null; 180 } 181 } 182 } finally { 183 184 zf.close(); 186 fis.close(); 187 zis.close(); 188 bis.close(); 189 } 190 191 JahiaConsole.println("JahiaJarHandler"," Decompressing " + f.getName() + " done ! "); 192 193 } catch ( IOException ioe ) { 194 195 JahiaConsole.println("JahiaJarHandler"," fail unzipping " + ioe.getMessage() ); 196 197 throw new JahiaException ("JahiaJarHandler", "faile processing unzip", 198 JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY); 199 200 } 201 202 } 203 204 205 212 public File extractFile( String entryName ) throws IOException { 213 214 File tmpFile = null; 215 216 217 ZipEntry entry = m_JarFile.getEntry(entryName); 219 220 if ( (entry != null) && !entry.isDirectory() ) { 221 222 InputStream ins = m_JarFile.getInputStream(entry); 223 224 if ( ins != null ){ 225 File f = new File (""); 226 tmpFile = File.createTempFile("tmpfile",""); 227 if ( tmpFile == null || !tmpFile.canWrite() ){ 228 229 throw new IOException ("extractFile error creating temporary file"); 230 231 } 232 233 copyStream(ins,new FileOutputStream (tmpFile)); 234 235 } 236 } else { 237 JahiaConsole.println("JahiaJarHandler", "extractFile(entry), " + entryName + " is null or a directory " ); 238 throw new IOException ("extractFileEntry cannot find an entry file of name " + entryName); 239 } 240 241 return tmpFile; 242 243 } 244 245 246 253 public void extractEntry( String entryName, 254 String destPath ) throws JahiaException { 255 256 try { 257 258 ZipEntry entry = m_JarFile.getEntry(entryName); 259 260 if ( entry == null ){ 261 StringBuffer strBuf = new StringBuffer (1024); 262 strBuf.append(" extractEntry(), cannot find entry "); 263 strBuf.append(entryName); 264 strBuf.append(" in the jar file "); 265 JahiaConsole.println("JahiaJarHandler", strBuf.toString()); 266 267 throw new JahiaException ("JahiaJarHandler", strBuf.toString(), 268 JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY); 269 270 } 271 272 File destDir = new File (destPath); 273 if ( destDir == null || !destDir.isDirectory() || !destDir.canWrite() ){ 274 275 JahiaConsole.println("JahiaJarHandler"," extractEntry(), cannot access to the destination dir "); 276 277 throw new JahiaException ("JahiaJarHandler", " extractEntry(), cannot access to the destination dir ", 278 JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY); 279 } 280 281 282 File f = new File (m_FilePath); 283 284 JahiaConsole.println("JahiaJarHandler"," Start extractEntry(entryName,path) Decompressing entry " + entryName ); 285 286 String path = null; 287 File fileItem = null; 288 289 FileInputStream fis = new FileInputStream (m_FilePath); 290 BufferedInputStream bis = new BufferedInputStream (fis); 291 ZipInputStream zis = new ZipInputStream (bis); 292 ZipFile zf = new ZipFile (m_FilePath); 293 ZipEntry ze = null; 294 String zeName = null; 295 296 while ( (ze = zis.getNextEntry()) != null && !ze.getName().equals(entryName) ) { 297 JahiaConsole.println("JahiaJarHandler","extractEntry(extryName, path), bypass " + ze.getName() ); 299 } 300 301 try{ 302 303 while ( ze != null ){ 304 zeName = ze.getName(); 305 path = destPath + File.separator + genPathFile(zeName); 306 if ( ze.isDirectory() ){ 307 File fo = new File (path); 308 fo.mkdirs(); 309 } else { 310 File fo = new File (path); 311 copyStream(zis,new FileOutputStream (fo)); 312 } 313 314 ze = zis.getNextEntry(); 315 } 316 317 } finally { 318 319 zf.close(); 321 fis.close(); 322 zis.close(); 323 bis.close(); 324 } 325 326 327 JahiaConsole.println("JahiaJarHandler"," Decompressing " + f.getName() + " done ! "); 328 329 } catch ( IOException ioe ) { 330 331 JahiaConsole.println("JahiaJarHandler"," fail unzipping " + ioe.getMessage() ); 332 333 throw new JahiaException ("JahiaJarHandler", "faile processing unzip", 334 JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY); 335 336 } 337 338 } 339 340 341 347 public ZipEntry getEntry(String entryName){ 348 349 return m_JarFile.getEntry(entryName); 350 351 } 352 353 354 360 public boolean isDirectory(String entryName){ 361 return (( m_JarFile.getEntry(entryName) != null) && m_JarFile.getEntry(entryName).isDirectory()); 362 } 363 364 370 public boolean entryExists(String entryName){ 371 372 if ( m_JarFile.getEntry(entryName) != null ){ 373 return true; 374 } 375 return false; 376 377 } 378 379 380 385 public void closeJarFile(){ 386 387 try { 388 m_JarFile.close(); 389 } catch ( IOException e ) { 390 JahiaConsole.println("JahiaJarHandler","cannot close jar file"); 391 } 393 394 } 395 396 397 402 protected String genPathFile(String entryName){ 403 404 StringBuffer sb = new StringBuffer (entryName.length()); 405 for ( int i= 0; i< entryName.length() ; i++ ){ 406 if ( entryName.charAt(i) == '/' ){ 407 sb.append(File.separator); 408 } else { 409 sb.append(entryName.charAt(i)); 410 } 411 } 412 413 return ( sb.toString() ); 414 415 } 416 417 418 425 protected void copyStream( InputStream ins, 426 OutputStream outs) 427 throws IOException 428 { 429 int bufferSize = 1024; 430 byte[] writeBuffer = new byte[bufferSize]; 431 432 BufferedOutputStream bos = 433 new BufferedOutputStream (outs, bufferSize); 434 int bufferRead; 435 while((bufferRead = ins.read(writeBuffer)) != -1) 436 bos.write(writeBuffer,0,bufferRead); 437 bos.flush(); 438 bos.close(); 439 } 440 441 442 443 444 445 } | Popular Tags |