如何重用/重置ZipInputStream?

Jus*_*elp 4 inputstream reset reusability zipinputstream

我想重置ZipInputStream(即返回到起始位置),以便按顺序读取某些文件.我怎么做?我被困了......

      ZipEntry entry;
        ZipInputStream input = new ZipInputStream(fileStream);//item.getInputStream());

        int check =0;
        while(check!=2){

          entry = input.getNextEntry();
          if(entry.getName().toString().equals("newFile.csv")){
              check =1;
              InputStreamReader inputStreamReader = new InputStreamReader(input);
                reader = new CSVReader(inputStreamReader);
                //read files
                //reset ZipInputStream if file is read.
                }
                reader.close();
          }
            if(entry.getName().toString().equals("anotherFile.csv")){
              check =2;
              InputStreamReader inputStreamReader = new InputStreamReader(input);
                reader = new CSVReader(inputStreamReader);
                //read files
                //reset ZipInputStream if file is read.
                }
                reader.close();
          }

        }
Run Code Online (Sandbox Code Playgroud)

Thi*_*ilo 5

如果可能(即你有一个实际的文件,而不仅仅是一个要读取的流),尝试使用ZipFile类而不是更低级的ZipInputStream.ZipFile负责在文件中跳转并打开各个条目的流.

ZipFile zip = new ZipFile(filename);
ZipEntry entry = zip.getEntry("newfile.csv");
if (entry != null){
    CSVReader data = new CSVReader(new InputStreamReader(
         zip.getInputStream(entry)));
} 
Run Code Online (Sandbox Code Playgroud)

  • `ZipInputStream`是用纯Java编写的不同实现,它不是`ZipFile`的_low-level_.`ZipFile`使用JNI,无法处理非本地文件. (2认同)