📄 jeffile.java
字号:
//// ____.// __/\ ______| |__/\. _______// __ .____| | \ | +----+ \// _______| /--| | | - \ _ | : - \_________// \\______: :---| : : | : | \________>// |__\---\_____________:______: :____|____:_____\// /_____|//// . . . i n j a h i a w e t r u s t . . .//package org.jahia.services.jef;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.FileNotFoundException;import java.io.DataInputStream;import java.io.IOException;import java.io.File;import java.io.EOFException;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.util.zip.CRC32;import java.util.*;import org.jahia.utils.JahiaConsole;import org.jahia.registries.ServicesRegistry;import org.jahia.exceptions.JahiaException;import java.security.*;import java.security.spec.*;import javax.crypto.*;import javax.crypto.spec.*;/** * Load and extract data from a JEF file * * @author Khue Nguyen * @version 1.0 */class JefFile{ /** the full file path */ private String filePath = ""; /** the Hashtable of key values */ private Hashtable keys = new Hashtable(); /** * the Hashtable with the key = the filename and the value = the file content in a byte array. */ private Hashtable files = new Hashtable(); private static String CLASS_NAME = "JefFile"; private static final String CHAR_ENC = "UTF-16"; private final String MSG_INTERNAL_ERROR = "JefFile internal error"; //------------------------------------------------------------------------- /** * Construct a jef file object by extracting encrypted * information out of the specified jef file. * * @param String the full path to the file * * @exception IOException when the file does not exist or on read error * @exception InvalidJefException * Throws this exception when the specified jef file is * not valid. * @exception JahiaException when a java security error occurs */ public JefFile (String path) throws IOException , InvalidJefFileException, JahiaException{ readFile(path); } //------------------------------------------------------------------------- /** * Return the hastable of keys * * @return Hashtable keys the hastables of keys */ public final Hashtable getKeys(){ return keys; } //------------------------------------------------------------------------- /** * Return the key value * * @param String the key name * @return String the key value as String */ public final String getKey(String name){ return (String)keys.get(name); } //------------------------------------------------------------------------- /** * Return the hastable of files * * @return Hashtable files, the hastables of files */ public final Hashtable getFiles(){ return files; } //------------------------------------------------------------------------- /** * Return an enumaration of all filenames * * @return Hashtable files, the hastables of files */ public final Enumeration getFilenames(){ return files.keys(); } //------------------------------------------------------------------------- /** * Save an extracted file to disk * * @param String filename, the filename of the file to save * @param String dest, the destination file full path * @return boolean true if successful */ public final boolean extractFile(String filename, String dest){ boolean result = true; if ( filename == null || dest == null ){ return false; } byte[] aFile = (byte[])files.get(filename); if ( aFile == null ){ return false; } File f = new File(dest); // write out the original file. try { FileOutputStream fos = new FileOutputStream (dest); fos.write (aFile); // flush the stream into the file and close the file fos.flush(); fos.close(); } catch (FileNotFoundException ex) { System.out.println ("ERROR : Could not create the encrypted file"); result = false; } catch (SecurityException ex) { System.out.println ("ERROR : No security permissions to create the file."); result = false; } catch (IOException ex) { System.out.println ("ERROR : I/O error."); result = false; } return result; } //------------------------------------------------------------------------- /** * Save all extracted files to disk in a gived directory * * @param String dest, the destination folder * @return boolean true if successful */ public final boolean extractFiles(String dest){ boolean result = true; if ( dest == null ){ return false; } File f = new File(dest); if ( !f.isDirectory() || !f.canWrite() ){ return false; } //JahiaConsole.println(CLASS_NAME, "extractFiles , dest = " + f.getAbsolutePath() ); Enumeration filenames = getFilenames(); String filename = null; // write out the original file. while ( filenames.hasMoreElements() ){ filename = (String)filenames.nextElement(); if ( !extractFile(filename,dest + File.separator + filename) ) { return false; } } return result; } //------------------------------------------------------------------------- /** * Save all extracted at the current folder * * @return boolean true if successful */ public final boolean extractFiles(){ boolean result = true; File f = new File(filePath); if ( !f.isFile() ){ return false; } return extractFiles(f.getParentFile().getAbsolutePath()); } //------------------------------------------------------------------------- /** * Method getTokens : return an arrays of String tokens * * @param str the string to parse * @param sep the separator * @return an array of string values * @author NK */ private static String[] getTokens(String str, String sep) { if (str==null){ return null; } StringTokenizer st = new StringTokenizer(str,sep); String[] result = new String[st.countTokens()]; int count = 0; while ( st.hasMoreTokens() ){ result[count] = st.nextToken(); count++; } return result; } //------------------------------------------------------------------------- /** * parse the keys from a string like "key1=val1,key2=val2,key3=val3" * * @param String keysString the keys String to parse * @return */ private static Hashtable parseKeys(String keysString) throws InvalidJefFileException{ Hashtable keys = new Hashtable(); if ( keysString == null || keysString.trim().equals("") ){ throw new InvalidJefFileException ("invalid keys values"); } //JahiaConsole.println(CLASS_NAME," parseKeys keyString = " + keysString); String k = ""; String val = ""; String[] tokens = getTokens(keysString,","); for ( int i=0 ; i<tokens.length ; i++ ){ if ( (tokens[i].indexOf("=") == -1) || tokens[i].indexOf("=") == (tokens[i].length()-1) ){ throw new InvalidJefFileException ("invalid keys values"); } k = tokens[i].substring(0,tokens[i].indexOf("=")); val = tokens[i].substring(tokens[i].indexOf("=")+1,tokens[i].length()); //JahiaConsole.println(CLASS_NAME," found key: name = " + k + ", val = " + val); keys.put(k.trim(),val.trim()); } return keys; } //------------------------------------------------------------------------- /** * Read the encrypted file * * @param String the path to the file */ private final void readFile(String path) throws IOException , InvalidJefFileException, JahiaException{ FileInputStream fstream = null; DataInputStream stream = null; ByteArrayOutputStream bos = null; ByteArrayInputStream byteStream = null; try { // open the file File srcFile = new File (path);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -