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中文件/目錄便利,以及讀?。?/h4>
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從流中讀取(和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(); // 關(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é):文章來源:http://www.zghlxwxcb.cn/news/detail-563718.html
由于zip和jar結(jié)構(gòu)時(shí)一致的,所以ZipFile和JarFile,ZipInputStream和JarInputStream的使用方法是一樣的。需要說明的一點(diǎn)是,由于zip包的這種特殊結(jié)構(gòu),默認(rèn)ZipInputStream中是不包含數(shù)據(jù)的,只有在調(diào)用getNextEntry方法后,才回把對(duì)應(yīng)的entry(zip包中的一個(gè)文件)內(nèi)容寫入到ZipInputStream中。上面的一個(gè)錯(cuò)誤寫法中,可以看到直接從ZipInputStream中讀不到數(shù)據(jù),只有調(diào)用getNextEntry后才可以。文章來源地址http://www.zghlxwxcb.cn/news/detail-563718.html
到了這里,關(guān)于java讀取zip/jar包中的文件的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!