我在我的应用程序中使用TrueZip将文件/文件夹添加到模板zip然后将zip移动到用户指定的位置。当我从Netbeans运行应用程序或从命令提示符运行.jar时,一切正常。如果用户将zip命名为“test”,则会在指示的位置创建两个文件,test.zip和一个名为“test.zip。{random-number} .tmp”的tmp文件,当应用程序关闭时,tmp文件为除去。
现在,当我使用Java Web Start将我的应用程序部署到Web服务器并运行它时,会再次创建两个文件,但是当应用程序关闭时,tmp文件不会被删除,并且在尝试打开创建的zip时,我得到了消息,“存档格式未知或已损坏”。
我无法弄清楚为什么它可以从netbeans或.jar中正常工作,但不能通过网络启动。
调用正在创建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);
}
}
}
答案 0 :(得分:0)
显然,没有运行关闭挂钩来将更改提交到任何存档文件。只需在适当的时候添加对TVFS.umount()
的通话,例如在buildZip
的最后一个块中。确保在完成归档文件后只调用一次,而不是每次更改都会调用它,因为这会导致二次运行时间。
需要改进的地方:
workingArchive
成为TFile
而非File
,以提高效果。dir.mkdir()
中对importApkFiles()
的来电。在归档文件中,它不是必需的,并且会导致多余的归档条目。