commons-compress TarArchiveEntry.getName()无效的文件路径

时间:2016-01-14 18:28:32

标签: java vmware apache-commons-compress

我在这里使用apache commons压缩示例从另一个帖子中提取文件,但是它失败了:

$LESS

这只发生在我传递给它的一些vmware ova文件(它们是tar文件btw)但不包含所有ova文件;其他人工作正常。

以下是代码:

java.io.IOException: Invalid file path. 

看起来问题是在tarEntry.getName()中尝试设置destFile的值时引入的。通过使用调试器逐步执行它,destPath正在拾取额外的不可显示的字符加上单词"某人"在路径中:

public static void unTar(File tarFile, File dest) throws IOException {
    dest.mkdir();
    TarArchiveInputStream tarIn = null;

    tarIn = new TarArchiveInputStream(
                new BufferedInputStream(
                        new FileInputStream(
                                tarFile
                        )
                )
    );

    TarArchiveEntry tarEntry = tarIn.getNextTarEntry();
    // tarIn is a TarArchiveInputStream
    while (tarEntry != null) {// create a file with the same name as the tarEntry
        File destPath = new File(dest, tarEntry.getName());
        System.out.println("working: " + destPath.getCanonicalPath());
        if (tarEntry.isDirectory()) {
            destPath.mkdirs();
        } else {
            destPath.createNewFile();
            byte [] btoRead = new byte[1024];
            BufferedOutputStream bout =
                    new BufferedOutputStream(new FileOutputStream(destPath));
            int len = 0;

            while((len = tarIn.read(btoRead)) != -1)
            {
                bout.write(btoRead,0,len);
            }

            bout.close();
            btoRead = null;
        }
        tarEntry = tarIn.getNextTarEntry();
    }
    tarIn.close();
}

对于我可以成功解压的ova文件,desPath的值看起来很正常:

target/mybuildname-SNAPSHOT/extractedDirectory/<garbage characters>someone/test.ovf

&#34;某人&#34; text是一个不错的线索,因为当我用hexdump -C查看时,我在tar(ova)文件头中都看到了这个文本。但是他们并不在同一个地方。

我觉得这里的解决方案与弄清楚文件名存储的偏移量和从特定偏移量读取的内容有关。这是我最好的猜测,但我对阅读十六进制并不是很好。

重要的是要注意我的目标是读取ova中的ovf xml文件,并且我不能控制ova的创建...所以我可以&#39 ; t事先修复标题中的问题。 ova文件本身功能完善,我也可以使用tar -xvf test.ova从命令行中成功解压缩它们。实际上,如果我从命令行重新打包tar文件,上面的代码就可以工作。

0 个答案:

没有答案
相关问题