打包你需要的基本类、其他的不要

看了一篇瘦身文件、记录一下、http://nonconductor.bokee.com/5085956.html

源代码如下

/*
* Alipay.com Inc.
* Copyright (c) 2004-2008 All Rights Reserved.
*/
package com.beyond.web.trade;

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.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.jar.JarOutputStream;
import java.util.zip.ZipEntry;

public class Packager {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
File f = new File("C:/Documents and Settings/en.xuze/hello.list");
Packager pkger = new Packager();
pkger.debugEnabled = true;
List ret = pkger.parseOutput(new FileInputStream(f));
pkger.pkgResources(ret, "C:/Documents and Settings/en.xuze/Hello.jar");
}

private boolean debugEnabled;

public List parseOutput(InputStream in) throws IOException {
ArrayList ret = new ArrayList();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = reader.readLine();
while (line != null) {
if (line.matches("\\[Loaded .* from .*")) {
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(List 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 (Iterator iterator = res.iterator(); iterator.hasNext();) {
InputStream in = null;
try {
String s = (String) iterator.next();
s = s.replace('.', '/') + ".class";
if (debugEnabled) {
System.out.println("adding: " + s);
}
in = this.getClass().getClassLoader().getResourceAsStream(s);
out.putNextEntry(new ZipEntry(s));
int w = in.read(buf);
while (w >= 0) {
out.write(buf, 0, w);
w = in.read(buf);
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
} finally {
in.close();
}
}
out.finish();
out.close();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值