我正在尝试向 install4j 添加一个运行脚本,该脚本处理嵌入在我的安装程序中的诸如 mysql 和 tomcat 之类的 tarball 的解压缩和解压缩。我意识到我可以将这些焦油作为 ant 构建过程的一部分进行分解,但至少在一个用例中我不能这样做。
我使用 org.apache.tools.tar.TarEntry 和 TarInputStream 类在我的运行脚本操作中包含了以下代码。这工作得相当好,有一个错误。
使用此实现,长度超过 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;