JAR解压缩方法

JAR解压缩方法 作者:hunhun1981 出自:http://blog.csdn.net/hunhun1981/

        下面这些代码,是用来解压jar文件的(我自己写了一个打包工具,专门用于修改配置文件并对原始JAR进行二次发布)。

第一种是使用JarFile类来完成功能,大家可以稍加修改,集成ClassLoader既可以实现一个自解压的JAR包。

package otheri;



import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.Enumeration;

import java.util.jar.JarEntry;

import java.util.jar.JarFile;



/**

 *

 * @author hunhun1981 <hunhun1981@hotmail.com>

 */

public class JARDecompressionTool {



    public static synchronized void decompress(String fileName, String outputPath) throws IOException {

        if (!outputPath.endsWith(File.separator)) {

            outputPath += File.separator;

        }



        File dir = new File(outputPath);



        if (!dir.exists()) {

            dir.mkdir();

        }



        JarFile jf = new JarFile(fileName);

        for (Enumeration e = jf.entries(); e.hasMoreElements();) {

            JarEntry je = (JarEntry) e.nextElement();

            String outFileName = outputPath + je.getName();

            File f = new File(outFileName);



            if (outFileName.endsWith("/") || outFileName.endsWith("//") || outFileName.endsWith(File.separator)) {

                f.mkdir();

            } else {

                InputStream in = jf.getInputStream(je);

                OutputStream out = new BufferedOutputStream(new FileOutputStream(f));

                byte[] buffer = new byte[2048];

                int nBytes = 0;



                while ((nBytes = in.read(buffer)) > 0) {

                    out.write(buffer, 0, nBytes);

                }

                out.flush();

                out.close();

                in.close();

            }

        }

    }

}

 

第二种是使用ant,代码中还附带了jar的打包和wtk签名的方法。使用前请先集成ant和antenna。

package otheri.j2mePackageTools;



import de.pleumann.antenna.WtkSign;

import java.io.File;

import org.apache.tools.ant.Project;

import org.apache.tools.ant.taskdefs.Expand;

import org.apache.tools.ant.taskdefs.Jar;

import org.apache.tools.ant.types.FileSet;



/**

 *

 * @author hunhun1981 <hunhun1981@hotmail.com>

 */

public class JARUtils {



    public static void jarPackage(File srcPath, File manifest, String manifestEncoding, File output) {

        Project prj = new Project();

        Jar jar = new Jar();

        jar.setProject(prj);

        jar.setDestFile(output);



        FileSet fileSet = new FileSet();

        fileSet.setProject(prj);

        fileSet.setDir(srcPath);

        fileSet.setIncludes("**/*.*");

        jar.addFileset(fileSet);



        jar.setManifest(manifest);

        jar.setManifestEncoding(manifestEncoding);



        jar.execute();

    }



    public static void jarExpand(File jarFile, File outputPath) {

        Project prj = new Project();

        Expand expand = new Expand();

        expand.setProject(prj);

        expand.setSrc(jarFile);

        expand.setOverwrite(true);

        expand.setDest(outputPath);

        expand.execute();

    }



    public static void wtkSign(File jadFile, File jarFile, File keyStoreFile, String storePass, String cerAlias, String cerPass) {

        Project prj = new Project();

        WtkSign wtk = new WtkSign();

        wtk.setProject(prj);



        wtk.setJadFile(jadFile);

        wtk.setJarFile(jarFile);



        wtk.setKeyStore(keyStoreFile);

        wtk.setStorePass(storePass);



        wtk.setCertAlias(cerAlias);

        wtk.setCertPass(cerPass);



        wtk.execute();

    }

}

 

更多信息,请关注hunhun1981的专栏

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值