Java 从 ZIP 中间检索内容的最有效方法 [英] Java most efficient way to retrieve something out of the middle of a ZIP

查看:38
本文介绍了Java 从 ZIP 中间检索内容的最有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找最有效的方式(就速度而言)从 ZIP 文件的中间检索一些文件.

I am seeking for most efficient way (in terms of speed) to retrieve some file out of the middle of a ZIP file.

例如我有 ZIP 文件,其中包括 700 个文件夹(标记为 1 到 700).每个文件夹等于图片和 mp3 文件.有一个名为 Info 的特殊文件夹,其中包含 XML 文件.问题是,我需要遍历这个 ZIP 文件来查找 XML 文件,然后我显示所需文件夹中的图像.我正在使用 ZipFile 方法(因此我遍历整个 ZIP 文件,即使我想要文件夹 666,我也需要遍历 ZIP 文件中的 665 个项目)-> 从 ZIP 文件中选择非常慢.

e.g. I have ZIP file, which includes 700 folders (tagged 1 to 700). Each folder equals picture and mp3 file. There is special folder called Info, which contains XML file. Problem is, I need to iterate through this ZIP file to find XML file and then I am displaying images from desired folders. I am using ZipFile approach (thus I am iterating through whole ZIP file, even if I want folder 666, I need to go through 665 items in ZIP file) -> selecting from ZIP file is extremely slow.

请问您,如果您遇到过类似的问题,您是如何解决的?Java中是否有任何方法可以将我的ZIP文件转换为虚拟文件夹以更快地浏览它?有没有什么外部库,时间上效率最高的?

I would like to ask you, If you have faced similar issue, how have you solved this? Is there any approach in Java, which turns my ZIP file into virtual folder to browse it much more quicker? Is there any external library, which is the most efficient in terms of time?

源代码片段:

try {
  FileInputStream fin = new FileInputStream(
      "sdcard/external_sd/mtp_data/poi_data/data.zip");
  ZipInputStream zin = new ZipInputStream(fin);
  ZipEntry ze = null;
  while ((ze = zin.getNextEntry()) != null) {
    // Log.d("ZE", ze.getName());
    if (ze.getName().startsWith("body/665/")) {
      // Log.d("FILE F", "soubor: "+ze.getName());
      if (ze.getName().endsWith(".jpg")
          || ze.getName().endsWith(".JPG")) {
        Log.d("OBR", "picture: " + ze.getName());

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int count;

        while ((count = zin.read(buffer)) != -1) {
          baos.write(buffer, 0, count);
        }
        byte[] bytes = baos.toByteArray();

        bmp = BitmapFactory.decodeByteArray(bytes, 0,
            bytes.length);
        photoField.add(bmp);
        i++;
      }
    }
  }
}

推荐答案

ZipFile.getEntry()ZipFile.getInputStream() 方法可用于访问ZIP 存档中的特定文件.例如:

The ZipFile.getEntry() and ZipFile.getInputStream() methods can be used to access a specific file in a ZIP archive. For example:

ZipFile file = ...
ZipEntry entry = file.getEntry("folder1/picture.jpg");
InputStream in = file.getInputStream(entry);

这篇关于Java 从 ZIP 中间检索内容的最有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆