【问题标题】:Reading remote zip file using apache.commons.compress.archivers.zip使用 apache.commons.compress.archivers.zip 读取远程 zip 文件
【发布时间】:2021-04-18 12:00:20
【问题描述】:

我正在尝试读取远程 zip 文件(使用 IONIC vendor 创建),但我需要使用 apache.commons.compress.archivers.zip 才能仅使用 java 来实现它。

我已经尝试过使用 java.util.zip ZipInputStream 但没有成功(对于常规 zip 文件,它可以工作,但不适用于 IONIC zip files)。

我尝试过的代码:

  public static String read(String zipPath, String partOfFileName) throws IOException {
    URL url = new URL(zipPath);
    InputStream in = new BufferedInputStream(url.openStream(), 1024);
    ZipInputStream stream = new ZipInputStream(in);
    ZipEntry entry;

    while ((entry = stream.getNextEntry()) != null) {
            if (FilenameUtils.getName(entry.getName()).contains(partOfFileName)) {
                StringBuilder out = getTxtFiles(stream);
                return out.toString();
            }
    }

    return zipPath;
}

private static StringBuilder getTxtFiles(InputStream in) {
    StringBuilder out = new StringBuilder();
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    String line;
    try {
        while ((line = reader.readLine()) != null) {
            out.append(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return out;
  }
}

我们将不胜感激。

【问题讨论】:

  • 手动复制到本地文件是否有效?
  • 是的,当然。手动操作时确实有效。
  • 如果应用到本地文件,您的代码是否有效?
  • 我通过使用 ZipFile() 解决了这个问题

标签: java zip zipinputstream


【解决方案1】:

我使用 FileZip() 解决了这个问题:

        ZipFile zipFile = new ZipFile(new File(zipPath));
        List<String> filesList = new ArrayList<>();
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        ZipEntry entry = null;
        
        StringBuilder out = null;
        while (entries.hasMoreElements()) {
            entry = entries.nextElement();
           
            if (FilenameUtils.getName(entry.getName()).contains(partOfFileName)) {
                out = getTxtFiles(zipFile.getInputStream(entry));
                LogUtil.logServer(out.toString());

                return out.toString();
            }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-19
    • 2013-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多