ZipInputStream getNextEntry()在Android 6.x更新后返回Null

时间:2017-01-23 18:19:29

标签: java android android-sdcard android-external-storage zipinputstream

我有使用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或更低,我就不需要它们。

0 个答案:

没有答案