java 直接讀取zip檔案和檔案內容
阿新 • • 發佈:2019-01-05
不解壓zip檔案,直接讀取zip包內的資料夾以及檔案內容
zip包內內容:
程式碼如下:
import java.io.*;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
public class ReadFile {
public static void main(String[] args) throws IOException {
String path = "F:\\*******\\201707\\78641695079026649.zip" ;
ZipFile zf = new ZipFile(path);
InputStream in = new BufferedInputStream(new FileInputStream(path));
Charset gbk = Charset.forName("gbk");
ZipInputStream zin = new ZipInputStream(in,gbk);
ZipEntry ze;
while((ze = zin.getNextEntry()) != null){
if (ze.toString().endsWith("txt")){
BufferedReader br = new BufferedReader(
new InputStreamReader(zf.getInputStream(ze)));
String line;
while((line = br.readLine()) != null){
System.out.println(line.toString());
}
br.close();
}
System.out.println();
}
zin.closeEntry();
}
}