java 直接读取zip文件和文件内容

不解压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();
    }
}

你可能感兴趣的:(工具类)