java.util.zip.ZipException : invalid general purpose flag: 9

标签 java android zip

我在 Android 6.0 上遇到此错误

java.util.zip.ZipException:无效的通用位标志:9 java.util.zip.ZipInputStream.getNextEntry(ZipInputStream.java:253)

这是我的代码:

ZipInputStream zin = new ZipInputStream(getAppContext().getContentResolver().openInputStream(uri));

这是什么意思?我做错了什么?

最佳答案

这是 ZIP 文件规范:https://users.cs.jmu.edu/buchhofp/forensics/formats/pkzip.html

Flags   General purpose bit flag:
Bit 00: encrypted file
Bit 01: compression option
Bit 02: compression option
Bit 03: data descriptor
Bit 04: enhanced deflation
Bit 05: compressed patched data
Bit 06: strong encryption
Bit 07-10: unused
Bit 11: language encoding
Bit 12: reserved
Bit 13: mask header values
Bit 14-15: reserved 

因此,GPBF 值为 9,同时设置了“加密文件”和“数据描述符”位。

查看 Android 源代码: https://chromium.googlesource.com/android_tools/+/9e9b6169a098bc19986e44fbbf65e4c29031e4bd/sdk/sources/android-22/java/util/zip/ZipFile.java (较旧的版本,但我怀疑这没有改变)显示:

static final int GPBF_ENCRYPTED_FLAG = 1 << 0;

[...]

/**
 * Supported General Purpose Bit Flags Mask.
 * Bit mask of bits not supported.
 * Note: The only bit that we will enforce at this time
 * is the encrypted bit. Although other bits are not supported,
 * we must not enforce them as this could break some legitimate
 * use cases (See http://b/8617715).
 */
static final int GPBF_UNSUPPORTED_MASK = GPBF_ENCRYPTED_FLAG;

[...]

// At position 6 we find the General Purpose Bit Flag.
int gpbf = Short.reverseBytes(is.readShort()) & 0xffff;
if ((gpbf & ZipFile.GPBF_UNSUPPORTED_MASK) != 0) {
    throw new ZipException("Invalid General Purpose Bit Flag: " + gpbf);
}

因此,您的 ZIP 文件声称已加密该文件(设置了 GPBF 的位 00),并且 ZipFile 实现不支持读取加密文件。

关于java.util.zip.ZipException : invalid general purpose flag: 9,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41187062/

相关文章:

java - TextSecure 服务器、Signal Android 应用程序和推送服务器配置

java - 拖放 LibGDX

java - 在所有项目中运行所有测试类

java - 是否有适用于 Android 1.5 VideoView/MediaPlayer 类的工作示例?

android - Canvas 绘制文字方向

python - 如何将文件夹压缩为zip?

Java根据条件计算计数

安卓位置 : simple one-time GPS fetch using AsyncTask

python - 数据帧列表理解 "zip(...)": loop through chosen df columns efficiently with just a list of column name strings

ruby - 解压缩文件并忽略 OS X 添加的 'junk files'