首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 企业软件 > 行业软件 >

java操作jar资料

2013-08-01 
java操作jar文件package org.jacobsonimport java.io.Fileimport java.io.FileInputStreamimport java.

java操作jar文件

package org.jacobson;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.net.URL;import java.net.URLClassLoader;import java.util.Enumeration;import java.util.Set;import java.util.jar.Attributes;import java.util.jar.JarEntry;import java.util.jar.JarFile;import java.util.jar.JarInputStream;import java.util.jar.Manifest;import java.util.zip.ZipEntry;import java.util.zip.ZipFile;import java.util.zip.ZipInputStream;public class JarFileProcessor {/** * 程序执行入口 * @param args 命令行参数列表 * @throws IOException 文件操作异常。 */public static void main(String args[]) throws IOException {new JarFileProcessor().run();}/** * 测试方法 * @throws IOException 文件操作异常 */public void run() throws IOException {// be readyString filePath = "C:/Documents and Settings/Administrator/桌面/新建文件夹/"+ "commons-io.jar";URL url = new File(filePath).toURL();JarFile jarFile = new JarFile(url.getFile().substring(1));// print all entry messages 1printAllEntriesMessage(jarFile);// print all entry messages 2printAllEntriesMessage2(jarFile);// print manifest messageprintManifestMessage(jarFile);// unzip a jar fileString localPath = "C:/Documents and Settings/Administrator/桌面/新建文件夹/新建文件夹";unzip(jarFile, localPath, 2011);// print all entry as classes.printJarClasss(jarFile, url);}/** * 打印Jar包压缩的文件的信息。 * @param jarFile Jar文件 * @throws IOException 文件流操作失败时抛出 * @throws FileNotFoundException 找不到Jar文件时抛出 */private void printAllEntriesMessage(JarFile jarFile) throws IOException,FileNotFoundException {System.out.println("JAR包:" + jarFile.getName());JarInputStream jarInputStream = new JarInputStream(new FileInputStream(jarFile.getName()));JarEntry jarEntry = null;while ((jarEntry = jarInputStream.getNextJarEntry()) != null) {String jarEntryName = jarEntry.getName();if (jarEntry.isDirectory()) {System.out.println("目录:" + jarEntryName);} else {System.out.println("文件:" + jarEntryName);}}}/** * 打印Jar包压缩的文件的信息。 * @param jarFile Jar文件 */private void printAllEntriesMessage2(JarFile jarFile) {System.out.println("JAR包:" + jarFile.getName());Enumeration<JarEntry> entries = jarFile.entries();while (entries.hasMoreElements()) {JarEntry jarEntry = entries.nextElement();String jarEntryName = jarEntry.getName();if (jarEntry.isDirectory()) {System.out.println("目录:" + jarEntryName);} else {System.out.println("文件:" + jarEntryName);}}}/** * 打印jar包的Manifest信息。 * @param jarFile jar文件 */private void printManifestMessage(JarFile jarFile){try {Manifest manifest = jarFile.getManifest();Attributes attributes = manifest.getMainAttributes();Set<Object> keyset = attributes.keySet();for (Object objKey : keyset) {String value = (String) attributes.get(objKey);System.out.println(objKey + " : " + value);}} catch (IOException e) {e.printStackTrace();}}/** * 解压缩zip格式的压缩文件。 * @param zipFile zip压缩包 * @param localPath 解压的目的地 * @param bufferSize 解压过程中使用的buffer的大小 */public static void unzip(ZipFile zipFile, String localPath, int bufferSize) {byte[] buffer = new byte[bufferSize];ZipInputStream zip = null;ZipEntry zipEntry = null;try {zip = new ZipInputStream(new FileInputStream(zipFile.getName()));while ((zipEntry = zip.getNextEntry()) != null) {File file = new File(localPath, zipEntry.getName()).getAbsoluteFile();System.out.println(file.getName());if (zipEntry.isDirectory()) {file.mkdirs();} else {File parent = file.getParentFile();if (!parent.exists()) {parent.mkdirs();}file.createNewFile();long size = zipEntry.getSize();FileOutputStream fos = null;try {fos = new FileOutputStream(file);int readLen = -1;while (size != 0) {readLen = zip.read(buffer);size -= readLen;if (readLen != -1) {fos.write(buffer, 0, readLen);}}} finally {if (fos != null) {fos.close();}}}}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {if (zip != null) {zip.close();}} catch (IOException e) {e.printStackTrace();}}}/** * 打印jar包中所具有的类的类信息。 * @param jarFile Jar文件 * @param jarFileURL jar文件对应的URL */private void printJarClasss(JarFile jarFile, URL... jarFileURL) {Enumeration<JarEntry> entries = jarFile.entries();while (entries.hasMoreElements()) {JarEntry jarEntry = entries.nextElement();String entryName = jarEntry.getName();if (jarEntry.isDirectory()) {// System.out.println(entryName);} else if (entryName.endsWith(".class")) {String className = entryName.replaceAll("/", ".").substring(0,entryName.length() - 6);URLClassLoader urlLoader = new URLClassLoader(jarFileURL);try {Class clazz = urlLoader.loadClass(className);System.out.println(" Class => " + clazz);} catch (ClassNotFoundException e) {e.printStackTrace();}}}}}

?

热点排行