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. Easyui的datagrid结合hibernate实现数据分页

    最近在学习easyui的使用,在学到datagrid的时候遇到了一些问题,终于抽点时间整理了一下,分享出来,请各位前辈高手多多指教! 1.先来看看效果,二话不说,上图直观! 2.easyui的data ...

  2. 六、saltstack的module组件

    Module是saltstack日常使用中用的最多的一个组件.用于管理操作对象. 查看系统module: [root@super65 ~]# salt 'super66' sys.list_modul ...

  3. oracle在线重定义表

    在一个高可用系统中,如果需要改变一个表的定义是一件比较棘手的问题,尤其是对于7×24系统.Oracle提供的基本语法基本可以满足一般性修改,但是对于把普通堆表改为分区表,把索引组织表修改为堆表等操作就 ...

  4. react-native 学习

    官网 https://facebook.github.io/react-native/docs/getting-started.html#content 下载 npm install -g react ...

  5. WTF,这到底是在做什么?

    1 <?php 2 $data = "<soap:Envelope>[...]</soap:Envelope>"; 3 $tuCurl = curl_ ...

  6. diff和patch配合使用(转载备用)

    Linux下diff与patch命令的配合使用 在Linux下,diff与patch命令配合使用可以进行简单的代码维护工作. [A] diffdiff命令用于比较文件的差异,可以用于制作patch文件 ...

  7. SqlParameter 基本用法

    因为通过SQL 语句的方式,有时候存在脚本注入的危险,所以在大多数情况下不建议用拼接SQL语句字符串方式,希望通过SqlParameter实现来实现对数据的操 作,针对SqlParameter的方式我 ...

  8. Java自学之道全文下载地址

     道可道非常道,名可名非常名. Java自学之道博大精深,自己只是将理论和实际相结合,进行了简单总结. Java自学之道全文可在自己的博客下载 http://my.oschina.net/mkh/bl ...

  9. 项目部署到Tomat报错:jar not loaded.See Servlet Spec 2.3, section 9.7.2. Offending

    项目部署到Tomcat报这样的异常: Java代码   jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: ja ...

  10. 【Spec for GS5】不要嘲笑程序员不懂烂漫

    // // main.cpp // 生日快乐 // // Created by wasdns on 16/11/21. // Copyright © 2016年 wasdns. All rights ...