//************************************************************* // Java application // 「JarOperation」 // //          作 成 者:ルート高菜    //          作成開始月:2008/2 //          最終更新月:2008/2 [TN119/J109] //************************************************************* import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Enumeration; import java.util.Iterator; import java.util.jar.Attributes; import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.jar.JarOutputStream; import java.util.jar.Manifest; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.filechooser.FileFilter; import javax.swing.filechooser.FileNameExtensionFilter; public class JarOperation extends JPanel implements ActionListener { //Eclipseの場合、シリアライズ可能クラスでこれがないと警告が出る private static final long serialVersionUID=0; public static void main(String[] args) { JFrame frame=new JFrame(); frame.getContentPane().add(new JarOperation()); frame.setTitle("JarOperation"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //右上×印で終了 frame.setSize(700,400); frame.setVisible(true); frame.setLocationRelativeTo(null); //画面の真ん中に表示 } //------------------------------------------------------------------ // File file; //解凍元・圧縮先のJARファイル File folder; //解凍先・圧縮元のフォルダ // JTextField textJar=new JTextField(30); JButton buttonJar=new JButton("JAR選択"); JButton buttonJarToFolder=new JButton("内容を下のフォルダに解凍"); JTextField textFolder=new JTextField(30); JButton buttonFolder=new JButton("フォルダ選択"); JButton buttonFolderToJar=new JButton("内容を上のJARに圧縮"); JTextArea textOutput=new JTextArea(); //------------------------------------------------------------------ // public JarOperation(){ // file=folder=null; // JPanel panelJar=new JPanel(); panelJar.add(textJar); panelJar.add(buttonJar); panelJar.add(buttonJarToFolder); // JPanel panelFolder=new JPanel(); panelFolder.add(textFolder); panelFolder.add(buttonFolder); panelFolder.add(buttonFolderToJar); // setLayout(new BorderLayout()); add(panelJar,BorderLayout.NORTH); add(new JScrollPane(textOutput),BorderLayout.CENTER); add(panelFolder,BorderLayout.SOUTH); // buttonJar.addActionListener(this); buttonJarToFolder.addActionListener(this); buttonFolder.addActionListener(this); buttonFolderToJar.addActionListener(this); } //------------------------------------------------------------------ // public void actionPerformed(ActionEvent e) { Object object=e.getSource(); if(object==buttonJar){ //JAR選択(同時に情報表示) //JARファイル選択ダイアログ JFileChooser chooser=new JFileChooser(file); FileFilter filter=new FileNameExtensionFilter("JARファイル","jar"); chooser.addChoosableFileFilter(filter); chooser.setAcceptAllFileFilterUsed(false); //基本の「すべてを表示」フィルターを表示しない // if(chooser.showOpenDialog(this)==JFileChooser.APPROVE_OPTION){ file=chooser.getSelectedFile(); textJar.setText(file.toString()); // if(file.exists()){ try { JarFile jarFile = new JarFile(file); textOutput.setText(outputJarInfo(jarFile)); } catch (IOException ioe) { textOutput.setText(ioe.toString()); } }else{ textOutput.setText("file not found"); } } }else if(object==buttonFolder){ //フォルダ選択 //フォルダ選択ダイアログ JFileChooser chooser=new JFileChooser(folder); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); // if(chooser.showSaveDialog(this)==JFileChooser.APPROVE_OPTION){ folder=chooser.getSelectedFile(); textFolder.setText(folder.toString()); // } }else if(object==buttonJarToFolder){ //解凍 if(file==null){ JOptionPane.showMessageDialog(this,"JARファイルを指定してください","エラー",JOptionPane.ERROR_MESSAGE); return; }else if(!file.exists()){ JOptionPane.showMessageDialog(this,"JARファイルが存在しません","エラー",JOptionPane.ERROR_MESSAGE); return; } // if(folder==null){ JOptionPane.showMessageDialog(this,"解凍先フォルダを指定してください","エラー",JOptionPane.ERROR_MESSAGE); return; } folder.mkdirs(); //出力先フォルダが存在しないかもしれないので作成 // try { JarFile jarFile=new JarFile(file); // Enumeration enm=jarFile.entries(); while(enm.hasMoreElements()){ JarEntry jarEntry=(JarEntry)enm.nextElement(); entryToFile(jarFile,jarEntry); } // JOptionPane.showMessageDialog(this,"解凍しました","完了",JOptionPane.INFORMATION_MESSAGE); } catch (IOException ioe) { JOptionPane.showMessageDialog(this,ioe.toString(),"エラー",JOptionPane.ERROR_MESSAGE); } }else if(object==buttonFolderToJar){ //圧縮 if(file==null){ JOptionPane.showMessageDialog(this,"JARファイルを指定してください","エラー",JOptionPane.ERROR_MESSAGE); return; } // if(folder==null){ JOptionPane.showMessageDialog(this,"圧縮元フォルダを指定してください","エラー",JOptionPane.ERROR_MESSAGE); return; }else if(!folder.exists()){ JOptionPane.showMessageDialog(this,"圧縮元フォルダが存在しません","エラー",JOptionPane.ERROR_MESSAGE); return; } // try { //マニフェスト作成 Manifest manifest=new Manifest(); Attributes attributes=manifest.getMainAttributes(); attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0"); //そのマニフェストでJAR JarOutputStream jos=new JarOutputStream(new BufferedOutputStream(new FileOutputStream(file)),manifest); // filesToJar(folder,jos); // jos.close(); // JOptionPane.showMessageDialog(this,"圧縮しました","完了",JOptionPane.INFORMATION_MESSAGE); } catch (IOException ioe) { JOptionPane.showMessageDialog(this,ioe.toString(),"エラー",JOptionPane.ERROR_MESSAGE); } } } //------------------------------------------------------------------圧縮 //指定されたフォルダ以下にある全ファイルをJARに書き込む(再帰呼び出し) private void filesToJar(File outFolder,JarOutputStream jos) throws IOException{ String[] outFilesList=outFolder.list(); // File outFile; for(int f=0;f