1

我在我的应用程序中使用 TrueZip 将文件/文件夹添加到模板 zip,然后将 zip 移动到用户指定的位置。当我从 Netbeans 运行应用程序或从命令提示符运行 .jar 时,一切都按我的意愿运行。如果用户将 zip 命名为“test”,则将在指定位置创建两个文件,即 test.zip 和一个名为“test.zip.{random-number}.tmp”的 tmp 文件,当应用程序关闭时,tmp 文件是删除。

现在,当我使用 Java Web Start 将我的应用程序部署到 Web 服务器并运行它时,会再次创建两个文件,但是当应用程序关闭时,tmp 文件不会被删除,当我尝试打开创建的 zip 时,我会收到消息,“存档格式未知或已损坏”。

我不知道为什么它可以从 netbeans 或 .jar 正常工作,但不能通过 web 启动。

调用创建 zip 的类的代码:

JFileChooser chooser = new JFileChooser();
    chooser.setAcceptAllFileFilterUsed(false);
    chooser.addChoosableFileFilter(new FileFilter() { 
        @Override
        public boolean accept(File file) {
            if (file.isDirectory()) {
                return true;
            }
            if(file.getName().toLowerCase().endsWith(".zip")) {
                return true;
            }
            return false;
        }
        @Override
        public String getDescription() {
            return "*.zip";
        }
    });

    int rVal = chooser.showSaveDialog(this);

    if (rVal == JFileChooser.APPROVE_OPTION) {
        try {
            //Get savepath and ensure it ends with .zip extension
            String savePath = chooser.getSelectedFile().getCanonicalPath();
            if (!savePath.endsWith(".zip")) {
                savePath = savePath.concat(".zip");
            }

            PackageBuilder build = new PackageBuilder(groups, calibData);
            build.buildZip(savePath);
        } catch (IOException ex) {
            System.out.println(ex);
        }

    }

PackageBuilder 类:

package apkinstallingaromacreator;

import de.schlichtherle.truezip.file.TFile;
import de.schlichtherle.truezip.file.TFileWriter;
import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.FileUtils;

public class PackageBuilder {

    private File workingArchive;
    private AromaConfigBuilder aromaBuilder;
    private UpdaterScriptBuilder updaterBuilder;
    private ApkGroup[] groups;

    public PackageBuilder(ApkGroup[] groups, String calibData) {
        workingArchive = new File(System.getProperty("java.io.tmpdir"), "aromabuild.zip");
        aromaBuilder = new AromaConfigBuilder(groups, calibData);
        updaterBuilder = new UpdaterScriptBuilder(groups);
        this.groups = groups;

        //Get template.zip from jar package and move it to system tmp directory
        URL templateUrl = getClass().getResource("resources/template.zip");
        try {
            FileUtils.copyURLToFile(templateUrl, workingArchive);
        } catch (IOException ex) {
            System.out.println("Failed to copy template zip from resources");
            Logger.getLogger(PackageBuilder.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void importApkFiles() {
        //For each Apk in each ApkGroup add Apk file to zip in system tmp directory
        //If Apk doesn't list a file, create an empty directory for adding manually
        for (int x = 0; x < groups.length; x++) {
            Apk[] apks = groups[x].getApkArray();
            for (int y = 0; y < apks.length; y++) {
                if (apks[y].getApkFileLocation().isEmpty()) {
                    TFile dir = new TFile(workingArchive, "data/" + apks[y].getApkName());
                    dir.mkdir();
                }
                else {
                    TFile src = new TFile(apks[y].getApkFileLocation());
                    TFile dst = new TFile(workingArchive, "data/" + apks[y].getApkName() + 
                            "/" + apks[y].getApkName() + ".apk");
                    try {
                        src.cp_rp(dst);
                    } catch (IOException ex) {
                        Logger.getLogger(PackageBuilder.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            } 
        }
    }

    public void buildZip(String savePath) {
        //populate aroma-config file within template.zip
        File aroma = new TFile(workingArchive, "META-INF/com/google/android/aroma-config");
        try {
            Writer aromaWriter = new TFileWriter(aroma);
            aromaWriter.write(aromaBuilder.buildConfig());
            aromaWriter.close();
        } catch (IOException ex) {
            Logger.getLogger(PackageBuilder.class.getName()).log(Level.SEVERE, null, ex);
        }

        //Populate updater-script file within template.zip
        File updater = new TFile(workingArchive, "META-INF/com/google/android/updater-script");
        try {
            Writer updaterWriter = new TFileWriter(updater);
            updaterWriter.write(updaterBuilder.buildConfig());
            updaterWriter.close();
        } catch (IOException ex) {
            Logger.getLogger(PackageBuilder.class.getName()).log(Level.SEVERE, null, ex);
        }

        importApkFiles();

        //Copy the zip from system tmp directory to user specified location
        TFile src = new TFile(workingArchive);
        TFile dst = new TFile(savePath);

        try {
           src.cp_rp(dst);
        } catch (IOException ex) {
            Logger.getLogger(PackageBuilder.class.getName()).log(Level.SEVERE, null, ex);
        }   
    }
}
4

1 回答 1

0

显然关闭挂钩不会运行以将您的更改提交到任何存档文件。只需在适当的地方添加一个调用TVFS.umount(),例如在 finally 块中buildZip。确保在完成存档文件后只调用一次,而不是每次更改,因为这会导致二次运行时间。

需要改进的地方:

  • 制作workingArchiveaTFile而不仅仅是 aFile来提高性能。
  • dir.mkdir()删除对in的调用importApkFiles()。在存档文件中,它不是必需的,并且会导致多余的存档条目。
于 2012-08-23T05:42:19.140 回答