最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java讀取zip/jar包中文件的幾種方式

 更新時(shí)間:2023年07月25日 11:06:35   作者:趕路人兒  
這篇文章主要給大家介紹了關(guān)于java讀取zip/jar包中文件的幾種方式,在我們?nèi)粘J褂弥袎嚎s文件是非常常用的,文中通過示例代碼將java讀取zip/jar包中文件的方法介紹的非常詳細(xì),需要的朋友可以參考下

1、jar vs zip:

jar 文件和 zip 文件都是歸檔文件,并且都經(jīng)過壓縮。事實(shí)上,jar 文件使用與 zip 文件相同的存檔和壓縮技術(shù),所以 jar 文件實(shí)際上是一種特定類型的 zip 文件。(JAR 文件本質(zhì)上是一個(gè)包含可選 META-INF 目錄的 zip 文件。)這一切都意味著:

  • 您可以使用與打開 zip 文件相同的工具打開 jar 文件
  • jar 文件是 zip 文件的子集,因此如果 zip 文件遵循 jar 規(guī)范,則它可以用作 jar 文件

2、讀取zip壓縮文件:

1)方法一:通過ZipFile從文件中讀取

private static void readZipFile() {
    try (ZipFile zipFile = new ZipFile("/data/testzip.zip");) {
	    Enumeration<? extends ZipEntry> entries = zipFile.entries();
         while(entries.hasMoreElements()){
             ZipEntry entry = entries.nextElement();
		 System.out.println("fileName:"+entry.getName()); //文件名
		 InputStream stream = zipFile.getInputStream(entry); //讀取文件內(nèi)容
		 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;
}
//錯(cuò)誤方法
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(); // 關(guān)閉zipEntry
			}
		}
	} finally {
		zis.close(); //關(guān)閉zipInputStream
	}
}

注意:在從流中讀取數(shù)據(jù)是使用了IOUtils,原因是自定義read方法讀取完后會(huì)把傳遞進(jìn)來的inputStream給關(guān)閉了。如果zip包中有多個(gè)文件,那么在讀取第二個(gè)entry文件時(shí)就會(huì)報(bào)錯(cuò)。zipInputStream只能在最后關(guān)閉。而IOUtils使用了copy的方式,不會(huì)關(guān)閉傳入的流。

3、jar中文件/目錄便利,以及讀?。?/h2>

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<JarEntry> 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 api
try (Stream<JarEntry> 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從流中讀?。ê蚙ipInputStream類似)

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(); // 關(guān)閉zipEntry
			}
		}
	} finally {
		jis.close(); //關(guān)閉zipInputStream
	}
}

3)方法三:通過JarURLConnection來打開一個(gè)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();
	}
}

總結(jié):

由于zip和jar結(jié)構(gòu)時(shí)一致的,所以ZipFile和JarFile,ZipInputStream和JarInputStream的使用方法是一樣的。需要說明的一點(diǎn)是,由于zip包的這種特殊結(jié)構(gòu),默認(rèn)ZipInputStream中是不包含數(shù)據(jù)的,只有在調(diào)用getNextEntry方法后,才回把對應(yīng)的entry(zip包中的一個(gè)文件)內(nèi)容寫入到ZipInputStream中。上面的一個(gè)錯(cuò)誤寫法中,可以看到直接從ZipInputStream中讀不到數(shù)據(jù),只有調(diào)用getNextEntry后才可以。

到此這篇關(guān)于java讀取zip/jar包中文件的幾種方式的文章就介紹到這了,更多相關(guān)java讀取zip/jar包文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

新建县| 乌鲁木齐市| 丹东市| 余庆县| 东安县| 西宁市| 旬阳县| 青浦区| 龙陵县| 疏勒县| 鄂温| 阿尔山市| 石棉县| 楚雄市| 霍城县| 镇坪县| 金川县| 南华县| 遂平县| 霍山县| 如皋市| 沙田区| 蓝山县| 瓮安县| 新津县| 台安县| 内丘县| 冀州市| 泸州市| 安徽省| 桃园市| 南漳县| 金门县| 灯塔市| 施秉县| 万宁市| 乌鲁木齐县| 玛曲县| 佳木斯市| 临夏县| 南靖县|