📄 jahiaarchivefilehandler.java
字号:
//// ____.// __/\ ______| |__/\. _______// __ .____| | \ | +----+ \// _______| /--| | | - \ _ | : - \_________// \\______: :---| : : | : | \________>// |__\---\_____________:______: :____|____:_____\// /_____|//// . . . i n j a h i a w e t r u s t . . .//////// JahiaArchiveFileHandler//// NK 15.01.2001////package org.jahia.utils.zip;import java.io.*;import java.util.*;import java.util.jar.*;import java.util.zip.*;import org.jahia.exceptions.*;import org.jahia.utils.*;/** * A Wrapper to handle some manipulations on .jar, .war and .ear files * * @author Khue ng */public class JahiaArchiveFileHandler { private static final String CLASS_NAME = JahiaArchiveFileHandler.class.getName(); /** The full path to the file **/ private String m_FilePath; /** The JarFile object **/ private JarFile m_JarFile; /** * Constructor * * @param (String) path, the full path to the file * @exception IOException */ public JahiaArchiveFileHandler ( String path ) throws IOException { m_FilePath = path; File f = new File(path); try { m_JarFile = new JarFile(f); } catch ( IOException ioe ) { JahiaConsole.println(CLASS_NAME+"::Constructor","IOException occurred " + f.getAbsolutePath() ); throw new IOException (CLASS_NAME +" IOException occurred "); } catch ( java.lang.NullPointerException e ) { JahiaConsole.println(CLASS_NAME+"::Constructor","NullPointerException " + f.getAbsolutePath() ); throw new IOException (CLASS_NAME + " NullPointerException occurred "); } if ( m_JarFile == null ) { throw new IOException (CLASS_NAME+" source file is null"); } } /** * Decompresses the file in it's current location * */ public void unzip() throws JahiaException { try { File f = new File(m_FilePath); //JahiaConsole.println(CLASS_NAME + ".upzip"," Start Decompressing " + f.getName() ); String parentPath = f.getParent() + File.separator; String path = null; FileInputStream fis = new FileInputStream(m_FilePath); BufferedInputStream bis = new BufferedInputStream(fis); ZipInputStream zis = new ZipInputStream(bis); ZipFile zf = new ZipFile(m_FilePath); ZipEntry ze = null; String zeName = null; try{ while ( (ze = zis.getNextEntry()) != null ){ zeName = ze.getName(); path = parentPath + genPathFile(zeName); File fo = new File(path); if ( ze.isDirectory() ){ fo.mkdirs(); } else { copyStream(zis,new FileOutputStream(fo)); } zis.closeEntry(); } } finally { // Important !!! zf.close(); fis.close(); zis.close(); bis.close(); } //JahiaConsole.println(CLASS_NAME+".unzip"," Decompressing " + f.getName() + " done ! "); } catch ( IOException ioe ) { JahiaConsole.println("JahiaArchiveFileHandler"," fail unzipping " + ioe.getMessage() ); throw new JahiaException ("JahiaArchiveFileHandler", "faile processing unzip", JahiaException.SERVICE_ERROR, JahiaException.ERROR); } } /** * Decompress the file in a gived folder * * @param (String) path */ public void unzip(String path) throws JahiaException { try { File f = new File(m_FilePath); //JahiaConsole.println(CLASS_NAME+".unzip(path)"," Start Decompressing " + f.getName() ); String destPath = null; FileInputStream fis = new FileInputStream(m_FilePath); BufferedInputStream bis = new BufferedInputStream(fis); ZipInputStream zis = new ZipInputStream(bis); ZipFile zf = new ZipFile(m_FilePath); ZipEntry ze = null; String zeName = null; try { while ( (ze = zis.getNextEntry()) != null ){ zeName = ze.getName(); destPath = path + File.separator + genPathFile(zeName); File fo = new File(destPath); if ( ze.isDirectory() ){ fo.mkdirs(); } else { File parent = new File(fo.getParent()); parent.mkdirs(); FileOutputStream fos = new FileOutputStream(fo); copyStream(zis,fos); } zis.closeEntry(); } } finally { // Important !!! zf.close(); fis.close(); zis.close(); bis.close(); } //JahiaConsole.println(CLASS_NAME+".unzip(path)"," Decompressing " + f.getName() + " done ! "); } catch ( IOException ioe ) { JahiaConsole.println(CLASS_NAME+".unzip(path)"," fail unzipping " + ioe.getMessage() ); throw new JahiaException (CLASS_NAME, "faile processing unzip", JahiaException.SERVICE_ERROR, JahiaException.ERROR, ioe); } } /** * Extract an entry of file type in the jar file * Return a File Object reference to the uncompressed file * * @param (String) entryName, the entry name * @return (File) fo, a File Handler to the file ( It's a temporary file ) */ public File extractFile( String entryName ) throws IOException, JahiaArchiveFileException { File tmpFile = null; // Create a temporary file and write the content of the file in it ZipEntry entry = m_JarFile.getEntry(entryName); if ( (entry != null) && !entry.isDirectory() ) { InputStream ins = m_JarFile.getInputStream(entry); if ( ins != null ){ // File f = new File(""); does not work on UNIX systems because we // don't have write permissions all over the disk by default like // under Windows. // However Khue says it's necessary for Tomcat 4.0.2 ... This is // weird, more investigation will be needed. Maybe Tomcat 4.0.2 // messes with jahia.io.tmpdir global system property ? // tmpFile = File.createTempFile("jahia_temp","",f); tmpFile = File.createTempFile("jahia_temp","",null); if ( tmpFile == null || !tmpFile.canWrite() ){ throw new IOException ("extractFile error creating temporary file"); } FileOutputStream outs = new FileOutputStream(tmpFile); copyStream(ins,outs); outs.flush(); outs.close(); } } else { JahiaConsole.println(CLASS_NAME+".extractFile(entry)", " entryName" + " is null or a directory " ); throw new JahiaArchiveFileException (JahiaException.ENTRY_NOT_FOUND); } return tmpFile; } /** * Extract an entry in a gived folder. If this entry is a directory, * all its contents are extracted too. * * @param (String) entryName, the name of an entry in the jar * @param (String) destPath, the path to the destination folder */ public void extractEntry( String entryName, String destPath ) throws JahiaException { try { ZipEntry entry = m_JarFile.getEntry(entryName); if ( entry == null ){ StringBuffer strBuf = new StringBuffer(1024); strBuf.append(" extractEntry(), cannot find entry "); strBuf.append(entryName); strBuf.append(" in the jar file "); //JahiaConsole.println(CLASS_NAME + ".extractEntry", strBuf.toString()); throw new JahiaException (CLASS_NAME, strBuf.toString(), JahiaException.SERVICE_ERROR, JahiaException.ERROR); } File destDir = new File(destPath); if ( destDir == null || !destDir.isDirectory() || !destDir.canWrite() ){ JahiaConsole.println(CLASS_NAME+".extractEntry()"," cannot access to the destination dir "); throw new JahiaException (CLASS_NAME," cannot access to the destination dir ", JahiaException.SERVICE_ERROR, JahiaException.ERROR); } File f = new File(m_FilePath); //JahiaConsole.println(CLASS_NAME+".extractEntry(entryName,path)"," Decompressing entry " + entryName ); String path = null; FileInputStream fis = new FileInputStream(m_FilePath); BufferedInputStream bis = new BufferedInputStream(fis); ZipInputStream zis = new ZipInputStream(bis); ZipFile zf = new ZipFile(m_FilePath); ZipEntry ze = null; String zeName = null; boolean isFile = false; //JahiaConsole.println(CLASS_NAME+".extractEntry(entryName,path)"," Decompressing entry " + entryName + " in " + destDir.getAbsolutePath() ); while ( (ze = zis.getNextEntry()) != null && !ze.getName().equalsIgnoreCase(entryName) ) { // loop until the requested entry //JahiaConsole.println(CLASS_NAME+".extractEntry(extryName, path)"," bypass " + ze.getName() ); zis.closeEntry(); } //JahiaConsole.println(CLASS_NAME+".extractEntry(entryName,path)"," found entry " + ze.getName()); try{ if ( ze.isDirectory() ){ //JahiaConsole.println(CLASS_NAME+".extractEntry(entryName,path)"," entry " + ze.getName() + " is a Directory Entry "); while ( ze != null ){ zeName = ze.getName(); path = destPath + File.separator + genPathFile(zeName); File fo = new File(path); if ( ze.isDirectory() ){ fo.mkdirs(); } else { FileOutputStream outs = new FileOutputStream(fo); copyStream(zis,outs); //outs.flush(); //outs.close(); } zis.closeEntry(); ze = zis.getNextEntry(); } } else { //JahiaConsole.println(CLASS_NAME+".extractEntry(entryName,path)"," entry " + ze.getName() + " is a File Entry "); zeName = ze.getName(); path = destPath + File.separator + genPathFile(zeName); //JahiaConsole.println(CLASS_NAME+".extractEntry(entryName,path)"," file path " + path ); File fo = new File(path); FileOutputStream outs = new FileOutputStream(fo); copyStream(zis,outs); //outs.flush(); //outs.close(); } //JahiaConsole.println(CLASS_NAME+".extractEntry(entryName,path)"," extract " + entryName + " done ! "); } finally { // Important !!! zf.close(); fis.close(); zis.close(); bis.close(); } } catch ( IOException ioe ) { JahiaConsole.println(CLASS_NAME," fail unzipping " + ioe.getMessage() ); throw new JahiaException (CLASS_NAME, "faile processing unzip", JahiaException.SERVICE_ERROR, JahiaException.ERROR); } } /** * Return an entry in the jar file of the gived name or null if not found * * @param (String) entryName the entry name * @return (ZipEntry) the entry */ public ZipEntry getEntry(String entryName){ return m_JarFile.getEntry(entryName); } /** * Check if an entry is a directory or not * * @param (String) entryName the entry name * @return (boolean) true if the entry exists and is a a directory */ public boolean isDirectory(String entryName){ return ((m_JarFile.getEntry(entryName) != null) && m_JarFile.getEntry(entryName).isDirectory()); } /** * Check if an entry exist or not * * @param (String) entryName the entry name * @return (boolean) true if exist */ public boolean entryExists(String entryName){ return ( m_JarFile.getEntry(entryName) != null ); } /** * Close the Zip file. Important to close the JarFile object * to be able to delete it from disk. * */ public void closeArchiveFile(){ try { m_JarFile.close(); } catch ( IOException e ) { JahiaConsole.println(CLASS_NAME,"cannot close jar file"); // cannot close file } } /** * Generates a file path for a gived entry name * Parses "/" char and replaces them with File.separator char * */ protected String genPathFile(String entryName){ StringBuffer sb = new StringBuffer(entryName.length()); for ( int i= 0; i< entryName.length() ; i++ ){ if ( entryName.charAt(i) == '/' ){ sb.append(File.separator); } else { sb.append(entryName.charAt(i)); } } return ( sb.toString() ); } /** * Copy an InputStream to an OutPutStream * * @param ins An InputStream. * @param outs An OutputStream. * @exception IOException. */ protected void copyStream( InputStream ins, OutputStream outs) throws IOException { int bufferSize = 1024; byte[] writeBuffer = new byte[bufferSize]; BufferedOutputStream bos = new BufferedOutputStream(outs, bufferSize); int bufferRead; while((bufferRead = ins.read(writeBuffer)) != -1) bos.write(writeBuffer,0,bufferRead); bos.flush(); bos.close(); outs.flush(); outs.close(); }} // End Class JahiaArchiveFileHandler
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -