2012-08-23 39 views
1

我在我的應用程序中使用TrueZip將文件/文件夾添加到模板zip,然後將zip移動到用戶指定的位置。當我從Netbeans運行應用程序或從命令提示符運行.jar時,一切正常。如果用戶將zip命名爲「test」,將在指定的地方創建兩個文件,test.zip和一個名爲「test.zip。{random-number} .tmp」的tmp文件,並且當應用程序關閉時,tmp文件是除去。從netbeans/jar啓動時TrueZip工作正常,但不是javaws

現在,當我使用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); 
     } 
    } 
} 

回答

0

顯然關機鉤不執行提交更改任何檔案文件。只要在適當的地方添加對TVFS.umount()的呼叫,例如,在buildZip的最後一個塊中。確保在完成存檔文件時只調用一次,而不是每次更改,因爲這會導致二次運行時間。

事情,以改善:

  • workingArchive一個TFile,而不僅僅是一個File以提高性能。
  • 刪除dir.mkdir()的電話importApkFiles()。在歸檔文件中,它不是必需的,並且會導致一個多餘的歸檔條目。
+0

'TVFS.umount()'調用做了訣竅。感謝您的幫助和改進提示。 – osafi

+0

我很高興能幫上忙! –

相關問題