读写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文件有些不一样,让新手很容易 ...
随机推荐
- JS。 问题类型:穷举,迭代。两个关键词:break和continue
问题类型: 穷举:(在不知道什么情况下是我们需要的结果的时候只能够让它一个一个都给走一遍) 百鸡百钱:公鸡1钱,母鸡2钱,小鸡0.5钱. 思路: 公鸡买100只,母鸡,小鸡都是0只: 母鸡50只,公鸡 ...
- Linux环境下实现哲学家就餐问题(2)
#include <stdio.h> #include <pthread.h> #include <stdlib.h> #include <string.h& ...
- iisNS 安装程序的思路
1. 安装程序的目录和 frontEnd一个目录,通过判断是否存在 common/config/db.php 来验证是否已经安装过,如果已经安装过,该文件会自动生成 2. 如果没有安装过,则跳转到 ...
- HDU 2795 Billboard(线段树的另类应用)
Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- ASP.NET\ASP.NET MVC表单提交遇到的问题结论
同步提交的两种基本方式 1,用type=“submit”按钮.form没有必要runat=“server” <form method="post" action=" ...
- JS页面跳转 神器! window.href window.open
window.open 打开页面, 网址为新的网址 window.href 为打开本网址的链接, 为网站URL+传入的URL
- python多线程使用
内容链接: http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/00138683236 ...
- Windows下编译objective-C
Windows下编译objective-C 2011-08-31 14:32 630人阅读 评论(0) 收藏 举报 windowscocoa工具objective clibraryxcode 目录 ...
- RT-Thread的CPU使用率计算
CPU 的使用率一般是我们比较关心的问题,在这里我们就用空闲线程的钩子函数去统计 CPU 的使用率,并通过串口打印出来.首先我们在初始化线程中设置好钩子函数,并在 LED 线程中给系统人为的加入很多“ ...
- github 有名的问题【ERROR: Permission to .git denied to user】
小乌龙 以前一直是单兵做战,所以github repo对于我而言,只是一个存放.同步.备份代码的地方,协同作用完全没有体现出来. 最近跟朋友一起开发一个项目,他在github建了个公共的repo,我正 ...