java 在zipentry java中查找文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11123528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:54:09  来源:igfitidea点击:

Finding a file in zipentry java

javazipinputstream

提问by Donkey King

I am trying to find a file within a zip file and get it as an InputStream. So this is what I am doing to get it so far and I am not certain if I am doing it correctly.

我正在尝试在 zip 文件中找到一个文件并将其作为InputStream. 所以这就是我目前正在做的事情,我不确定我是否做得正确。

Here is a sample as the original is slightly longer but this is the main component...

这是一个示例,因为原件稍长,但这是主要组件...

public InputStream Search_Image(String file_located, ZipInputStream zip) 
    throws IOException {
    for (ZipEntry zip_e = zip.getNextEntry(); zip_e != null ; zip_e = zip.getNextEntry()) {
        if (file_located.equals(zip_e.getName())) {
            return zip;
        }
        if (zip_e.isDirectory()) {
            Search_Image(file_located, zip); 
        }
    }
    return null;
}

Now the main problem I am facing is that The ZipInputStreamin Search_Imageis the same as the original component of the ZipInputStream...

现在我面临的主要问题是ZipInputStreaminSearch_ImageZipInputStream...

if(zip_e.isDirectory()) {
    //"zip" is the same as the original I need a change here to find folders again.
    Search_Image(file_located, zip); 
}

Now for the question, how do you get the ZipInputStreamas the new zip_entry? Also please add in if I did anything wrong in my method as my logic with this class is still lacking.

现在的问题是,你如何获得ZipInputStream新的zip_entry?如果我在我的方法中做错了什么,也请补充,因为我的这个类的逻辑仍然缺乏。

回答by Hyman

You should use the class ZipFilewithout worrying yourself with an input stream if you don't need it yet.

ZipFile如果您还不需要它,您应该使用该类而不必担心输入流。

ZipFile file = new ZipFile("file.zip");
ZipInputStream zis = searchImage("foo.png", file);

public InputStream searchImage(String name, ZipFile file) {
  for (ZipEntry e : Collections.list(file.entries())) {
    if (e.getName().endsWith(name)) {
      return file.getInputStream(e);
    }
  }
  return null;
}

Some facts:

一些事实:

  • you should follow conventions for naming methods and variables in your code (Search_Imageis not fine, searchImageis)
  • directories in zip files does not contain any file, they are just entries like everything else so you shouldn't try to recurse into them)
  • you should compare the name you provide by using endsWith(name)because the file could be inside a folder and a filename inside a zip always contains the path
  • 您应该遵循在代码中命名方法和变量的约定(Search_Image不好,searchImage是)
  • zip 文件中的目录不包含任何文件,它们只是像其他所有内容一样的条目,因此您不应该尝试递归它们)
  • 您应该比较您提供的名称,endsWith(name)因为该文件可能位于文件夹内,而 zip 内的文件名始终包含路径

回答by Nicolas Filotto

Accessing to a zip entry using ZipInputStreamis clearly not the way to do it as you will need to iterate over the entries to find it which is not a scalable approachbecause the performance will depend on total amount of entries in your zip file.

使用访问 zip 条目ZipInputStream显然不是这样做的方法,因为您需要遍历条目以找到它,这不是一种可扩展的方法,因为性能将取决于 zip 文件中的条目总数。

To get the best possible performances, you need to use a ZipFilein order to access directly to an entrythanks to the method getEntry(name)whatever the size of your archive.

为了获得最佳的性能,你需要使用一个ZipFile直接访问一个条目得益于方法getEntry(name)您归档文件的大小什么的。

public InputStream searchImage(String name, ZipFile zipFile) throws IOException {
    // Get the entry by its name
    ZipEntry entry = zipFile.getEntry(name);
    if (entry != null) {
        // The entry could be found
        return zipFile.getInputStream(entry);
    }
    // The entry could not be found
    return null;
}

Please note that the name to provide here is the relative path of your imagein the archive using /as path separator so if you want to access to foo.pngthat is in the directory bar, the expected name will be bar/foo.png.

请注意,此处提供的名称是使用作为路径分隔符的存档中图像的相对路径,/因此如果您想访问foo.png目录中的图像bar,则预期名称将为bar/foo.png.

回答by ChaitanyaBhatt

Here is my take on this:

这是我的看法:

ZipFile zipFile = new ZipFile(new File("/path/to/zip/file.zip));
InputStream inputStream = searchWithinZipArchive("findMe.txt", zipFile);

public InputStream searchWithinZipArchive(String name, ZipFile file) throws Exception {
  Enumeration<? extends ZipEntry> entries = file.entries();
  while(entries.hasMoreElements()){
     ZipEntry zipEntry = entries.nextElement();
      if(zipEntry.getName().toLowerCase().endsWith(name)){
             return file.getInputStream(zipEntry);
      }
  }
  return null;
}