一个寻找.jar 和.zip文件中class文件的工具
相信很多人跟我一样,苦于在各种包之间,不知道Class存在什么地方,为此,自己写了一个小工具,来寻找目录下的Class文件
支持 目录查询,支持带包路径查询
入口Entrance.java
package com.freud.looking; import java.io.IOException; /**
* Entrance which contains Main Method
*
* @author Freud
*
*/
public class Entrance { public static void main(String[] args) throws IOException { UIFrame entrance = new UIFrame();
entrance.initThePanel();
entrance.repaint();
}
}
Logic.java
package com.freud.looking; import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile; /**
* 1. Get all the ended tails file 2. Validate the jar files which contains the
* condition classes 3. Validate the zip files which contains the condition
* classes
*
* @author Freud
*
*/
public class Logic { /**
* Traversal all the file to get end with tail's file
*
* @param file
* Files
* @param tail
* End tail
* @return Matched files
*/
public List<File> traversalFiles(File file, String tail) { List<File> files = new ArrayList<File>(); if (file.isDirectory()) {
for (File descendant : file.listFiles()) { for (File define : traversalFiles(descendant, tail)) { if (define.getName().endsWith(tail)) {
files.add(define);
} }
}
} else { if (file.getName().endsWith(tail)) {
files.add(file);
} } return files; } /**
* Validate the jar files for the condition classes
*
* @param files
* Jar file
* @param condition
* Needed classes
* @return Validated files
* @throws IOException
*/
public Set<String> validateJarFile(List<File> files, String condition) throws IOException { Set<String> flag = new HashSet<String>(); for (File file : files) { try {
JarFile jarFile = null;
try {
jarFile = new JarFile(file); Enumeration<JarEntry> jarentrys = jarFile.entries(); while (jarentrys.hasMoreElements()) { JarEntry jarEntry = (JarEntry) jarentrys.nextElement(); String name = jarEntry.getName().replace("/", ".").replace("\\", "."); if (name.contains(condition)) {
flag.add(jarFile.getName());
} if (name.contains(condition.replace("/", "."))) {
flag.add(jarFile.getName());
} if (name.contains(condition.replace("\\", "."))) {
flag.add(jarFile.getName());
} } } finally {
if (jarFile != null)
jarFile.close();
}
} catch (Exception e) {
System.out.println("Error Occured in File - " + file.getAbsolutePath());
}
}
return flag;
} /**
* Validate the zip files for the condition classes
*
* @param files
* Zip file
* @param condition
* Needed classes
* @return Validated files
* @throws IOException
*/
public Set<String> validateZipFile(List<File> files, String condition) throws IOException { Set<String> flag = new HashSet<String>(); for (File file : files) {
try {
ZipFile zipFile = null;
try {
zipFile = new ZipFile(file); @SuppressWarnings("unchecked")
Enumeration<ZipEntry> zipentrys = (Enumeration<ZipEntry>) zipFile.entries(); while (zipentrys.hasMoreElements()) { ZipEntry zipEntry = (ZipEntry) zipentrys.nextElement(); String name = zipEntry.getName().replace("/", ".").replace("\\", "."); if (name.contains(condition)) {
flag.add(zipFile.getName());
} if (name.contains(condition.replace("/", "."))) {
flag.add(zipFile.getName());
} if (name.contains(condition.replace("\\", "."))) {
flag.add(zipFile.getName());
}
}
} finally {
if (zipFile != null) {
zipFile.close();
}
} } catch (Exception e) {
System.out.println("Error Occured in File - " + file.getAbsolutePath());
}
}
return flag;
}
}
UIFrame.java
package com.freud.looking; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.List; import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField; /**
* Show the main component and do Submit logic
*
* @author Freud
*
*/
public class UIFrame extends JFrame { /**
* serialVersionUID
*/
private static final long serialVersionUID = 1L; /**
* Components For the main frame
*/
private JLabel pathLable;
private JTextField pathField;
private JLabel conditionLable;
private JTextField conditionField;
private JLabel tailLabel;
private JCheckBox jarCheckBox;
private JCheckBox zipCheckBox;
private JLabel resultLabel;
private JTextField resultField;
private JButton submit; /**
* Constructor
*/
public UIFrame() { /**
* Main Frame initialization
*/
this.setTitle("Looking for Classes!");
this.setVisible(true);
this.setBounds(100, 100, 400, 300);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(null); } /**
* Initialization the main panel's components
*
* @return status
*/
public boolean initThePanel() { try { /**
* Location defined
*/
pathLable = new JLabel("Path Of the Location!");
pathLable.setBounds(10, 10, 350, 20); pathField = new JTextField();
pathField.setBounds(10, 40, 350, 30); tailLabel = new JLabel("Tails");
tailLabel.setBounds(10, 80, 350, 20); jarCheckBox = new JCheckBox(".jar", true);
jarCheckBox.setBounds(10, 110, 50, 30); zipCheckBox = new JCheckBox(".zip");
zipCheckBox.setBounds(60, 110, 80, 30); conditionLable = new JLabel("Condition");
conditionLable.setBounds(170, 80, 100, 20); conditionField = new JTextField();
conditionField.setBounds(170, 110, 150, 30); resultLabel = new JLabel("Result Show --");
resultLabel.setBounds(10, 150, 350, 20); resultField = new JTextField();
resultField.setBounds(10, 180, 350, 30); submit = new JButton("Submit");
submit.setBounds(120, 220, 100, 30);
submit.addActionListener(new ActionListener() { /**
* Submit operations
*/
@Override
public void actionPerformed(ActionEvent e) { try {
String tail = "";
String condition = conditionField.getText(); Logic logic = new Logic(); StringBuffer sb = new StringBuffer(); /**
* Check the jar file box
*/
if (jarCheckBox.isSelected()) { tail = ".jar"; /**
* Get all files ended with ".jar"
*/
List<File> files = logic.traversalFiles(new File(pathField.getText()), tail); int i = 0; /**
* Validate the files which have the condition
* classes
*/
for (String item : logic.validateJarFile(files, condition)) {
if (i == 0) {
sb.append(item);
} else {
sb.append(";").append(item);
} i++; }
} /**
* Check the zip file box
*/
if (zipCheckBox.isSelected()) { tail = ".zip"; /**
* Get all files ended with ".zip"
*/
List<File> files = logic.traversalFiles(new File(pathField.getText()), tail); /**
* Validate the files which have the condition
* classes
*/
for (String item : logic.validateZipFile(files, condition)) {
if (sb.toString().equals("")) {
sb.append(item);
} else {
sb.append(";").append(item);
}
}
} /**
* If no files contains
*/
if (sb.toString().equals("")) {
sb.append("No matched file find!");
} /**
* Set result field
*/
resultField.setText(sb.toString()); } catch (Exception e1) {
/**
* Error handling
*/
resultField.setText("Error Occured : " + e1.getMessage());
} }
}); this.add(pathLable);
this.add(pathField);
this.add(conditionLable);
this.add(conditionField);
this.add(tailLabel);
this.add(jarCheckBox);
this.add(zipCheckBox);
this.add(resultLabel);
this.add(resultField);
this.add(submit); this.repaint(); return true; } catch (Exception e) {
return false;
}
}
}
一个寻找.jar 和.zip文件中class文件的工具的更多相关文章
- jar命令+7z:创建,替换,修改,删除Jar, war, ear包中的文件
虽然现在已经有各种智能的IDE可以为我们生成jar包,war包,ear包,甚至带上了自动替换,部署的功能.但一定会有那么些时候,你需要修改或是替换jar包,war包,ear包中的某个文件而不是整个重新 ...
- 文件_ _android从资源文件中读取文件流并显示的方法
======== 1 android从资源文件中读取文件流并显示的方法. 在android中,假如有的文本文件,比如TXT放在raw下,要直接读取出来,放到屏幕中显示,可以这样: private ...
- pip freeze > requirements.txt` 命令输出文件中出现文件路径而非版本号
pip freeze > requirements.txt 命令输出文件中出现文件路径而非版本号 解决办法: pip list --format=freeze > requirements ...
- 推荐一个SAM文件或者bam文件中flag含义解释工具
SAM是Sequence Alignment/Map 的缩写.像bwa等软件序列比对结果都会输出这样的文件.samtools网站上有专门的文档介绍SAM文件.具体地址:http://samtools. ...
- 推荐一个SAM文件中flag含义解释工具--转载
SAM是Sequence Alignment/Map 的缩写.像bwa等软件序列比对结果都会输出这样的文件.samtools网站上有专门的文档介绍SAM文件.具体地址:http://samtools. ...
- VS2010在C#头文件中添加文件注释的方法
步骤: 1.VS2010 中找到安装盘符(本人安装目录在D盘,所以以D盘为例)D:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\I ...
- VS2010在C#头文件中添加文件注释的方法(转)
步骤: 1.VS2010 中找到(安装盘符以D盘为例)D:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTempl ...
- 31、SAM文件中flag含义解释工具--转载
转载:http://www.cnblogs.com/nkwy2012/p/6362996.html SAM是Sequence Alignment/Map 的缩写.像bwa等软件序列比对结果都会输出这 ...
- python移动多个子文件中的文件到一个文件夹
import os import os.path import shutil def listDir(dirTemp): if None == dirTemp: return global nameL ...
随机推荐
- 4种方法生成二维码 (js 控制canvas 画出 二维码)
随着网络的迅速发展 发展 发展,二维码的应用将会越来越多.同时很多只是很平凡的二维码,请拿起你的手 把这个二维码 设计起来吧.下面分享了几个非常好的二维码设计. 二维码原理: 二维条码/二维码可以分 ...
- 开源搜索引擎Sphinx 中启动多个搜索进程的方法
http://blog.163.com/yang_jianli/blog/static/1619900062010316504471/ 要在同一机器上启动多个sphinx搜索进程searchd,必须为 ...
- uboot start.S分析
一.概述 1.本文综述及特色 阅读uboot,start.S是第一个源程序文件,主要完成初始化看门狗.定时器.重定位(拷贝代码段到内存中).初始化堆栈.跳转到第二阶段等工作. 网上关于这些内容的 ...
- 第 17 章 责任链模式【Chain of Responsibility Pattern】
以下内容出自:<<24种设计模式介绍与6大设计原则>> 中国古代对妇女制定了“三从四德”的道德规范,“三从”是指“未嫁从父.既嫁从夫.夫死从子”,也就是说一个女性,在没有结婚的 ...
- poi 操作excel
poi操作 创建一个excel关联对象HSSFWorkbook: HSSFWorkbook book = new HSSFWorkbook(); 创建一个sheet: HSSFSheet st = b ...
- 忽然想到,为什么以前iOS的工资高
听说做iOS的在上海工资也可以 前几年还行,现在也不太行了除非你水平很高 现在移动应用已经100多万了,基本饱和了以前是新的商业模式要抢时间,会的人也少
- 【转】Xcode7.1环境下上架iOS App到AppStore 流程 (Part 三)
原文网址:http://www.cnblogs.com/ChinaKingKong/p/4964745.html 前言部分 part三 部分主要讲解 Xcode关联绑定发布证书的配置.创建App信息. ...
- C# Get Desktop Screenshot ZZ
I found this feature while looking inside Graphics class and since it was so simple to use, I decide ...
- xamarin for vs2013
安装需求(下载的包及版本) 先安装VS2013 然后到官网下Xamarin,运行后会自动下载以下文件 这是下载的详细列表 jdk-6u39-windows-i586.exe(69.73M) Andro ...
- webbrowser控件事件
Beforenavigate2: Fired before navigate occurs in the given WebBrowser(window or frameset element). T ...