天天看點

讀取壓縮檔案下多級目錄的檔案(按行讀取)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;

public class ZipUtil {

	/**
	 * 讀取壓縮檔案下多級目錄的檔案(按行讀取)
	 * @author  zhengqiang
	 */
	public List<String> printZipTxt(String zipPath) throws IOException{
		List<String> list = new ArrayList<String>();
		
		ZipFile zipFile=new ZipFile(zipPath); 
		for (Enumeration<? extends ZipEntry> e = zipFile.getEntries(); e.hasMoreElements();){
			ZipEntry entry=e.nextElement();
			if(!entry.isDirectory()){
				
				BufferedReader br=new BufferedReader(new InputStreamReader(zipFile.getInputStream(entry)));
				String line = "";
				while((line = br.readLine()) != null){
					if(line.length() > 0){
						list.add(line);
					}
				}
				br.close();
			}
		}
		return list;
	}
}