Eclipse + Visual Editor + ZIP ファイル内エントリ一覧(JTable)と解凍

  ダウンロードとセットアップ



Apache Ant - Binary Distributions から apache-ant-1.7.1-bin.zip をダウンロードします

※ ドキュメントは ZipFile (Apache Ant API) からオンラインで参照できます


プロジェクトを作成して、ant.jar を「外部 JAR の追加」で追加します




  アプリケーション作成の準備



ビジュアルクラスをデフォルトパッケージとして作成

 ※ Main.java
 ※ JFrame
 ※ public static void main あり


LboxTableクラスを実装

 ※ バッケージを追加 ( llightbox )
 ※ lightbox パッケージに LboxTable クラスを追加
 ※ LboxTable extends JTable よりソースコードを貼り付け


画面作成

 ※ JPanel のレイアウトを null に変更
 ※ 上記 JPanel の中に JButton と JPanel を配置
 ※ 上記 JPanel の中に LboxTable を配置( Bean の選択より )
 ※ private LboxTable getLboxTable を変更


  
private JScrollPane getLboxTable() {
	if (lboxTable == null) {
		lboxTable = new LboxTable();
		
		lboxTable.AddColumn("COLUMN_1");
		lboxTable.AddColumn("COLUMN_2");

		lboxTable.SetColumnTitle("COLUMN_1", "書庫内のサイズ");
		lboxTable.SetColumnTitle("COLUMN_2", "パス");

		lboxTable.SetColumnWidth("COLUMN_1", 150);
		lboxTable.SetColumnWidth("COLUMN_2", 400);
	}
	return lboxTable.root;
}
  


Javazipfile


ボタンの処理用のクラス作成

 ※ デフォルトパッケージに、ButtonAction クラスを追加


  
public class ButtonAction {

	//*************************************************
	// ZipFileの処理
	//*************************************************
	public void listZipFile(Object param[]) {
		
		System.out.println("ZipFileの処理");

	}

}
  



  ボタンイベントの実装と ButtonAction の呼び出しと ZipFile の一覧

Zipfilelist


ボタンイベントは、Visual Editor より追加して、actionPerformed が自動的に作成されます。
そこから、ButtonAction のインスタンスを呼び出して、処理を行います。

※ ソースコードを簡潔にする為、zip 書庫のパスは固定にしています

  
private ButtonAction buttonAction = new ButtonAction();  //  @jve:decl-index=0:

/**
 * This method initializes jButton	
 * 	
 * @return javax.swing.JButton	
 */
private JButton getJButton() {
	if (jButton == null) {
		jButton = new JButton();
		jButton.setBounds(new Rectangle(30, 19, 158, 28));
		jButton.setText("実行");
		jButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				
				Object param[] = {lboxTable,e};
				buttonAction.listZipFile(param);
				
			}
		});
	}
	return jButton;
}
  

  
import java.awt.event.ActionEvent;
import java.util.Iterator;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;

import lightbox.LboxTable;

public class ButtonAction {

	//*************************************************
	// ZipFileの処理
	//*************************************************
	public void listZipFile(Object param[]) {

		LboxTable lboxTable = (LboxTable)param[0];
		lboxTable.Clear();
		
		ActionEvent e = (ActionEvent)param[1];

		System.out.println("ZipFileの処理");
		System.out.println(e.getActionCommand());

		String targetZipFile = "C:\\TMP\\apache-ant-1.7.1-bin.zip";
		
		try {
			ZipFile zf = new ZipFile(targetZipFile,"shift_jis");

			Iterator<?> myIterator = (Iterator<?>)zf.getEntries();
			while( myIterator.hasNext() ) {
				ZipEntry ze = (ZipEntry)myIterator.next();
				
				if( !ze.isDirectory() ) {

					int row = lboxTable.AddRow();
					lboxTable.SetColumnText(
						row,
						"COLUMN_1",
						ze.getCompressedSize()+""
					);
					lboxTable.SetColumnText(
						row,
						"COLUMN_2",
						ze.getName()
					);
				}
			}
			zf.close();
		}
		catch ( Exception ex ) {
			ex.printStackTrace();
		}

	}

}
  



  ダブルクリックした行のファイルを解凍する

イベントは、Visual Editor から追加できます。( mouseClicked を選択 )

  
private JScrollPane getLboxTable() {
	if (lboxTable == null) {
		lboxTable = new LboxTable();
		lboxTable.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent e) {
				// ダブルクリックの処理
				if ( e.getClickCount() == 2 ) {
					Object param[] = {lboxTable,e};
					buttonAction.meltZipFile(param);
				}
			}
		});
		
		lboxTable.AddColumn("COLUMN_1");
		lboxTable.AddColumn("COLUMN_2");

		lboxTable.SetColumnTitle("COLUMN_1", "書庫内のサイズ");
		lboxTable.SetColumnTitle("COLUMN_2", "パス");

		lboxTable.SetColumnWidth("COLUMN_1", 150);
		lboxTable.SetColumnWidth("COLUMN_2", 400);
	}
	return lboxTable.root;
}
  

  
//*************************************************
// ZipFileの解凍( ひとつのみ )
//*************************************************
public void meltZipFile(Object param[]) {

	LboxTable lboxTable = (LboxTable)param[0];
	MouseEvent e = (MouseEvent)param[1];

	System.out.println("ZipFileの解凍");

	String targetZipFile = "C:\\TMP\\apache-ant-1.7.1-bin.zip";
	// 行の位置
	int row = lboxTable.convertRowIndexToModel(
				lboxTable.rowAtPoint(e.getPoint())
			);
	// カラムの位置 ( 今回は使わない )
	int col = lboxTable.convertColumnIndexToModel(
				lboxTable.columnAtPoint(e.getPoint())
			);
	String targetFile = (String)lboxTable.GetColumnText(row, "COLUMN_2");
	
	try {
		ZipFile zf = new ZipFile(targetZipFile,"shift_jis");
		ZipEntry ze = zf.getEntry(targetFile);
		
		if( !ze.isDirectory() ) {
			// 入力ZIP内エントリ
			InputStream inZipEntry = zf.getInputStream(ze);
			// 出力用
			File realFile = new File( new File("C:\\TMP"), ze.getName());
			realFile.getParentFile().mkdirs();
			FileOutputStream out = new FileOutputStream(realFile);
			// 出力
			byte[] buf = new byte[4096];
			int size = 0;
			while( (size = inZipEntry.read(buf)) != -1 ) {
				out.write(buf, 0, size);
			}
			out.close();
			inZipEntry.close();
		}
		zf.close();
	}
	catch ( Exception ex ) {
		ex.printStackTrace();
	}

}
  



関連する記事
Java : WEB 上の XML を JTable に表示











  infoboard   管理者用   
このエントリーをはてなブックマークに追加





フリーフォントWEBサービス
SQLの窓WEBサービス

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ