//************************************************************* // アーカイブのパスワード保護 // 「ArchivePasswordTest」 // //          作 成 者:ルート高菜    //          作成開始月:2009/11 //          最終更新月:2009/11 [TN122/J112] //************************************************************* import java.awt.BorderLayout; import java.awt.GridLayout; 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 java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipOutputStream; import javax.swing.BorderFactory; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import javax.swing.filechooser.FileNameExtensionFilter; //************************************************************** //*** このクラスにimportされているZIPは、java.util.zipである *** //************************************************************** public class ArchivePasswordTest extends JPanel implements ActionListener,DocumentListener { //Eclipseの場合、シリアライズ可能クラスでこれがないと警告が出る private static final long serialVersionUID=0; //------------------------------------------------------------------ // public static void main(String[] args) { JFrame frame=new JFrame(); frame.getContentPane().add(new ArchivePasswordTest()); frame.setTitle("ArchivePasswordTest"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //右上×印で終了 frame.setSize(500,600); frame.setVisible(true); frame.setLocationRelativeTo(null); //画面の真ん中に表示 } //------------------------------------------------------------------ // // File fileJar; File fileZip; File folder; // JTextField textJar=new JTextField(10); JButton buttonJar=new JButton("選択"); JButton buttonJarToFolder=new JButton("下のフォルダに解凍"); // JTextField textZip=new JTextField(10); JButton buttonZip=new JButton("選択"); JButton buttonZipToFolder=new JButton("下のフォルダに解凍"); // JTextField textPassword=new JTextField(10); JRadioButton radioUtil=new JRadioButton("java.util.zip"); JRadioButton radioHishidama=new JRadioButton("jp.hishidama.zip"); // JTextField textFolder=new JTextField(10); JButton buttonFolder=new JButton("選択"); JButton buttonFolderToJar=new JButton("上のJARに圧縮"); JButton buttonFolderToZip=new JButton("上のZIPに圧縮"); // JTextArea textOutput=new JTextArea(); // ArchivePasswordTestPassZip passZip; //パスワードを使うクラス //------------------------------------------------------------------コンストラクタ // public ArchivePasswordTest(){ // ButtonGroup radioGroup=new ButtonGroup(); radioGroup.add(radioUtil); radioGroup.add(radioHishidama); // fileJar=fileZip=folder=null; // JPanel panelJar=new JPanel(); panelJar.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"JARファイル")); //枠線 panelJar.add(textJar); panelJar.add(buttonJar); panelJar.add(buttonJarToFolder); // JPanel panelZipFile=new JPanel(); panelZipFile.add(textZip); panelZipFile.add(buttonZip); panelZipFile.add(buttonZipToFolder); // JPanel panelZipPassword=new JPanel(); panelZipPassword.add(new JLabel("パスワード:")); panelZipPassword.add(textPassword); panelZipPassword.add(radioUtil); panelZipPassword.add(radioHishidama); // JPanel panelZip=new JPanel(); panelZip.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"ZIPファイル")); //枠線 panelZip.setLayout(new GridLayout(2,1)); panelZip.add(panelZipFile); panelZip.add(panelZipPassword); // JPanel panelFolder=new JPanel(); panelFolder.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"フォルダ")); //枠線 panelFolder.add(textFolder); panelFolder.add(buttonFolder); panelFolder.add(buttonFolderToJar); panelFolder.add(buttonFolderToZip); // JPanel panelInfo=new JPanel(); panelInfo.setLayout(new BorderLayout()); panelInfo.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"情報表示欄")); //枠線 panelInfo.add(new JScrollPane(textOutput),BorderLayout.CENTER); // JPanel panelFunction=new JPanel(); panelFunction.setLayout(new GridLayout(3,1)); panelFunction.add(panelJar); panelFunction.add(panelZip); panelFunction.add(panelFolder); // setLayout(new BorderLayout()); add(panelFunction,BorderLayout.NORTH); add(panelInfo,BorderLayout.CENTER); // buttonJar.addActionListener(this); buttonJarToFolder.addActionListener(this); buttonZip.addActionListener(this); buttonZipToFolder.addActionListener(this); buttonFolder.addActionListener(this); buttonFolderToJar.addActionListener(this); buttonFolderToZip.addActionListener(this); // textPassword.getDocument().addDocumentListener(this); changeRadio(); // passZip=new ArchivePasswordTestPassZip(this); } //------------------------------------------------------------------イベント // public void actionPerformed(ActionEvent e) { Object object=e.getSource(); if(object==buttonJar){ //JAR選択(同時に情報表示) //ダイアログ準備 JFileChooser chooser=new JFileChooser(fileJar); chooser.addChoosableFileFilter(new FileNameExtensionFilter("JARファイル","jar")); chooser.setAcceptAllFileFilterUsed(false); //JARのみ選択可能 // if(chooser.showOpenDialog(this)==JFileChooser.APPROVE_OPTION){ fileJar=chooser.getSelectedFile(); textJar.setText(fileJar.getName()); //情報表示 if(fileJar.exists()){ textOutput.setText(outputJarInfo(fileJar)); //java.util.jarを使う }else{ textOutput.setText("JAR file not found"); } } }else if(object==buttonZip){ //ZIP選択(同時に情報表示) //ダイアログ準備 JFileChooser chooser=new JFileChooser(fileZip); chooser.addChoosableFileFilter(new FileNameExtensionFilter("ZIPファイル","zip")); chooser.setAcceptAllFileFilterUsed(false); //ZIPのみ選択可能 // if(chooser.showOpenDialog(this)==JFileChooser.APPROVE_OPTION){ fileZip=chooser.getSelectedFile(); textZip.setText(fileZip.getName()); //情報表示 if(fileZip.exists()){ if(radioUtil.isSelected()){ textOutput.setText(outputZipInfo(fileZip)); //java.util.zipを使う }else{ textOutput.setText(passZip.outputZipInfo(fileZip)); //jp.hishidama.zipを使う } }else{ textOutput.setText("ZIP 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.getName()); //情報表示 if(folder.exists()){ textOutput.setText(outputFolderInfo(folder)); }else{ textOutput.setText("folder not found"); } } }else if(object==buttonJarToFolder){ //JAR解凍 //元と先をチェック if(fileJar==null){ JOptionPane.showMessageDialog(this,"JARファイルを指定してください","エラー",JOptionPane.ERROR_MESSAGE); return; }else if(!fileJar.exists()){ JOptionPane.showMessageDialog(this,"JARファイルが存在しません","エラー",JOptionPane.ERROR_MESSAGE); return; } if(folder==null){ JOptionPane.showMessageDialog(this,"解凍先フォルダを指定してください","エラー",JOptionPane.ERROR_MESSAGE); return; } folder.mkdirs(); //出力先フォルダが存在しないかもしれないので作成 // try { //java.util.jarを使う JarFile jarFile=new JarFile(fileJar); // Enumeration enm=jarFile.entries(); while(enm.hasMoreElements()){ JarEntry jarEntry=(JarEntry)enm.nextElement(); jarEntryToFile(jarFile,jarEntry); } // jarFile.close(); // JOptionPane.showMessageDialog(this,"解凍しました(by java.util.jar)","完了",JOptionPane.INFORMATION_MESSAGE); } catch (IOException ioe) { JOptionPane.showMessageDialog(this,ioe.toString(),"エラー",JOptionPane.ERROR_MESSAGE); } }else if(object==buttonZipToFolder){ //ZIP解凍 //元と先をチェック if(fileZip==null){ JOptionPane.showMessageDialog(this,"ZIPファイルを指定してください","エラー",JOptionPane.ERROR_MESSAGE); return; }else if(!fileZip.exists()){ JOptionPane.showMessageDialog(this,"ZIPファイルが存在しません","エラー",JOptionPane.ERROR_MESSAGE); return; } if(folder==null){ JOptionPane.showMessageDialog(this,"解凍先フォルダを指定してください","エラー",JOptionPane.ERROR_MESSAGE); return; } folder.mkdirs(); //出力先フォルダが存在しないかもしれないので作成 // if(radioUtil.isSelected()){ //java.util.zipを使う try { ZipFile zipFile=new ZipFile(fileZip); // Enumeration enm=zipFile.entries(); while(enm.hasMoreElements()){ ZipEntry zipEntry=(ZipEntry)enm.nextElement(); zipEntryToFile(zipFile,zipEntry); } // zipFile.close(); // JOptionPane.showMessageDialog(this,"解凍しました(by java.util.zip)","完了",JOptionPane.INFORMATION_MESSAGE); } catch (IOException ioe) { JOptionPane.showMessageDialog(this,ioe.toString(),"エラー",JOptionPane.ERROR_MESSAGE); } }else{ //jp.hishidama.zipを使う passZip.passwordZipToFolder(fileZip, folder, textPassword.getText()); } }else if(object==buttonFolderToJar){ //JAR圧縮 //元と先をチェック if(fileJar==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 { //java.util.jarを使う //マニフェスト作成 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(fileJar)),manifest); // filesToJar(folder,jos); // jos.close(); // JOptionPane.showMessageDialog(this,"圧縮しました(by java.util.jar)","完了",JOptionPane.INFORMATION_MESSAGE); } catch (IOException ioe) { JOptionPane.showMessageDialog(this,ioe.toString(),"エラー",JOptionPane.ERROR_MESSAGE); } }else if(object==buttonFolderToZip){ //ZIP圧縮 //元と先をチェック if(fileZip==null){ JOptionPane.showMessageDialog(this,"ZIPファイルを指定してください","エラー",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; } // if(radioUtil.isSelected()){ //java.util.zipを使う try { //ZIPはマニフェストなし ZipOutputStream zos=new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(fileZip))); // filesToZip(folder,zos); // zos.close(); // JOptionPane.showMessageDialog(this,"圧縮しました(by java.util.zip)","完了",JOptionPane.INFORMATION_MESSAGE); } catch (IOException ioe) { JOptionPane.showMessageDialog(this,ioe.toString(),"エラー",JOptionPane.ERROR_MESSAGE); } }else{ //jp.hishidama.zipを使う passZip.filesToPasswordZip(fileZip, folder, textPassword.getText()); } } } //------------------------------------------------------------------JAR圧縮 //指定されたフォルダ以下にある全ファイルをJARに書き込む(再帰呼び出し) private void filesToJar(File outFolder,JarOutputStream jos) throws IOException{ String[] outFilesList=outFolder.list(); // File outFile; for(int f=0;f