【问题标题】:Unzip - java.lang.IllegalArgumentException: File filename/ contains a path separator解压缩 - java.lang.IllegalArgumentException:文件文件名/包含路径分隔符
【发布时间】:2014-08-31 18:30:36
【问题描述】:

我想将包含图像的 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:文件文件名/包含路径分隔符

我该怎么办?

【问题讨论】:

  • 你应该做什么:找出导致错误的行(行号在日志中,可能是这样的:FileOutputStream zipOutputStream = openFileOutput(zipEntry.getName(), MODE_PRIVATE););查看该行的变量值(使用调试器和该行上的断点)并调查为什么有一个带有路径分隔符的变量(即斜杠)。 (我猜这是因为它是一个匹配目录条目而不是文件条目的 ZipEntry 名称)

标签: android unzip


【解决方案1】:

来自 openFileOutput documentation:

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

希望这会有所帮助 亚龙

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-19
    • 1970-01-01
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    • 2010-12-06
    相关资源
    最近更新 更多