一个检查ZIP格式增量包的小工具

阅读更多
公司增量太多 为了保证质量 提高效率 开发了一个增量包检查工具
主要功能是搜索ZIP增量包里的所有文件 看是否有相应XML里配置的违规文件和检查.CLASS文件的JDK版本
main:
package main;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 * @author july
 * 
 */
public class Files {

	/**
	 * @return
	 */
	public void getFrame() {
		JFrame frame = new JFrame();
		JPanel pane = new JPanel();
		frame.setLayout(null);
		pane.setLayout(null);

		// 窗口位置调整
		frame.setSize(800, 500);
		int windowWidth = frame.getWidth(); // 获得窗口宽
		int windowHeight = frame.getHeight(); // 获得窗口高
		Toolkit kit = Toolkit.getDefaultToolkit(); // 定义工具包
		Dimension screenSize = kit.getScreenSize(); // 获取屏幕的尺寸
		int screenWidth = screenSize.width / 2; // 获取屏幕的宽
		int screenHeight = screenSize.height / 2; // 获取屏幕的高
		frame.setLocation(screenWidth - windowWidth / 2, screenHeight
				- windowHeight / 2);// 窗口居中

		JLabel lab1 = new JLabel();
		lab1.setText("文件夹路径:");
		lab1.setBounds(20, 5, 80, 40);

		final JLabel lab2 = new JLabel();
		lab2.setText("检查jdk版本:");
		lab2.setBounds(20, 65, 100, 40);

		String strJdk[] = { "请选择", "jdk1.4", "jdk1.5", "jdk1.6" };
		final JComboBox cb = new JComboBox(strJdk);
		cb.setBounds(20, 100, 230, 30);

		final JTextArea inputTextbox = new JTextArea();
		JScrollPane scrollPane;
		scrollPane = new JScrollPane(inputTextbox);
		inputTextbox.setEditable(true);
		scrollPane.setBounds(2, 180, 790, 275);

		final JTextField zhwb = new JTextField();
		zhwb.setBounds(20, 35, 230, 30);
		zhwb.setEditable(true);

		final JRadioButton r3 = new JRadioButton("查找配置项中文件");
		r3.setBounds(300, 100, 140, 30);
		r3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (r3.isSelected()) {
					cb.setVisible(false);
					lab2.setVisible(false);
				} else {
					cb.setVisible(true);
					lab2.setVisible(true);
				}
			}
		});
		;

		JButton button1 = new JButton("搜索");
		button1.setBounds(360, 35, 60, 30);
		button1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				actionButton(zhwb, inputTextbox, cb, r3);
			}
		});
		final Component cmpt = null;

		JButton chooseButton = new JButton("目录...");
		chooseButton.setBounds(250, 35, 70, 30);
		chooseButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser chooser = new JFileChooser();
				chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); // 只显示目录
				chooser.setCurrentDirectory(new File("."));
				int ret = chooser.showOpenDialog(cmpt);
				if (ret == JFileChooser.APPROVE_OPTION) {
					zhwb.setText(chooser.getSelectedFile().getPath());
				}

			}
		});

		pane.add(lab1);
		pane.add(lab2);
		pane.add(r3);
		pane.add(zhwb);
		pane.add(chooseButton);
		pane.add(button1);
		pane.setBounds(0, 0, 600, 180);

		frame.add(cb);
		frame.add(pane);
		frame.add(scrollPane);
		frame.setTitle("增量包检查工具");
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.setVisible(true);
	}

	/**
	 * @param zhwb
	 * @param inputTextbox
	 * @param cb
	 * @param radio
	 * @return
	 * 
	 */
	public void actionButton(JTextField zhwb, JTextArea inputTextbox,
			JComboBox cb, JRadioButton radio) {
		String strMassage = "此文件夹下无相应文件";
		if ("".equals(zhwb.getText())) {
			strMassage = "请选择文件夹";
		} else if (cb.getSelectedItem().toString().equals("请选择")
				&& radio.isSelected() == false) {
			strMassage = "请选择查找类型(JDK版本或者文件)";
		}
		if (!"".equals(zhwb.getText())
				&& (!cb.getSelectedItem().toString().equals("请选择") || radio
						.isSelected() == true)) {
			String str = new FilesService().search(zhwb.getText(), cb
					.getSelectedItem().toString(), radio.isSelected(), cb
					.getSelectedItem().toString());
			if (str.equals("")) {
				if (radio.isSelected() == true) {
					strMassage = "此文件夹下无相应文件";
				} else {
					strMassage = "此文件夹下无相应CLASS文件";
				}
			} else {
				strMassage = str;
			}
		}
		inputTextbox.setText(strMassage);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new Files().getFrame();
	}
}


逻辑处理:
package main;

import java.io.File;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * @author july
 * 
 */
public class FilesService {
	/**
	 * @param filesUrl
	 * @param jdkVersion
	 * @param boolRadio
	 * @param radioText
	 * @return
	 * 
	 */
	public String search(String filesUrl, String jdkVersion, boolean boolRadio,
			String radioText) {
		String str = "此文件夹下无相应文件";
		if (boolRadio) {
			str = this.getFileNameList(filesUrl);
		} else {
			str = this.getSearchJdkResult(jdkVersion, filesUrl, radioText);
		}
		return str;
	}

	/**
	 * 返回要检查的文件集合
	 * 
	 * @param filesUrl
	 * 
	 * @return
	 */
	public String getFileNameList(String filesUrl) {
		StringBuffer strFileresult = new StringBuffer("");
		String strFileUrl = new File("").getAbsolutePath() + "\\FileName.xml";
		SAXReader saxReader = new SAXReader();
		Document document;
		String strReuselt = "";
		try {
			document = saxReader.read(new File(strFileUrl));
			Iterator iter = document.selectNodes("//file-name").iterator();
			while (iter.hasNext()) {
				strReuselt = "";
				Element elm = (Element) iter.next();
				strReuselt = this.getSearchResult(elm.getText(), filesUrl);
				strFileresult.append(strReuselt);
				if (!"".equals(strReuselt)) {
					strFileresult.append("\n");
				}
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return strFileresult.toString();
	}

	/**
	 * 返回查找结果
	 * 
	 * @param fileName
	 * @param filesUrl
	 * @param FilesUrl
	 * @return
	 */
	public String getSearchResult(String fileName, String filesUrl) {
		ZipFile zip = null;
		String strFileResult = "";
		StringBuffer strSearchResult = new StringBuffer("");
		File[] entriesFile = new File(filesUrl).listFiles();
		for (int i = 0; i < entriesFile.length; i++) {
			if (entriesFile[i].getName().indexOf(".zip") > 0
					|| entriesFile[i].getName().indexOf(".ZIP") > 0) {
				try {
					zip = new ZipFile(entriesFile[i]);
					Enumeration entries = zip.entries();
					while (entries.hasMoreElements()) {
						strFileResult = "";
						ZipEntry entry = entries.nextElement();
						String strName = "";
						if (!entry.isDirectory()) {
							if (entry.getName().indexOf("\\") < 0) {
								strName = entry.getName();
							} else {
								strName = entry.getName().substring(
										entry.getName().lastIndexOf("\\") + 1,
										entry.getName().length());
							}
							if (strName.equals(fileName)) {
								strFileResult = fileName + "文件" + "存在于"
										+ entriesFile[i].getName() + "中\n";
							}
						}
						strSearchResult.append(strFileResult);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return strSearchResult.toString();
	}

	/**
	 * 
	 * @param jdkVersion
	 * @param filesUrl
	 * @param radioText
	 * @param fileName
	 * @param FilesUrl
	 * @return
	 */
	public String getSearchJdkResult(String jdkVersion, String filesUrl,
			String radioText) {
		ZipFile zip = null;
		StringBuffer strFileResult = new StringBuffer("");
		File[] entriesFile = new File(filesUrl).listFiles();
		for (int i = 0; i < entriesFile.length; i++) {
			if (entriesFile[i].getName().indexOf(".zip") > 0
					|| entriesFile[i].getName().indexOf(".ZIP") > 0) {
				try {
					zip = new ZipFile(entriesFile[i]);
					Enumeration entries = zip.entries();
					while (entries.hasMoreElements()) {
						ZipEntry entry = entries.nextElement();
						if (!entry.isDirectory()) {
							if (entry.getName().indexOf(".class") > 0) {
								InputStream fis = zip.getInputStream(entry);
								int length = fis.available();
								byte[] data = new byte[length];
								fis.read(data);
								fis.close();
								if (parseFile(data, jdkVersion)) {
									strFileResult.append("使用" + radioText
											+ "编译的CLASS文件存在于:" + zip.getName()
											+ "\n");
									break;
								}
							}
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return strFileResult.toString();
	}

	/**
	 * @param data
	 * @param jdkVersion
	 * @return
	 */
	private static boolean parseFile(byte[] data, String jdkVersion) {
		int minor_version = ((data[4]) << 8) + data[5];
		int major_version = ((data[6]) << 8) + data[7];
		String strVersion = major_version + "." + minor_version;
		if ("46.0".equals(strVersion)) {
			if (jdkVersion.equals("jdk1.4")) {
				return true;
			}
		}
		if ("49.0".equals(strVersion)) {
			if (jdkVersion.equals("jdk1.5")) {
				return true;
			}
		}
		if ("50.0".equals(strVersion)) {
			if (jdkVersion.equals("jdk1.6")) {
				return true;
			}
		}
		return false;
	}
}

  • FilesSearch.rar (479.9 KB)
  • 下载次数: 3
  • dom4j.rar (474 KB)
  • 下载次数: 1

你可能感兴趣的:(Swing,360,JDK,XML)