• 一个寻找.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;
    		}
    	}
    }


     

  • 相关阅读:
    好记性不如烂笔头-linux学习笔记2kickstart自动化安装和cacti
    好记性不如烂笔头-linux学习笔记1
    关于TP5中的依赖注入和容器和facade
    vbs 脚本2
    vbs脚本
    Memcache 和 Radis 比较
    MongoDB 索引的使用, 管理 和优化
    mysql大数据高并发处理
    sql处理高并发
    LB 负载均衡的层次结构
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3233688.html
Copyright © 2020-2023  润新知