我想重置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();
          }

        }

最佳答案

如果可能的话(即您有一个实际的文件,而不仅仅是要读取的流),请尝试使用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)));
}

关于inputstream - 如何重用/重置ZipInputStream?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3912172/

10-10 14:31