iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > JAVA >java读取zip/jar包中的文件
  • 516
分享到

java读取zip/jar包中的文件

javajarjvm 2023-09-03 20:09:59 516人浏览 薄情痞子
摘要

1、jar vs zip: jar 文件和 zip 文件都是归档文件,并且都经过压缩。事实上,jar 文件使用与 zip 文件相同的存档和压缩技术,所以 jar 文件实际上是一种特定类型的 zip 文件。(JAR 文件本质上是一个包含可选

1、jar vs zip:

jar 文件和 zip 文件都是归档文件,并且都经过压缩。事实上,jar 文件使用与 zip 文件相同的存档和压缩技术,所以 jar 文件实际上是一种特定类型的 zip 文件。(JAR 文件本质上是一个包含可选 META-INF 目录的 zip 文件。)这一切都意味着:

  • 您可以使用与打开 zip 文件相同的工具打开 jar 文件
  • jar 文件是 zip 文件的子集,因此如果 zip 文件遵循 jar 规范,则它可以用作 jar 文件

2、读取zip压缩文件:

1)方法一:通过ZipFile从文件中读取

private static void readZipFile() {    try (ZipFile zipFile = new ZipFile("/data/testzip.zip");) {    Enumeration entries = zipFile.entries();         while(entries.hasMoreElements()){             ZipEntry entry = entries.nextElement(); System.out.println("fileName:"+entry.getName()); //文件名 InputStream stream = zipFile.getInputStream(entry); //读取文件内容 read(stream);         }    } catch(Exception e) {}//zipFile.close();}private static void read(InputStream in) {     try (InputStreamReader reader = new InputStreamReader(in, "UTF-8");         BufferedReader br = new BufferedReader(reader);) {         String con = null;      while ((con = br.readLine()) != null) {          System.out.println(con);      }    } catch (Exception e) {}}

2)方法二:通过ZipInputStream从流中读取

private static InputStream getInputStream() throws FileNotFoundException {File file = new File("/data/testzip.zip");InputStream in = new FileInputStream(file);return in;}//错误方法private static void readZipInputStream() throws FileNotFoundException, IOException {InputStream zippedIn = getInputStream(); // zip压缩文件流ZipInputStream zis = new ZipInputStream(zippedIn);read(zis); //读取的是空}//正确方法private static void readZipInputStream2() throws FileNotFoundException, IOException {InputStream zipFileInput = getInputStream(); // zip压缩文件流ZipInputStream zis = new ZipInputStream(zipFileInput);ZipEntry entry = null;try {while ((entry = zis.getNextEntry()) != null) {try {final String name = entry.getName();System.out.println("fileName:"+name);String content = IOUtils.toString(zis);System.out.println(content);} finally {zis.closeEntry(); // 关闭zipEntry}}} finally {zis.close(); //关闭zipInputStream}}

注意:在从流中读取数据是使用了IOUtils,原因是自定义read方法读取完后会把传递进来的inputStream给关闭了。如果zip包中有多个文件,那么在读取第二个entry文件时就会报错。zipInputStream只能在最后关闭。而IOUtils使用了copy的方式,不会关闭传入的流

3、jar中文件/目录便利,以及读取:

1)方法一:使用JarFile读取Jar文件

和ZipFile类似,使用'getEntry(String name)'或'entires'获得ZipEntry或JarEntry(它们可以看作同一东西),接下来使用" JarFile.getInputStream(ZipEntry ze)"将其用于获取InputStream

static void test1() {    String path = "/Users/liuxiao/Maven-rep/org/apache/thrift/libthrift/0.9.0/libthrift-0.9.0.jar";    try (JarFile jarFile = new JarFile(new File(path));) {        Enumeration entries = jarFile.entries();        while (entries.hasMoreElements()) {            JarEntry entry = entries.nextElement();            String entryName = entry.getName();            if (!entry.isDirectory() && entryName.equals("org/apache/thrift/TBase.java")) {                System.out.println(entryName);//  org/apache/thrift/EncodingUtils.class                read(jarFile.getInputStream(entry));            }        }    } catch (Exception e) {    }    //使用stream apitry (Stream stream = new JarFile(new File(path)).stream();) {        stream            .filter(entry -> !entry.isDirectory() && entry.getName().endsWith(".class"))            .forEach(entry -> System.out.println(entry.getName()));    } catch(Exception e) {    }}

2)方法二:通过JarInputStream从流中读取(和ZipInputStream类似)

private static InputStream getJarFileInputStream() throws FileNotFoundException {File file = new File("/data/mvn_repo/commons-lang/commons-lang/2.1/commons-lang-2.1.jar");InputStream in = new FileInputStream(file);return in;}private static void readJarInputStream2() throws FileNotFoundException, IOException {InputStream zipFileInput = getJarFileInputStream(); // jar包流JarInputStream jis = new JarInputStream(zipFileInput);JarEntry entry = null;try {while ((entry = jis.getNextJarEntry()) != null) {try {if (entry.isDirectory()) {continue;}final String name = entry.getName();System.out.println("fileName:"+name);String content = IOUtils.toString(jis);System.out.println(content);} finally {jis.closeEntry(); // 关闭zipEntry}}} finally {jis.close(); //关闭zipInputStream}}

3)方法三:通过JarURLConnection来打开一个jar中的资源,然后通过流来读取 

static void test2() throws Exception {String filePath = "/Users/liuxiao/maven-rep/org/apache/thrift/libthrift/0.9.0/libthrift-0.9.0.jar";String name = "org/apache/thrift/TBase.java";URL url = new URL("jar:file:" + filePath + "!/" + name);JarURLConnection jarConnection = (JarURLConnection) url.openConnection();try (InputStream in = jarConnection.getInputStream();BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));) {String con = null;while ((con = br.readLine()) != null) {System.out.println(con);}} catch (Exception e) {e.printStackTrace();}}

总结

由于zip和jar结构时一致的,所以ZipFile和JarFile,ZipInputStream和JarInputStream的使用方法是一样的。需要说明的一点是,由于zip包的这种特殊结构,默认ZipInputStream中是不包含数据的,只有在调用getNextEntry方法后,才回把对应的entry(zip包中的一个文件)内容写入到ZipInputStream中。上面的一个错误写法中,可以看到直接从ZipInputStream中读不到数据,只有调用getNextEntry后才可以。

来源地址:https://blog.csdn.net/liuxiao723846/article/details/130967940

--结束END--

本文标题: java读取zip/jar包中的文件

本文链接: https://www.lsjlt.com/news/392756.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作