集册 Java实例教程 测试Gen Jar

测试Gen Jar

欢马劈雪     最近更新时间:2020-01-02 10:19:05

573
测试Gen Jar
//来自 n o w j a v a . c o m - 时  代  Java

import java.io.BufferedInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.Enumeration;

import java.util.jar.Attributes;

import java.util.jar.JarEntry;

import java.util.jar.JarFile;

import java.util.jar.JarOutputStream;

import java.util.jar.Manifest;
/** 来自 时 代 J     a    v  a - nowjava.com**/

public class Main{

    public static void testGenJar() {

        try {

            String javaClassPath = "C:/classes/";// E:/workspace/zcloud-education/target/classes/

            String targetPath = System.getProperty("user.home")

                    + File.separator + "temp" + File.separator

                    + "education.jar";

            System.out.println("targetPath:" + targetPath);

            JarUtil util = new JarUtil(javaClassPath, targetPath);

            util.generateJar();

        } catch (FileNotFoundException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        } catch (IOException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

    public void generateJar() throws FileNotFoundException, IOException {


        System.out.println("*** --> ?????jar?...");

        String targetDirPath = targetPath.substring(0,

                targetPath.lastIndexOf(File.separator));

        File targetDir = new File(targetDirPath);

        if (!targetDir.exists()) {

            targetDir.mkdirs();

        }


        Manifest manifest = new Manifest();

        manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION,

                "1.0");


        JarOutputStream target = new JarOutputStream(new FileOutputStream(

                targetPath), manifest);

        writeClassFile(new File(javaClassPath), target);

        target.close();

        System.out.println("*** --> jar???????");

    }

    private void writeClassFile(File source, JarOutputStream target)

            throws IOException {

        BufferedInputStream in = null;

        try {

            if (source.isDirectory()) {

                String name = source.getPath().replace("\\", "/");

                if (!name.isEmpty()) {

                    if (!name.endsWith("/")) {

                        name += "/";

                    }

                    name = name.substring(javaClassPath.length());

                    if (!name.equals("")) {// fix bug

                        JarEntry entry = new JarEntry(name);

                        entry.setTime(source.lastModified());

                        target.putNextEntry(entry);

                        target.closeEntry();

                    }

                }

                for (File nestedFile : source.listFiles())

                    writeClassFile(nestedFile, target);

                return;

            }


            String middleName = source.getPath().replace("\\", "/")

                    .substring(javaClassPath.length());

            if (middleName.endsWith(".class")) {

                JarEntry entry = 
展开阅读全文