集册 Java实例教程 测试联合国Jar

测试联合国Jar

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

465
测试联合国Jar


import java.io.BufferedInputStream;

import java.io.File;/* from nowjava.com - 时  代  Java*/

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;


public class Main{

    public static void testUnJar() {

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

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

                + "education-1.jar";// 来自 时 代      J a v a   公   众 号 - nowjava.com

        File jarFile = new File(targetPath);

        File toDir = new File(System.getProperty("user.home")

                + File.separator + "temp");

        try {

            JarUtil.unJar(jarFile, toDir);

        } catch (IOException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

    }

    public static void unJar(File jarFile, File toDir) throws IOException {

        JarFile jar = new JarFile(jarFile);

        try {

            @SuppressWarnings("rawtypes")

            Enumeration entries = jar.entries();

            while (entries.hasMoreElements()) {

                JarEntry entry = (JarEntry) entries.nextElement();

                if (!entry.isDirectory()) {

                    InputStream in = jar.getInputStream(entry);

                    try {

                        File file = new File(toDir, entry.getName());

                        if (!file.getParentFile().mkdirs()) {

                            if (!file.getParentFile().isDirectory()) {

                                throw new IOException(

                                        "Mkdirs failed to create "

                                                + file.getParentFile()

                                                        .toString());

                            }

                        }

                        OutputStream out = new FileOutputStream(file);

                        try {

                            byte[] buffer = 
展开阅读全文