【问题标题】:Copy folder into jar将文件夹复制到jar中
【发布时间】:2013-08-19 12:46:03
【问题描述】:

我正在制作一个修改安装程序,一切正常,但我不知道如何将文件夹复制到 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();
      }
}
}

【问题讨论】:

    标签: java jar copy directory


    【解决方案1】:

    简短的回答是,你不能……

    答案很长,你可以,但这并不容易。

    基本上,您需要做的是创建一个新的JarFile,将第一个(原始)Jar 中的内容复制到其中,而不关闭或完成新的JarFile,将您的新内容添加到其中。

    完成后(并关闭/完成新的JarFile),删除旧的并将新的移动到原位

    以下示例可能会被证明是有用的......

    请记住,Jar 文件只是一个带有一些附加功能的 Zip ;)

    【讨论】:

    • 所以我必须将第一个 jars 的内容复制到一个文件夹中,然后添加我的内容并将文件夹变成一个 jar。好的,谢谢
    • 您可以一次滑动完成所有操作 - 即,无需提取第一个 Jar、复制、重新 jar...只需在一个进程中解压缩和 Jar...
    • 是的,JarFile,围绕它编写了一个完整的 API。基本上,对于原件中的每个JarEntry,您都希望在目标中创建一个精确的副本,然后将JarInputSteam 从原件(条目)传送到目标的(JarEntry)JarOutputStream。完成后,您只需将文件夹添加到其中...以类似的方式...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-22
    • 1970-01-01
    • 1970-01-01
    • 2016-07-14
    • 2018-01-04
    • 2014-07-24
    相关资源
    最近更新 更多