Java对Jar文件的操作[转]
原文地址:http://www.cnblogs.com/mailingfeng/archive/2012/04/24/2122160.html
String dirPath = System.getProperty("user.dir") + "\\conf";
File dirFile = new File(dirPath);
获取jar包中文件的方法,是不能使用上面的方法的。需要通过遍历jar包中的文件来获得,此处如果不以“/” 开头,会被当作相对于当前类所在的包类处理,自然无法找到。Jar包内的文件是无法用File读取的,只能用Stream的方式读取。
获取jar包中的内容需要通过遍历JarFile下的JarEntry,通过File.list()是不能获得jar包下面的文件的。
例子程序:
包含了使用JarOutputStream和JarFile来进行 jar包的复制、解压操作,实际使用中使用JarOutputStream或JarFile的一种即可。
package jdk.util; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry; import org.apache.log4j.Logger;
import org.junit.Test; public class Util_Jar_Test { Logger log = Logger.getLogger(this.getClass()); //复制jar
public void copyJar(File src , File des) throws FileNotFoundException, IOException{
JarInputStream jarIn = new JarInputStream(new BufferedInputStream(new FileInputStream(src)));
Manifest manifest = jarIn.getManifest();
JarOutputStream jarOut = null;
if(manifest == null){
jarOut = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(des)));
}else{
jarOut = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(des)),manifest);
} byte[] bytes = new byte[1024];
while(true){
//重点
ZipEntry entry = jarIn.getNextJarEntry();
if(entry == null)break;
jarOut.putNextEntry(entry); int len = jarIn.read(bytes, 0, bytes.length);
while(len != -1){
jarOut.write(bytes, 0, len);
len = jarIn.read(bytes, 0, bytes.length);
}
log.info("Copyed: " + entry.getName());
// jarIn.closeEntry();
// jarOut.closeEntry();
String a = new String();
a.length();
}
jarIn.close();
jarOut.finish();
jarOut.close();
} //解压jar
public void unJar(File src , File desDir) throws FileNotFoundException, IOException{
JarInputStream jarIn = new JarInputStream(new BufferedInputStream(new FileInputStream(src)));
if(!desDir.exists())desDir.mkdirs();
byte[] bytes = new byte[1024]; while(true){
ZipEntry entry = jarIn.getNextJarEntry();
if(entry == null)break; File desTemp = new File(desDir.getAbsoluteFile() + File.separator + entry.getName()); if(entry.isDirectory()){ //jar条目是空目录
if(!desTemp.exists())desTemp.mkdirs();
log.info("MakeDir: " + entry.getName());
}else{ //jar条目是文件
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(desTemp));
int len = jarIn.read(bytes, 0, bytes.length);
while(len != -1){
out.write(bytes, 0, len);
len = jarIn.read(bytes, 0, bytes.length);
} out.flush();
out.close(); log.info("Copyed: " + entry.getName());
}
jarIn.closeEntry();
} //解压Manifest文件
Manifest manifest = jarIn.getManifest();
if(manifest != null){
File manifestFile = new File(desDir.getAbsoluteFile()+File.separator+JarFile.MANIFEST_NAME);
if(!manifestFile.getParentFile().exists())manifestFile.getParentFile().mkdirs();
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(manifestFile));
manifest.write(out);
out.close();
} //关闭JarInputStream
jarIn.close();
} //复制jar by JarFile
public void copyJarByJarFile(File src , File des) throws IOException{
//重点
JarFile jarFile = new JarFile(src);
Enumeration<JarEntry> jarEntrys = jarFile.entries();
JarOutputStream jarOut = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(des)));
byte[] bytes = new byte[1024]; while(jarEntrys.hasMoreElements()){
JarEntry entryTemp = jarEntrys.nextElement();
jarOut.putNextEntry(entryTemp);
BufferedInputStream in = new BufferedInputStream(jarFile.getInputStream(entryTemp));
int len = in.read(bytes, 0, bytes.length);
while(len != -1){
jarOut.write(bytes, 0, len);
len = in.read(bytes, 0, bytes.length);
}
in.close();
jarOut.closeEntry();
log.info("Copyed: " + entryTemp.getName());
} jarOut.finish();
jarOut.close();
jarFile.close();
} //解压jar文件by JarFile
public void unJarByJarFile(File src , File desDir) throws FileNotFoundException, IOException{
JarFile jarFile = new JarFile(src);
Enumeration<JarEntry> jarEntrys = jarFile.entries();
if(!desDir.exists())desDir.mkdirs(); //建立用户指定存放的目录
byte[] bytes = new byte[1024]; while(jarEntrys.hasMoreElements()){
ZipEntry entryTemp = jarEntrys.nextElement();
File desTemp = new File(desDir.getAbsoluteFile() + File.separator + entryTemp.getName()); if(entryTemp.isDirectory()){ //jar条目是空目录
if(!desTemp.exists())desTemp.mkdirs();
log.info("makeDir" + entryTemp.getName());
}else{ //jar条目是文件
//因为manifest的Entry是"META-INF/MANIFEST.MF",写出会报"FileNotFoundException"
File desTempParent = desTemp.getParentFile();
if(!desTempParent.exists())desTempParent.mkdirs(); BufferedInputStream in = new BufferedInputStream(jarFile.getInputStream(entryTemp));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(desTemp)); int len = in.read(bytes, 0, bytes.length);
while(len != -1){
out.write(bytes, 0, len);
len = in.read(bytes, 0, bytes.length);
} in.close();
out.flush();
out.close(); log.info("Copyed: " + entryTemp.getName());
}
} jarFile.close();
} /*实验结论:
* 1.JarInputStream的getNextJarEntry()和jarOutputStream的putNextJarEntry()中没有包括"META-INF/MANIFEST.MF"这一项,因此复制和解压都 要注意
* 2.JarFile的entries()方法包含了全部Entry,也包括"META-INF/MANIFEST.MF",没有"META-INF/"这一项,因此在解压的时候要先检测父文件存不存在
* 4.复制jar文件有3中方法, A是直接用BufferedInputStream和BufferedOutputStream复制,
* B是用JarInputStream的getNextJarEntry()和jarOutputStream的putNextJarEntry()
* C是用JarFile的entries()方法,遍寻JarEntry的InputStream,以此写出
* 5.解压jar的话推荐使用JarFile,当前实例方法只支持解压jar文件
* 6.在复制的时候,src文件只可以是jar文件,但des文件可以是带zip或rar后缀的文件
*/ @Test
public void testCopyJar(){
File src = new File("C:/a.jar");
File des = new File("C:/testCopy.jar");
//实验表明只运行复制和解压jar文件
// File src = new File("C:/rtf.zip");
// File des = new File("C:/testCopy.zip");
try {
copyJar(src,des);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @Test
public void testUnJar(){
File src = new File("C:/a.jar");
// File src = new File("C:/b.rar"); //不支持rar解压
String desFile = "aa";
File desDir = new File(src.getParent()+File.separator+desFile);
try {
unJar(src, desDir);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @Test
public void testCopyJarByJarFile(){
File src = new File("C:/a.jar");
File des = new File("C:/testCopy.zip");
try {
copyJarByJarFile(src,des);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @Test
public void testUnJarByJarFile(){
File src = new File("C:/a.jar");
// File src = new File("C:/b.rar"); //不支持rar解压
String desFile = "aa";
File desDir = new File(src.getParent()+File.separator+desFile);
try {
unJarByJarFile(src, desDir);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Java对Jar文件的操作[转]的更多相关文章
- loadrunner 脚本开发-调用java jar文件远程操作Oracle数据库测试
调用java jar文件远程操作Oracle数据库测试 by:授客 QQ:1033553122 测试环境 数据库:linux 下Oracle_11g_R2 Loadrunner:11 备注:想学ora ...
- Java的jar文件安装成windows 服务
Java的jar文件安装成windows 服务: 1.下载:nssm,复制到jar文件目录下 2. jar文件目录下创建bat文件[run.bat],内容为[java -jar 文件名.jar] 3. ...
- 更新java对xml文件的操作
//更新java在xml文件中操作的内容 public static void upda(Document doc) throws Exception{ //创建一个TransformerFactor ...
- Java 字节流实现文件读写操作(InputStream-OutputStream)
Java 字节流实现文件读写操作(InputStream-OutputStream) 备注:字节流比字符流底层,但是效率底下. 字符流地址:http://pengyan5945.iteye.com/b ...
- Java 图片爬虫,java打包jar文件
目录 1. Java 图片爬虫,制作 .jar 文件 spider.java 制作 jar 文件 添加执行权限 1. Java 图片爬虫,制作 .jar 文件 spider.java spider.j ...
- Java 执行jar文件出现版本错误信息
Java 执行jar文件出现版本错误信息 一.问题 执行jar文件出现如下错误信息: 二.解决方案 是因为在创建工程的时候选择的jdk编译版本,和执行jar环境的jdk版本不一致: 更改工程的jdk版 ...
- java 打包jar文件以在没有安装JDK或JRE的机子上运行
前言: java号称“一次编译,到处运行”,但这有个前提,那就是你的机子上得安装java环境.对于开发人员或其他一些比较懂计算机的人来说这没什么,但是对于一些不懂计算机的人来说这会很麻烦,他们更希望的 ...
- Xamarin.Android 入门之:Bind java的jar文件+Android显示gif图片
一.引言 在xamarin开发的时候,有时我们想要做一个功能,但是这个功能已经有人用java写好了,并且打包成了jar文件.那么我们可以直接把对方的jar文件拿过来用而不是重新用c#写代码. 关于bi ...
- [Java] 在 jar 文件中读取 resources 目录下的文件
注意两点: 1. 将资源目录添加到 build path,确保该目录下的文件被拷贝到 jar 文件中. 2. jar 内部的东西,可以当作 stream 来读取,但不应该当作 file 来读取. 例子 ...
随机推荐
- Nginx安装手册
前提是搭建yum安装环境,见前面的教程资料 Nginx安装手册1 nginx安装环境 nginx是C语言开发,建议在linux上运行,本教程使用Centos6.5作为安装环境. gcc 安装ngin ...
- Dora.Interception, 一个为.NET Core度身打造的AOP框架[3]:Interceptor的注册
在<不一样的Interceptor>中我们着重介绍了Dora.Interception中最为核心的对象Interceptor,以及定义Interceptor类型的一些约定.由于Interc ...
- mysql按照天统计报表,当天没有数据,填0
1.问题复现: 按照天数统计每天的总数,如果其中有几天没有数据,那么group by 返回会忽略那几天,如何填充0?如下图,统计的10-3~10-10 7天的数据,其中只有8号和10号有数据,这样返回 ...
- POJ 1422 Air Raid
题目链接: http://poj.org/problem?id=1422 Description Consider a town where all the streets are one-way a ...
- Spark性能调优之Shuffle调优
Spark性能调优之Shuffle调优 • Spark底层shuffle的传输方式是使用netty传输,netty在进行网络传输的过程会申请堆外内存(netty是零拷贝),所以使用了堆外内存. ...
- CCF系列之数字排序(201503-2)
问题描述试题编号: 201503-2试题名称: 数字排序时间限制: 1.0s内存限制: 256.0MB问题描述: 问题描述 给定n个整数,请统计出每个整数出现的次数,按出现次数从多到少的顺序输出. 输 ...
- Django文件上传三种方式以及简单预览功能
主要内容: 一.文件长传的三种方式 二.简单预览功能实现 一.form表单上传 1.页面代码 <!DOCTYPE html> <html lang="en"> ...
- 2017-06-29(cat tac more less head tail)
cat 查看文件内容 cat -A 相当于-vET的整合参数,可列出一些特殊的字符,而不是空白而已 -b 列出行号,空白行不标号 -E 将结尾的断行字符 $ 显示出来 -n 列出行号,空 ...
- python装饰器的用法
def logger(func): def inner(*args, **kwargs): #1 print "Arguments were: %s, %s" ...
- js_3_for_if_try
在js中有哪些特殊变量? null 指向一个空地址,一个特殊的地址 var u 定义了一个特殊变量u,类型未定义,boolean(u)=false js中的for循环是什么样子? 对列表: for(v ...