Unzip - java.lang.IllegalArgumentException:文件filename /包含路径分隔符

时间:2014-08-31 10:08:23

标签: android unzip

我想将包含图像的zip文件从资源复制到内部存储,然后解压缩。 这是我的代码:

   protected void copyFromAssetsToInternalStorage(String filename){
    AssetManager assetManager = getAssets();

    try {
        InputStream input = assetManager.open(filename);
        OutputStream output = openFileOutput(filename, Context.MODE_PRIVATE);

         copyFile(input, output);

    } catch (IOException e) {
        e.printStackTrace();
    }
}

private void unZipFile(String filename){
    try {
        ZipInputStream zipInputStream = new ZipInputStream(openFileInput(filename));
        ZipEntry zipEntry;

        while((zipEntry = zipInputStream.getNextEntry()) != null){
            FileOutputStream zipOutputStream = openFileOutput(zipEntry.getName(), MODE_PRIVATE);

            int length;
            byte[] buffer = new byte[1024];

            while((length = zipInputStream.read(buffer)) > 0){
                zipOutputStream.write(buffer, 0, length);
            }

            zipOutputStream.close();
            zipInputStream.closeEntry();
        }
        zipInputStream.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while((read = in.read(buffer)) != -1){
        out.write(buffer, 0, read);
    }
}

我有这个错误:     java.lang.IllegalArgumentException:文件filename /包含路径分隔符

我该怎么办?

1 个答案:

答案 0 :(得分:0)

来自openFileOutput documentation

name要打开的文件的名称; 不能包含路径分隔符。

希望这会有所帮助 亚龙