【问题标题】:How do I unzip and untar a file embedded in my installer built with install4j?如何解压缩和解压缩嵌入在使用 install4j 构建的安装程序中的文件?
【发布时间】:2012-02-08 22:45:56
【问题描述】:

我正在为一个新团队/项目评估 install4j。该计划是用一个 install4j 安装程序替换一组特定于其平台的本地安装程序。我为团队构建的输出创建了一个简单的安装程序。 Install4j 使用简单的安装程序做得很好,它将构建的输出放入自解压文件中,并在执行时将其放入目录中。

但是,当我尝试解压缩和解压缩嵌入在构建输出中的文件时,它会失败,但出现以下异常。

install4j 不能“开箱即用”处理 gzip 的 tar 文件吗?我需要为此编写自定义代码吗?或者它看起来像其他类型的错误?我检查了,我可以自己从命令行将文件解压缩并解压到我指定的目录中。

这是我需要在我的 MySQL、Tomcat 等安装程序中处理多个工件的事情。

java.util.zip.ZipException: error in opening zip file
    at java.util.zip.ZipFile.open(Native Method)
    at java.util.zip.ZipFile.<init>(ZipFile.java:131)
    at java.util.zip.ZipFile.<init>(ZipFile.java:148)
    at com.install4j.runtime.beans.actions.files.AbstractExtractZipFileAction.getMaxProgress(Unknown Source)
    at com.install4j.runtime.beans.actions.files.AbstractExtractZipFileAction.extractZip(Unknown Source)
    at com.install4j.runtime.beans.actions.files.AbstractExtractZipFileAction.execute(Unknown Source)
    at com.install4j.runtime.beans.actions.SystemInstallOrUninstallAction.install(Unknown Source)
    at com.install4j.runtime.installer.InstallerContextImpl.performActionInt(Unknown Source)
    at com.install4j.runtime.installer.ContextImpl.performAction(Unknown Source)
    at com.install4j.runtime.installer.controller.Controller.executeActions(Unknown Source)
    at com.install4j.runtime.installer.controller.Controller.handleCommand(Unknown Source)
    at com.install4j.runtime.installer.controller.Controller.start(Unknown Source)
    at com.install4j.runtime.installer.Installer.runInProcess(Unknown Source)
    at com.install4j.runtime.installer.Installer.main(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at com.exe4j.runtime.LauncherEngine.launch(Unknown Source)
    at com.install4j.runtime.launcher.Launcher.main(Unknown Source)

【问题讨论】:

    标签: installation install4j


    【解决方案1】:

    这并没有我想象的那么难。我在this post 中找到了一个 JAVA 代码的 sn-p 代码。

    下面是我最终添加到“运行脚本”操作的代码。为正在安装的平台确定合适的 mysql tar 文件还不够聪明,但这是一个开始。

    import java.io.*;
    import java.util.zip.*;
    import org.apache.tools.tar.TarEntry;
    import org.apache.tools.tar.TarInputStream;
    
    File tgzFile = new File(context.getInstallationDirectory(), "mysql/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);
    
    String outputDirectory = "mysql";
    
    // 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());
    
        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;
    

    【讨论】:

    • 我发现这个实现存在一个问题。它不处理 GNU 长文件名。因此路径长度超过 99 个字符的 tar 文件无法正确处理。仍在努力。
    【解决方案2】:

    install4j 中的 ZIP 操作目前不适用于 gzip 压缩的 tar 文件,只能用于 ZIP 文件。

    【讨论】:

    • 好的,这就回答了我现在必须编写自定义代码的问题。有什么建议/提示/指向最有效的方法吗?我可以轻松地使用“运行脚本”操作吗?还是我需要写一个扩展?如果我需要写一个扩展,你能指点我任何教程吗?
    • 今天我遇到了同样的问题,但它仍然不适用于 6.0.4 版本。也许它可能是下一个版本的改进?否则我必须在 maven 构建时解压和压缩我的文件,但这不是首选的解决方法。
    • 我已将此添加到我们的问题跟踪器中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-01
    • 2011-11-15
    • 2021-09-12
    • 1970-01-01
    相关资源
    最近更新 更多