我有工作解压缩代码,它使用 ZipInputStream 从 InputStream 解压缩一个大的 zip 文件。我在 Android 5.1.1 上测试了一些示例 ZIP 文件。我最近将我的开发 Galaxy S5 更新到了 Android 6.0.1。相同的代码不再适用于相同的测试 ZIP 文件。ZipInputStream.getNextEntry() 在第一次迭代时返回 null。
Android 5.1.1 和 Android 6.0.1 中的 ZipInputStream 会发生什么变化吗?我不能使用 ZipFile,因为我只有用户通过文档/内容提供程序选择的流。
是否有解压缩 InputStream 的替代方法?我无法从 [File] 源解压缩,它必须是流。
该代码类似于许多用于解压缩的标准片段:
public void unzip() {
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(mStream));
try {
String filename;
ZipEntry ze;
byte[] buffer = new byte[1024 * 8];
int count;
while ((ze = zis.getNextEntry()) != null) {
filename = ze.getName();
if (ze.isDirectory()) {
File fmd = new File(mPath + filename);
fmd.mkdirs();
continue;
}
// openFile is a helper function that attempts to open a FileOutputStream with built in retry
FileOutputStream fout = openFile(mPath + filename, 3, 100);
if (fout == null) {
Log.e(TAG, "unzip::Failed to open file: " + mPath + filename);
continue;
}
while ((count = zis.read(buffer)) != -1) {
fout.write(buffer, 0, count);
}
fout.close();
zis.closeEntry();
}
} catch (IOException ex) {
Log.e(TAG, "UnzipFileTask", ex);
} finally {
try { zis.close(); } catch (IOException e) { }
}
}
编辑:似乎与我的 SD 卡有关。可能是 SD 卡加密导致三星 Galaxy S5 上的 Android 6.0.1 出现问题。解压缩与内部存储(也加密)上的 ZIP 文件一起使用。
编辑2:这与解压缩无关,它与我的SD卡加密是否启用有关。关闭 SD 卡加密,一切正常。三星在带有 Galaxy S5 的 Android 6.0.1 上的 SD 卡加密出现了问题。我无法使用我的应用在设备存储和 SD 卡之间复制文件。我的文件应用程序虽然有效。
我正在针对 API 22 进行编译,因此我没有添加新的运行时权限,并且我知道如果我的目标是 22 或更低,我不需要它们。