使用install4j打包的org.apache.tools.tar.*类时,如何处理Java中的GNU长文件名



我正在尝试向install4j添加一个Run Script,它可以处理嵌入我的安装程序中的mysql和tomcat之类的tarball的解压缩和取消标记。我意识到,作为ant构建过程的一部分,我可以爆炸这些tars,但至少有一个用例我做不到。

我已经使用org.apache.tools.tar.TarEntry和TarInputStream类将下面的代码包含在我的Run Script操作中。这是相当好的工作,有一个错误。

使用此实现,将截断长度超过99个字符的文件路径,并将生成的文件分解到顶级目录中。

我试图弄清楚这是我的实现中的一个错误,还是apache工具类的问题。当tarEntry.getName()超过99个字符时,它似乎没有返回整个路径。有没有一种简单的方法可以解决这个问题,而不必重写TarInputStream的功能?Tar.Entry有一个isGNULongNameEntry方法,但当返回true时,我似乎无法找到一种可靠的方法来说明将文件放在哪里。

有什么建议吗?

import java.io.*;
import java.util.zip.*;
import org.apache.tools.tar.TarEntry;
import org.apache.tools.tar.TarInputStream;
String outputDirectory = "mysql";
File tgzFile = new File(context.getInstallationDirectory(), outputDirectory + File.separator + "mysql-5.5.17-linux2.6-i686.tar.gz");
// Create the Tar input stream.
FileInputStream fin = new FileInputStream(tgzFile);
GZIPInputStream gin = new GZIPInputStream(fin);
TarInputStream tin = new TarInputStream(gin);
// Create the destination directory.
File outputDir = new File(outputDirectory);
outputDir.mkdir();
// Extract files.
TarEntry tarEntry = tin.getNextEntry();
while (tarEntry != null) {
    File destPath = new File(context.getInstallationDirectory(), outputDirectory + File.separator + tarEntry.getName());
tarEntry.isGNULongNameEntry()
    if (tarEntry.isDirectory()) {
        destPath.mkdirs();
    } else {
        // If the parent directory of a file doesn't exist, create it.
        if (!destPath.getParentFile().exists())
            destPath.getParentFile().mkdirs();
        FileOutputStream fout = new FileOutputStream(destPath);
        tin.copyEntryContents(fout);
        fout.close();
    // Presserve the last modified date of the tar'd files.
        destPath.setLastModified(tarEntry.getModTime().getTime());
    }
    tarEntry = tin.getNextEntry();
}
tin.close();
return true;

请随意查看我为这个ant untar问题提出的补丁它至少可以给你一些启示。

虽然POSIX tar文件中的长文件名不由apache tar库处理,但您可以使用GNU tar来创建tar文件。在这种情况下,长文件名就没有问题了。

最新更新