1 20 21 package org.jahia.utils.zip; 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.JahiaArchiveFileException; 38 import org.jahia.exceptions.JahiaException; 39 import org.jahia.utils.JahiaConsole; 40 41 46 public class JahiaArchiveFileHandler { 47 48 private static final String CLASS_NAME = JahiaArchiveFileHandler.class.getName(); 49 50 51 private String m_FilePath; 52 53 private JarFile m_JarFile; 54 55 61 public JahiaArchiveFileHandler ( String path ) throws IOException { 62 63 m_FilePath = path; 64 File f = new File (path); 65 try { 66 67 m_JarFile = new JarFile (f); 68 69 } catch ( IOException ioe ) { 70 JahiaConsole.println(CLASS_NAME+"::Constructor","IOException occurred " + f.getAbsolutePath() ); 71 throw new IOException (CLASS_NAME +" IOException occurred "); 72 } catch ( java.lang.NullPointerException e ) { 73 JahiaConsole.println(CLASS_NAME+"::Constructor","NullPointerException " + f.getAbsolutePath() ); 74 throw new IOException (CLASS_NAME + " NullPointerException occurred "); 75 } 76 77 if ( m_JarFile == null ) { 78 79 throw new IOException (CLASS_NAME+" source file is null"); 80 } 81 82 } 83 84 85 89 public void unzip() throws JahiaException { 90 91 try { 92 93 File f = new File (m_FilePath); 94 95 97 String parentPath = f.getParent() + File.separator; 98 String path = null; 99 100 FileInputStream fis = new FileInputStream (m_FilePath); 101 BufferedInputStream bis = new BufferedInputStream (fis); 102 ZipInputStream zis = new ZipInputStream (bis); 103 ZipFile zf = new ZipFile (m_FilePath); 104 ZipEntry ze = null; 105 String zeName = null; 106 107 try{ 108 109 while ( (ze = zis.getNextEntry()) != null ){ 110 zeName = ze.getName(); 111 path = parentPath + genPathFile(zeName); 112 File fo = new File (path); 113 114 if ( ze.isDirectory() ){ 115 fo.mkdirs(); 116 } else { 117 copyStream(zis,new FileOutputStream (fo)); 118 } 119 zis.closeEntry(); 120 } 121 } finally { 122 123 zf.close(); 125 fis.close(); 126 zis.close(); 127 bis.close(); 128 } 129 130 131 133 } catch ( IOException ioe ) { 134 135 JahiaConsole.println("JahiaArchiveFileHandler"," fail unzipping " + ioe.getMessage() ); 136 137 throw new JahiaException ("JahiaArchiveFileHandler", "faile processing unzip", 138 JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY); 139 140 } 141 142 } 143 144 145 150 public void unzip(String path) throws JahiaException { 151 152 try { 153 154 File f = new File (m_FilePath); 155 156 158 String destPath = null; 159 160 FileInputStream fis = new FileInputStream (m_FilePath); 161 BufferedInputStream bis = new BufferedInputStream (fis); 162 ZipInputStream zis = new ZipInputStream (bis); 163 ZipFile zf = new ZipFile (m_FilePath); 164 ZipEntry ze = null; 165 String zeName = null; 166 167 try { 168 169 while ( (ze = zis.getNextEntry()) != null ){ 170 171 zeName = ze.getName(); 172 173 destPath = path + File.separator + genPathFile(zeName); 174 175 File fo = new File (destPath); 176 177 if ( ze.isDirectory() ){ 178 179 fo.mkdirs(); 180 } else { 181 File parent = new File (fo.getParent()); 182 parent.mkdirs(); 183 FileOutputStream fos = new FileOutputStream (fo); 184 copyStream(zis,fos); 185 } 186 zis.closeEntry(); 187 } 188 } finally { 189 190 zf.close(); 192 fis.close(); 193 zis.close(); 194 bis.close(); 195 } 196 197 199 } catch ( IOException ioe ) { 200 201 JahiaConsole.println(CLASS_NAME+".unzip(path)"," fail unzipping " + ioe.getMessage() ); 202 203 throw new JahiaException (CLASS_NAME, "faile processing unzip", 204 JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY, ioe); 205 206 } 207 208 } 209 210 211 218 public File extractFile( String entryName ) throws IOException , JahiaArchiveFileException { 219 220 File tmpFile = null; 221 222 223 ZipEntry entry = m_JarFile.getEntry(entryName); 225 226 if ( (entry != null) && !entry.isDirectory() ) { 227 228 InputStream ins = m_JarFile.getInputStream(entry); 229 230 if ( ins != null ){ 231 232 240 tmpFile = File.createTempFile("jahia_temp","",null); 241 242 if ( tmpFile == null || !tmpFile.canWrite() ){ 243 throw new IOException ("extractFile error creating temporary file"); 244 } 245 FileOutputStream outs = new FileOutputStream (tmpFile); 246 copyStream(ins,outs); 247 outs.flush(); 248 outs.close(); 249 } 250 } else { 251 JahiaConsole.println(CLASS_NAME+".extractFile(entry)", " entryName" + " is null or a directory " ); 252 throw new JahiaArchiveFileException (JahiaException.ENTRY_NOT_FOUND); 253 } 254 255 return tmpFile; 256 257 } 258 259 260 267 public void extractEntry( String entryName, 268 String destPath ) throws JahiaException { 269 270 try { 271 272 ZipEntry entry = m_JarFile.getEntry(entryName); 273 274 if ( entry == null ){ 275 StringBuffer strBuf = new StringBuffer (1024); 276 strBuf.append(" extractEntry(), cannot find entry "); 277 strBuf.append(entryName); 278 strBuf.append(" in the jar file "); 279 281 throw new JahiaException (CLASS_NAME, strBuf.toString(), 282 JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY); 283 284 } 285 286 287 File destDir = new File (destPath); 288 if ( destDir == null || !destDir.isDirectory() || !destDir.canWrite() ){ 289 290 JahiaConsole.println(CLASS_NAME+".extractEntry()"," cannot access to the destination dir "); 291 292 throw new JahiaException (CLASS_NAME," cannot access to the destination dir ", 293 JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY); 294 } 295 296 297 File f = new File (m_FilePath); 298 299 301 String path = null; 302 303 FileInputStream fis = new FileInputStream (m_FilePath); 304 BufferedInputStream bis = new BufferedInputStream (fis); 305 ZipInputStream zis = new ZipInputStream (bis); 306 ZipFile zf = new ZipFile (m_FilePath); 307 ZipEntry ze = null; 308 String zeName = null; 309 boolean isFile = false; 310 311 313 while ( (ze = zis.getNextEntry()) != null && !ze.getName().equalsIgnoreCase(entryName) ) { 314 zis.closeEntry(); 317 } 318 319 321 try{ 322 323 if ( ze.isDirectory() ){ 324 325 327 while ( ze != null ){ 328 zeName = ze.getName(); 329 path = destPath + File.separator + genPathFile(zeName); 330 File fo = new File (path); 331 if ( ze.isDirectory() ){ 332 fo.mkdirs(); 333 } else { 334 335 FileOutputStream outs = new FileOutputStream (fo); 336 copyStream(zis,outs); 337 } 340 zis.closeEntry(); 341 ze = zis.getNextEntry(); 342 343 } 344 } else { 345 346 348 zeName = ze.getName(); 349 path = destPath + File.separator + genPathFile(zeName); 350 351 353 File fo = new File (path); 354 FileOutputStream outs = new FileOutputStream (fo); 355 copyStream(zis,outs); 356 } 359 360 362 } finally { 363 364 zf.close(); 366 fis.close(); 367 zis.close(); 368 bis.close(); 369 } 370 371 372 373 } catch ( IOException ioe ) { 374 375 JahiaConsole.println(CLASS_NAME," fail unzipping " + ioe.getMessage() ); 376 377 throw new JahiaException (CLASS_NAME, "faile processing unzip", 378 JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY); 379 380 } 381 382 } 383 384 385 391 public ZipEntry getEntry(String entryName){ 392 393 return m_JarFile.getEntry(entryName); 394 395 } 396 397 398 404 public boolean isDirectory(String entryName){ 405 return ((m_JarFile.getEntry(entryName) != null) && m_JarFile.getEntry(entryName).isDirectory()); 406 } 407 408 414 public boolean entryExists(String entryName){ 415 return ( m_JarFile.getEntry(entryName) != null ); 416 } 417 418 419 424 public void closeArchiveFile(){ 425 426 try { 427 m_JarFile.close(); 428 } catch ( IOException e ) { 429 JahiaConsole.println(CLASS_NAME,"cannot close jar file"); 430 } 432 433 } 434 435 436 441 protected String genPathFile(String entryName){ 442 443 StringBuffer sb = new StringBuffer (entryName.length()); 444 for ( int i= 0; i< entryName.length() ; i++ ){ 445 if ( entryName.charAt(i) == '/' ){ 446 sb.append(File.separator); 447 } else { 448 sb.append(entryName.charAt(i)); 449 } 450 } 451 452 return ( sb.toString() ); 453 454 } 455 456 457 464 protected void copyStream( InputStream ins, 465 OutputStream outs) 466 throws IOException 467 { 468 int bufferSize = 1024; 469 byte[] writeBuffer = new byte[bufferSize]; 470 471 BufferedOutputStream bos = 472 new BufferedOutputStream (outs, bufferSize); 473 int bufferRead; 474 while((bufferRead = ins.read(writeBuffer)) != -1) 475 bos.write(writeBuffer,0,bufferRead); 476 bos.flush(); 477 bos.close(); 478 outs.flush(); 479 outs.close(); 480 } 481 482 483 484 485 486 } | Popular Tags |