前言

最近项目中需要拿到.zip文件中的文件内容,之前的做法是先解压到某个目录然后在对里面的文件进行处理,后面发现其实可以不用这么做,jdk中自带的包就可以解决这个问题。

示例如下:

public static void main(String[] args) throws IOException {

    //获取文件输入流
    FileInputStream input = new FileInputStream("C:\\Users\\admin\\Desktop\\test.zip");
    ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(input), Charset.forName("GBK"));

    ZipEntry ze = null;

    //循环遍历
    while ((ze = zipInputStream.getNextEntry()) != null) {

      System.out.println("文件名:" + ze.getName() + " 文件大小:" + ze.getSize() + " bytes");
      System.out.println("文件内容:");

      //读取
      BufferedReader br = new BufferedReader(new InputStreamReader(zipInputStream,Charset.forName("GBK")));

      String line;

      //内容不为空,输出
      while ((line = br.readLine()) != null) {
        System.out.println(line);
      }
    }

    //一定记得关闭流
    zipInputStream.closeEntry();
    input.close();
  }

Java如何不解压读取.zip的文件内容

总结

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。