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 来读取. 例子 ...
随机推荐
- hdu_1041(Computer Transformation) 大数加法模板+找规律
Computer Transformation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/ ...
- hdu_1017(水水水,坑格式)
#include<cstdio> #include<cmath> using namespace std; int main() { int T; scanf("%d ...
- 浅析Entity Framework Core2.0的日志记录与动态查询条件
前言 Entity Framework Core 2.0更新也已经有一段时间了,园子里也有不少的文章.. 本文主要是浅析一下Entity Framework Core2.0的日志记录与动态查询条件 去 ...
- 本地phpstudy时常停机连接失败,php.ini文件中9000端口问题
2018/01/05 13:35:07 [error] 20508#19380: *1 WSARecv() failed (10054: An existing connection was forc ...
- PHP实现伪静态方法汇总
PHP伪静态的使用主要是为了隐藏传递的参数名,下面给大家介绍php实现伪静态的方法,对php实现伪静态相关知识感兴趣的朋友一起学习吧 PHP伪静态的使用主要是为了隐藏传递的参数名,下面给大家介绍php ...
- mysql索引使用注意事项
索引是快速搜索的关键.MySQL索引的建立对于MySQL的高效运行是很重要的.下面介绍几种常见的MySQL索引类型. 在数据库表中,对字段建立索引可以大大提高查询速度.假如我们创建了一个 mytabl ...
- Reflection and array
java.lang.Reflect.Array类提供了动态创建和访问数组元素的各种静态方法. package com.sunchao.reflection; import java.lang.refl ...
- java实现定时任务
Java中实现定时任务执行某一业务.具体操作如下: 1.定义初始化任务 2.任务业务操作 3.定义初始化方法 4.在web.xml中注册启动 5.定义具体执行时间
- 修真院java后端工程师学习课程--任务1(day four)
今天学习的是spring框架,内容主要有: spring的概念,主要是做什么的: Spring是一个基于IOC和AOP的结构J2EE系统的框架 IOC 反转控制 是Spring的基础,Inversio ...
- c#中RGB与int类型之间的转换
Color color = Color.FromArgb(0, 0, 255);int colorInt = ParseRGB(color); --------------------- int Pa ...