2013-08-17 103 views
0

我做了一個修改安裝程序,一切正常,但我無法弄清楚如何將一個文件夾複製到JarFile中,該文件夾需要進入jar中的文件夾。這是我失敗的嘗試。 :(複製文件夾到罐子

我的代碼:

public class DisplayContent extends JPanel implements ActionListener{ 

    public DisplayContent() { 
     handleGraphics(); 
    } 
    public String chosenDir = null; 
    private void handleGraphics() { 
     setLayout(null); 
     JLabel paneTitle = new JLabel(); 
     paneTitle.setFont(new Font("Comic Sans MS", Font.BOLD, 20)); 
     paneTitle.setBounds(55, 6, 330, 62); 
     paneTitle.setBackground(new Color(237, 237, 237)); 
     paneTitle.setText("The Halo Ultimate Mod Installer"); 
     add(paneTitle); 

     JButton btnInstall = new JButton("Click Me To Install The Halo Ultimate Mod"); 
     btnInstall.setBounds(16, 139, 414, 47); 
     btnInstall.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       install(chosenDir); 
      } 
     }); 
     add(btnInstall); 

     final JButton forgeFolder = new JButton("Select Forge Jar File! Important!"); 
     forgeFolder.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       chosenDir = getSelectedDir(forgeFolder); 
      } 
     }); 
     forgeFolder.setBounds(16, 80, 414, 47); 
     add(forgeFolder); 
    } 

    public void install(String dir) { 
     String jar = dir + "/assets/"; 
     String absolutePath = new File("").getAbsolutePath(); 
     String halo = absolutePath + "/mods"; 

     try { 
      copyFile(halo, jar); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     System.out.print(" --- NEW DIR2 --- " + absolutePath); 
     System.out.print(" --- NEW DIR --- " + chosenDir); 
    } 

    public String getSelectedDir(Component parent){ 
     JFileChooser fc = new JFileChooser(); 
     fc.setFileSelectionMode(JFileChooser.FILES_ONLY); 

     if(fc.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) 
     { 
      return fc.getSelectedFile().getAbsolutePath(); 
     } 

     return null; 
    } 

    public void actionPerformed(ActionEvent e) {} 

    public void copyFile(String filenameoriginal, String filenamecopy)throws IOException{ 
     File destFile = new File(filenamecopy); 
     if (!destFile.exists()) { 
      destFile.createNewFile(); 
     } 

     FileChannel fileSource = new FileInputStream(filenameoriginal).getChannel(); 
     FileChannel destination = new FileOutputStream(filenamecopy).getChannel(); 
     destination.transferFrom(fileSource, 0, fileSource.size()); 

     System.out.println("Success."); 
     if (fileSource != null) { 
      fileSource.close(); 
     } 
     if (destination != null) { 
      destination.close(); 
     } 
} 
} 

回答

0

簡短的回答是,你不能......

長的答案是,你可以,但它並不容易

基本上。 ,你需要做的是創建一個新的JarFile,從第一個(原始)Jar的內容複製到它,而不關閉或完成新的JarFile,添加新的內容。

一旦你完成(並已關閉/敲定新JarFile),刪除舊的和移動的新到原來的位置

下面的例子可以證明是有用的......

請記住,Jar文件只是一些附加的Zip文件;)

+0

所以我必須將第一個jar內容複製到一個文件夾中,然後添加我的內容並將文件夾轉換爲jar。好的,謝謝 – marko5049

+0

你可以在一次掃描中完成所有工作 - 即不需要提取第一個罐子,複製,重新罐子...只需在一個進程中取消Jar和Jar ... – MadProgrammer

+0

你知道任何函數嗎?只是嘗試我的運氣。 :)謝謝sooo多!!! – marko5049

相關問題