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 に表示





yahoo  google  MSDN  MSDN(us)  WinFAQ  Win Howto  tohoho  ie_DHTML  vector  wdic  辞書  天気 


[javaSwing]
CCBot/2.0 (https://commoncrawl.org/faq/)
23/12/09 07:34:05
InfoBoard Version 1.00 : Language=Perl

1 BatchHelper COMprog CommonSpec Cprog CprogBase CprogSAMPLE CprogSTD CprogSTD2 CprogWinsock Cygwin GameScript HTML HTMLcss InstallShield InstallShieldFunc JScript JScriptSAMPLE Jsfuncs LLINK OldProg OracleGold OracleSilver PRO PRObrowser PROc PROconePOINT PROcontrol PROftpclient PROjscript PROmailer PROperl PROperlCHAT PROphp PROphpLesson PROphpLesson2 PROphpLesson3 PROphpfunction PROphpfunctionArray PROphpfunctionMisc PROphpfunctionString PROsql PROvb PROvbFunction PROvbString PROvbdbmtn PROvbonepoint PROwebapp PROwin1POINT PROwinSYSTEM PROwinYOROZU PROwindows ProjectBoard RealPHP ScriptAPP ScriptMaster VBRealtime Vsfuncs a1root access accreq adsi ajax amazon argus asp aspSample aspVarious aspdotnet aw2kinst cappvariety centura ckeyword classStyle cmaterial cmbin cmdbapp cmenum cmlang cmlistbox cmstd cmstdseed cmtxt cs daz3d db dbCommon dbaccess dnettool dos download flex2 flex3 flex4 framemtn framereq freeWorld freesoft gimp ginpro giodownload google hdml home hta htmlDom ie9svg install java javaSwing javascript jetsql jquery jsp jspTest jspVarious lightbox listasp listmsapi listmsie listmsiis listmsnt listmspatch listmsscript listmsvb listmsvc memo ms msde mysql netbeans oraPlsql oracle oracleWiper oraclehelper orafunc other panoramio pear perl personal pgdojo pgdojo_cal pgdojo_holiday pgdojo_idx pgdojo_ref pgdojo_req php phpVarious phpguide plsql postgres ps r205 realC realwebapp regex rgaki ruby rule sboard sc scprint scquest sdb sdbquest seesaa setup sh_Imagick sh_canvas sh_dotnet sh_google sh_tool sh_web shadowbox shgm shjquery shvbs shweb sjscript skadai skywalker smalltech sperl sqlq src systemdoc tcpip tegaki three toolbox twitter typeface usb useXML vb vbdb vbsfunc vbsguide vbsrc vpc wcsignup webanymind webappgen webclass webparts webtool webwsh win8 winofsql wmi work wp youtube