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文件的更多相关文章

  1. 分享非常有用的Java程序 (关键代码) (三)---创建ZIP和JAR文件

    原文:分享非常有用的Java程序 (关键代码) (三)---创建ZIP和JAR文件 import java.util.zip.*; import java.io.*; public class Zip ...

  2. Linux shell 中提取zip或jar文件中的某个文件

    Linux shell 中提取zip或jar文件中的某个文件 假如有个压缩包 abc.jar, 里面文件如下 (可以用unzip -l abc.jar 查看): data/1.txt data/2.t ...

  3. springboot jar文件打zip包运行linux环境中

    1.添加打包配置文件 1.1  assembly.xml <assembly xmlns="http://maven.apache.org/plugins/maven-assembly ...

  4. Struts2.5需要的最少jar文件

    以Struts2.5.2为例 从官网上下载“struts-2.5.2-min-lib.zip”,里面有7个jar文件: commons-fileupload-1.3.2.jarcommons-io-2 ...

  5. 编译protobuf的jar文件

    1.准备工作 需要到github上下载相应的文件,地址https://github.com/google/protobuf/releases protobuf有很多不同语言的版本,因为我们需要的是ja ...

  6. Android项目实战(二十四):项目包成jar文件,并且将工程中引用的jar一起打入新的jar文件中

    前言: 关于.jar文件: 平时我们Android项目开发中经常会用到第三方的.jar文件. 其实.jar文件就是一个类似.zip文件的压缩包,里面包含了一些源代码,注意的是.jar不包含资源文件(r ...

  7. java打jar包,引用其他.jar文件

    大家都知道一个java应用项目可以打包成一个jar,当然你必须指定一个拥有main函数的main class作为你这个jar包的程序入口. 具体的方法是修改jar包内目录META-INF下的MANIF ...

  8. 打包jar文件 外部调用资源 so等

    一个非常好的从jar文件中加载so动态库方法,在android的gif支持开源中用到.这个项目的gif解码是用jni c实现的,避免了OOM等问题. 项目地址:https://github.com/k ...

  9. 总结Spring、Hibernate、Struts2官网下载jar文件

    一直以来只知道搭SSH需要jar文件,作为学习的目的,最好的做法是自己亲自动手去官网下.不过官网都是英文,没耐心一般很难找到下载入口,更何 况版本的变化也导致不同版本jar文件有些不一样,让新手很容易 ...

随机推荐

  1. Update From 用法

    今天遇到用一个表的字段填充另一个表的问题,整理了一下   1.在mysql中,应该使用inner join,即: UPDATE   a INNER JOIN b ON a.userName = b.u ...

  2. fastjson解析任意json

    fastjson解析任意json到bean 解析案例的代码 package com.base.config; import java.util.List; import com.alibaba.fas ...

  3. 对于默认 Windows NT 安装的 SID 值

    https://support.microsoft.com/en-us/kb/163846/zh-cn

  4. LightOJ 1188 Fast Queries(简单莫队)

    1188 - Fast Queries    PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 64 MB Gi ...

  5. 使用Grunt启动和运行

    开始使用Grunt 大多数开发人员都一致认为,JavaScript开发的速度和节奏在过去的几年里已经相当惊人.不管是Backbone.js和Ember.js的框架还是JS Bin社区,这种语言的发展变 ...

  6. ArcGISDynamicMapServiceLayer 实现条件过滤

    <html>   <head>   <meta http-equiv="Content-Type" content="text/html; ...

  7. php获取某年某月的天数 【转】

    function days_in_month($month, $year) { // calculate number of days in a month return $month == 2 ? ...

  8. css修改,类似elememt.style样式修改

    使用!important 语法优先权. .yui-b { margin-left:0px ! important; }

  9. 如何开发ecshop支付插件

    如何开发ecshop支付插件 ecshop模板网 / 2014-06-03 目标一:搞懂ecshop的支付流程 选完商品,进入购物车页面,点击“结算中心”,页面跳转到flow.php?step=che ...

  10. lua学习笔记

    工作需要,上周对lua赶进度似地学习了一遍,主要参考<lua中文教程>一书,中间参考一些<lua游戏开发实践>,首先说说这两本书,后者不适合初学,里面是对一个游戏脚本系统进行粗 ...