⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 packcompact.java

📁 Eclipse RCP下编写的工作管理软件代码
💻 JAVA
字号:
package net.sf.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;
import java.util.jar.JarOutputStream;
import java.util.zip.ZipEntry;

public class PackCompact {
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		PackCompact pkger = new PackCompact();
		pkger.debugEnabled = true;
		pkger.jarFile = "mail.jar";
		Set<String> ret=new HashSet<String>();
//		ret.addAll(pkger.parseOutput(new FileInputStream(
//				"d:/zhanglong/temp/mywork.lst")));
//		ret.addAll(pkger.parseOutput(new FileInputStream(
//		"d:/zhanglong/temp/mywork-mail.lst")));
//		ret.addAll(pkger.parseOutput2(new FileInputStream(
//				"d:/zhanglong/temp/mywork2.lst")));
//		ret.addAll(pkger.parseOutput2(new FileInputStream(
//		"d:/zhanglong/temp/mywork3.lst")));
		ret.addAll(pkger.parseOutput(new FileInputStream(
		"d:/zhanglong/temp/mywork-m3.lst")));
		ret.addAll(pkger.parseOutput2(new FileInputStream(
		"d:/zhanglong/temp/mywork-m2.lst")));
		pkger.pkgResources(ret, "d:/zhanglong/temp/" + pkger.jarFile);

	}

	private boolean debugEnabled;

	private String jarFile;

	public Set<String> parseOutput(InputStream in) throws IOException {
		HashSet<String> ret = new HashSet<String>();
		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		String line = reader.readLine();
		while (line != null) {
			if (line.indexOf(jarFile) != -1) {
				if (debugEnabled)
					System.out.println("Matches:" + line);
				int pos1 = line.indexOf("\t");
				int pos2 = line.indexOf("\t", pos1 + 1);
				ret.add(line.substring(0, pos1) + "."
						+ line.substring(pos1 + 1, pos2));
			} else {
				if (debugEnabled)
					System.out.println("UnMatches:" + line);
			}
			line = reader.readLine();
		}
		return ret;
	}

	public Set<String> parseOutput2(InputStream in) throws IOException {
		HashSet<String> ret = new HashSet<String>();
		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		String line = reader.readLine();
		while (line != null) {
			if (line.startsWith("[Loaded") && line.indexOf(jarFile) != -1
					&& line.indexOf(" from ") != -1) {
				if (debugEnabled)
					System.out.println("Matches:" + line);
				ret.add(line.substring(8, line.indexOf(" from ")));
			} else {
				if (debugEnabled)
					System.out.println("UnMatches:" + line);
			}
			line = reader.readLine();
		}
		return ret;
	}

	public void pkgResources(Set<String> res, String fileName)
			throws IOException {
		File f = new File(fileName);
		if (!f.exists()) {
			f.createNewFile();
		}
		byte[] buf = new byte[1024];
		JarOutputStream out = new JarOutputStream(
				new FileOutputStream(fileName));
		for (String s : res) {
			s = s.replace('.', '/') + ".class";
			if (debugEnabled) {
				System.out.println("adding: " + s);
			}
			InputStream in = this.getClass().getClassLoader()
					.getResourceAsStream(s);
			if (in == null)
				continue;
			out.putNextEntry(new ZipEntry(s));
			int w = in.read(buf);
			while (w >= 0) {
				out.write(buf, 0, w);
				w = in.read(buf);
			}
			in.close();
		}
		out.finish();
		out.close();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -