读写ZIP&JAR文件
1. ZipEntry 是包括目录的,也就是目录也被当做是一个单独的Entry,在列出它下面的文件之前先列出这个directory entry.
这个在解压ZIP文件的的时候特别有用,我们要先创建这个目录,然后在解压目录下面的文件,否则解压的时候会说目录不存在.
ZipInputStream zis = new ZipInputStream(new FileInputStream("F:\\workspace\\HibernateSrc\\lib\\hibernate3.jar"));
ZipEntry entry = zis.getNextEntry();
2.JarEntry是不包括目录的,只包括class文件对应的entry, 要想访问目录,请使用ZipEntry读取Jar文件.
JarInputStream jis = new JarInputStream(new FileInputStream(mainJar));
JarEntry entry = jis.getNextJarEntry();
3. 往jar文件里写数据的时候,要首先调用jos.putNextEntry(entry);放入一个entry记录,然后调用jos.write(temp, 0, count);往对应的记录写数据.
4. 读entry内容的时候,先要穿件一个JarFile or ZipFile, 然后遍历所有的entry记录,可以直接调用zip.entires()来得到entry的所有记录,也可以对jar&zip文件
创建一个JarInputStream & ZipInputStream 来遍历entry记录, 当选定了entry后,调用JarFile.getInputStream(entry)来拿到对应entry记录的输入流.
下面的蓝色部分代码是读入的操作,红色部分代码是输出的操作
JarFile zipIn = new JarFile(mainJar);
InputStream readin = null;
JarOutputStream jos = new JarOutputStream(new FileOutputStream("rt.jar"));
JarInputStream jis = new JarInputStream(new FileInputStream(mainJar));
JarEntry entry = jis.getNextJarEntry();
while(entry!=null) {
String name = entry.getName();
//remove the .class suffix.
name = name.substring(0,name.lastIndexOf("."));
if(depencyClass.contains(name)) {
//put an entry record and write the binary data
jos.putNextEntry(entry);
readin = zipIn.getInputStream(entry);
byte[] temp = new byte[4096];
int count = readin.read(temp);
while (count != -1) {
jos.write(temp, , count);
count = readin.read(temp);
}
readin.close();
}
entry = jis.getNextJarEntry();
}
jis.close();
jos.close();
下面是两个例子
ReduceJRE是用来抽取Jar里面特定的class文件到一个新的rt.jar
public class ReduceJRE {
public static void main(String[] args) throws Exception {
String mainJar = null;
String classDenpdencyFile = null;
if(args!=null && args.length==2)
{
mainJar = args[0];
classDenpdencyFile = args[1];
}
else {
mainJar = "F:\\Program Files\\Java\\jre7\\lib\\rt.jar";
classDenpdencyFile = "F:\\Program Files\\Java\\jre7\\lib\\classdepency.txt";
}
List<String> depencyClass = new ArrayList<String>();
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(classDenpdencyFile)));
String templine = br.readLine();
// load all the dependency class and store them in a array list;
while(templine!=null) {
int end = templine.lastIndexOf("from");
int begin = templine.lastIndexOf("[Loaded")+7;
String className = templine.substring(begin,end).replace(".", "/").trim();
depencyClass.add(className);
templine= br.readLine();
}
JarFile zipIn = new JarFile(mainJar);
InputStream readin = null;
JarOutputStream jos = new JarOutputStream(new FileOutputStream("rt.jar"));
JarInputStream jis = new JarInputStream(new FileInputStream(mainJar));
JarEntry entry = jis.getNextJarEntry();
while(entry!=null) {
String name = entry.getName();
//remove the .class suffix.
name = name.substring(0,name.lastIndexOf("."));
if(depencyClass.contains(name)) {
//put an entry record and write the binary data
jos.putNextEntry(entry);
readin = zipIn.getInputStream(entry);
byte[] temp = new byte[4096];
int count = readin.read(temp);
while (count != -1) {
jos.write(temp, 0, count);
count = readin.read(temp);
}
readin.close();
}
entry = jis.getNextJarEntry();
}
jis.close();
jos.close();
}
}
extractZIPFile 把spPath指定的ZIP文件解压到destinationDir,如果指定了fileIdentifier,那么久解压指定的文件,否则解压全部文件。
private String extractZIPFile(String spPath, String destinationDir, String fileIdentifier) throws IOException {
ZipFile zip = new ZipFile(spPath);
String rootfolderPath = null;
FileOutputStream fos = null;
InputStream readin = null;
/*create the destination folder if it is non-existed.*/
File destFolder = new File(destinationDir);
if (!destFolder.exists()) {
destFolder.mkdirs();
}
try {
for (Enumeration<? extends ZipEntry> enums = zip.entries(); enums.hasMoreElements(); ) {
ZipEntry entry = (ZipEntry) enums.nextElement();
/*if specified the identifier and the current entry does not match it. then just skip this entry*/
if (fileIdentifier != null) {
if (!entry.getName().contains(fileIdentifier)) {
continue;
} else {
String fileName = destinationDir + separator + entry.getName().substring(entry.getName().lastIndexOf("/"));
File f = new File(fileName);
rootfolderPath = f.getCanonicalPath();
readin = zip.getInputStream(entry);
fos = new FileOutputStream(f);
byte[] temp = new byte[BUFFER_SIZE];
int count = readin.read(temp);
while (count != -1) {
fos.write(temp, 0, count);
count = readin.read(temp);
}
/*need to close the fos/readin as the fos/readin will be refer to another entry next time*/
fos.close();
readin.close();
}
} else {
String fileName = destinationDir + separator + entry.getName();
File f = new File(fileName);
if (entry.isDirectory()) {
f.mkdirs();
if (rootfolderPath == null) {
rootfolderPath = f.getCanonicalPath();
}
} else {
readin = zip.getInputStream(entry);
fos = new FileOutputStream(f);
byte[] temp = new byte[BUFFER_SIZE];
int count = readin.read(temp);
while (count != -1) {
fos.write(temp, 0, count);
count = readin.read(temp);
}
/*need to close the fos/readin as the fos/readin will be refer to another entry next time*/
fos.close();
readin.close();
}
}
}
return rootfolderPath;
} catch (Exception e) {
log.error("Bad file format: " + spPath, e);
return null;
} finally {
UpgradeUtils.close(readin, fos);
zip.close();
}
}
读写ZIP&JAR文件的更多相关文章
- 分享非常有用的Java程序 (关键代码) (三)---创建ZIP和JAR文件
原文:分享非常有用的Java程序 (关键代码) (三)---创建ZIP和JAR文件 import java.util.zip.*; import java.io.*; public class Zip ...
- Linux shell 中提取zip或jar文件中的某个文件
Linux shell 中提取zip或jar文件中的某个文件 假如有个压缩包 abc.jar, 里面文件如下 (可以用unzip -l abc.jar 查看): data/1.txt data/2.t ...
- springboot jar文件打zip包运行linux环境中
1.添加打包配置文件 1.1 assembly.xml <assembly xmlns="http://maven.apache.org/plugins/maven-assembly ...
- Struts2.5需要的最少jar文件
以Struts2.5.2为例 从官网上下载“struts-2.5.2-min-lib.zip”,里面有7个jar文件: commons-fileupload-1.3.2.jarcommons-io-2 ...
- 编译protobuf的jar文件
1.准备工作 需要到github上下载相应的文件,地址https://github.com/google/protobuf/releases protobuf有很多不同语言的版本,因为我们需要的是ja ...
- Android项目实战(二十四):项目包成jar文件,并且将工程中引用的jar一起打入新的jar文件中
前言: 关于.jar文件: 平时我们Android项目开发中经常会用到第三方的.jar文件. 其实.jar文件就是一个类似.zip文件的压缩包,里面包含了一些源代码,注意的是.jar不包含资源文件(r ...
- java打jar包,引用其他.jar文件
大家都知道一个java应用项目可以打包成一个jar,当然你必须指定一个拥有main函数的main class作为你这个jar包的程序入口. 具体的方法是修改jar包内目录META-INF下的MANIF ...
- 打包jar文件 外部调用资源 so等
一个非常好的从jar文件中加载so动态库方法,在android的gif支持开源中用到.这个项目的gif解码是用jni c实现的,避免了OOM等问题. 项目地址:https://github.com/k ...
- 总结Spring、Hibernate、Struts2官网下载jar文件
一直以来只知道搭SSH需要jar文件,作为学习的目的,最好的做法是自己亲自动手去官网下.不过官网都是英文,没耐心一般很难找到下载入口,更何 况版本的变化也导致不同版本jar文件有些不一样,让新手很容易 ...
随机推荐
- HTML5怎样在网页中使用摄像头功能
怎样在网页中使用摄像头,html5越来越多的使用到实际工作中,那么他怎样调用摄像头呢进行视频聊天,视频监控等活动呢,今天为大家抛砖引玉,教大家怎样实现怎样利用html5实现摄像头视频监控功能.废话不多 ...
- sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"问题解决
安装一个软件时,遇到这个问题sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION" 上网 ...
- java工程包的命名(-dev.jar,-javadoc.jar,jar)
1.-javadoc.jar 这个应该是一个doc包,就是API,相当于帮助文档,用来说明类.方法.静态变量 都是干什么的. 2.-dev.jar,.jar都有可能包含源码,也可能都不包含
- Powershell获取WMI设备清单
支持所有PS版本. WMI服务能够报告详细的硬件信息.通常,每个硬件都来自它们自己的WMI代理类.但是要找出这些硬件类的名字是不容易. 所有硬件类都在同一个WMI根下面,你可以在根类查询所有的硬件: ...
- 《GK101任意波发生器》升级固件发布(版本:1.0.2.build124)
一.固件说明: 硬件版本:0,logic.3 固件版本:1.0.2.build124 编译日期:2014-08-19 ====================================== 二. ...
- 【新产品发布】【GK101 10MHz任意波发生器】
简介: GK101 10MHz掌上任意波形发生器基于多项先进技术,在较小的体积上实现了普通台式仪器才具有的功能.仪器仅手掌大小,实现了80M采样率.10MHz最大频率.10Vpp最高幅度的输出. 仪器 ...
- Apache Spark源码走读之3 -- Task运行期之函数调用关系分析
欢迎转载,转载请注明出处,徽沪一郎. 概要 本篇主要阐述在TaskRunner中执行的task其业务逻辑是如何被调用到的,另外试图讲清楚运行着的task其输入的数据从哪获取,处理的结果返回到哪里,如何 ...
- Memcached 笔记与总结(2)编译 php-memcache 扩展
环境:CentOS 6.6 + Apache 2.2.21 + PHP 5.3.10 php-memcache 是 php 写的 memcached 的客户端,以扩展的形式发布. 对于正在运行的 ph ...
- 批处理之命令补充II
网络命令 net use \\ip\ipc$ " " /user:" " 建立IPC空链接 net use \\ip\ipc$ "密码" / ...
- 不再写.bat
<script type="text/javascript"> for (var w = 0; w < 24; w++) { setTimeout(functio ...