本文目的是從zip壓縮包中獲取各個文件中內容,然後從中查找關鍵字,將包含關鍵字的文件目錄打印出來。
package com.spider.readzip;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import org.junit.Test;
public class Readzip {
@Test
public void read() {
try {
File file = new File("C:\\Users\\zhangsan\\Desktop\\11.zip");
StringBuffer context = new StringBuffer();
String keyword = "password-username";
ZipFile zipFile = new ZipFile(file);
InputStream in = new BufferedInputStream(new FileInputStream(file));
ZipInputStream zipInputStream = new ZipInputStream(in);
ZipEntry zipEntry;
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
BufferedReader br = new BufferedReader(new InputStreamReader(zipFile.getInputStream(zipEntry)));
String line;
while ((line = br.readLine()) != null) {
context.append(line);
}
String[] keys = keyword.split("-");
String path = null;
for (int i = 0; i < keys.length; i++) {
if ((context.toString()).contains(keys[i])) {
path = file.getAbsolutePath() + zipEntry.getName();
}
}
System.out.println("文件路徑是:" + path + "內容是:" + context + "包含的關鍵字是:" + keyword);
}
} catch (ZipException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
其中壓縮包文件按照文件代碼中的文件路徑名稱自行創建即可。