一个寻找.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 ...
随机推荐
- Educational Codeforces Round 6 D. Professor GukiZ and Two Arrays
Professor GukiZ and Two Arrays 题意:两个长度在2000的-1e9~1e9的两个序列a,b(无序);要你最多两次交换元素,使得交换元素后两序列和的差值的绝对值最小:输出这 ...
- Spring实战——无需一行xml配置实现自动化注入
已经想不起来上一次买技术相关的书是什么时候了,一直以来都习惯性的下载一份电子档看看.显然,如果不是基于强烈的需求或强大的动力鞭策下,大部分的书籍也都只是蜻蜓点水,浮光掠影. 就像有位同事说的一样,有些 ...
- Quartz1.8.5例子(十四)
org.quartz.scheduler.instanceName: PriorityExampleScheduler # Set thread count to 1 to force Trigger ...
- 根据WSDL生成代理类方式(2)
运行开发人员工具提示 输入命令行svcutil http://localhost:8080/Test/TestClassPort?wsdl
- Bootstrap 分页插件 ajax获取数据显示
Bootstrap 分页插件 ajax获取数据显示 标签(空格分隔): bootstrap 文章的内容是使用bootstrap-paginator进行分页,使用ajax获取后台数据.渲染. 1. 版本 ...
- Server-Side Access Control
Firefox 3.5 implements the W3C Access Control specification. As a result, Firefox 3.5 sends specifi ...
- 裸眼3D立体显示技术原理详解
众所周知,现实世界是一个三维空间,除去时间这一维度,现实世界是由长度.宽度和高度三个维度组成,我们每天就生活在这个三维世界中,而现有的显示设备大多数都只能显示二维信息,并不能带给人真实的三维感觉.为了 ...
- 【Xamarin挖墙脚系列:卸载不彻底的解决】
原文:[Xamarin挖墙脚系列:卸载不彻底的解决] 卸载后,再次安装,总是授权还是原来的.请手工删除下文件: 卸载程序后 必须手工删除C:\ProgramData\Mono for Android\ ...
- 全球AI界最值得关注的十位科学家
全球AI界最值得关注的十位科学家 我们可以看到AI已经从象牙塔里的高冷研究,逐步转换为科技公司.互联网公司的最核心竞争力.AI代表了这时代人类的前沿智慧,也正达到一种科学的极致. 这两天在美国加利 ...
- op论坛,分支
http://www.arm9home.net/thread.php?fid=68 http://www.openwrt.org.cn/bbs/forum.php https://dev.openwr ...