1. 程式人生 > >從zip壓縮包中提取檔案中的關鍵字

從zip壓縮包中提取檔案中的關鍵字

本文目的是從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();
		}

	}
}

其中壓縮包檔案按照檔案程式碼中的檔案路徑名稱自行建立即可。