微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 apache.commons.compress.archivers.zip 读取远程 zip 文件

如何解决使用 apache.commons.compress.archivers.zip 读取远程 zip 文件

我正在尝试读取远程 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;
  }
}

任何帮助将不胜感激。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。