如何从表示为 byte [] 的 zip 存档中提取数据?

huangapple 未分类评论50阅读模式
英文:

How do I extract data from a zip archive that is represented as byte []?

问题

以下是翻译好的部分:

如何从表示为 byte [] 的 zip 归档中提取数据?

public static byte[] extractSingleFile(byte[] zip) {
    final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(zip);
    final ZipInputStream zipInputStream = new ZipInputStream(byteArrayInputStream);
    final byte[] bytes = IOUtils.toByteArray(zipInputStream);

    return bytes;
}

始终返回空的 byte []

如果我使用以下方式将 byte [] zip 写入磁盘文件:

final FileOutputStream fileOutputStream = new FileOutputStream(new File("somefile.zip"));
fileOutputStream.write(zip);

那么创建的 zip 归档会正常打开(即问题不在传递给方法的数组中)。

英文:

How do I extract data from a zip archive that is represented as byte []?

public static byte[] extractSingleFile(byte[] zip) {
        final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(zip);
        final ZipInputStream gzipInputStream = new ZipInputStream(byteArrayInputStream);
        final byte[] bytes = IOUtils.toByteArray(gzipInputStream);

        return bytes;
}

Always returns an empty byte []

If I write byte [] zip as a file to disk using

final FileOutputStream fileOutputStream = new FileOutputStream(new File("somefile.zip"));
fileOutputStream.write(zip);

Then the created zip archive is opened normally (i.e. the problem is not in the array passed to the method)

huangapple
  • 本文由 发表于 2020年4月10日 13:46:45
  • 转载请务必保留本文链接:https://java.coder-hub.com/61134677.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定