【问题标题】:Finding a file in zipentry java在 zipentry java 中查找文件
【发布时间】:2012-06-22 19:46:41
【问题描述】:

我正在尝试在 zip 文件中查找文件并将其作为 InputStream 获取。所以这就是我目前正在做的事情,我不确定我是否做对了。

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

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;
}

现在我面临的主要问题是Search_Image 中的ZipInputStreamZipInputStream 的原始组件相同...

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); 
}

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

【问题讨论】:

    标签: java zipinputstream


    【解决方案1】:

    如果您还不需要输入流,您应该使用 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;
    }
    

    一些事实:

    • 您应该遵循代码中命名方法和变量的约定(Search_Image 不行,searchImage 可以)
    • zip 文件中的目录不包含任何文件,它们只是与其他所有内容一样的条目,因此您不应尝试递归到它们中)
    • 您应该使用 endsWith(name) 比较您提供的名称,因为文件可能位于文件夹中,而 zip 中的文件名始终包含路径

    【讨论】:

    • 当我寻找的图像位于带有 zip 的文件夹中时会发生什么?我有这个,因为我最初的方法问题是这不会在目录中搜索图像。
    • 因为你用的是equals(..)而不是endsWith(..),看看我的第三点。
    【解决方案2】:

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

    为了获得最佳性能,您需要使用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;
    }
    

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

    【讨论】:

      【解决方案3】:

      这是我的看法:

      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;
      }
      

      【讨论】:

        猜你喜欢
        • 2017-02-11
        • 2016-08-21
        • 1970-01-01
        • 2011-09-09
        • 2011-06-18
        • 1970-01-01
        • 1970-01-01
        • 2016-07-02
        • 2016-05-31
        相关资源
        最近更新 更多