集册 Java实例教程 将文件解压缩到路径。

将文件解压缩到路径。

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

496
将文件解压缩到路径。


//package com.nowjava;/* 来自 时   代     Java  公  众  号 - nowjava.com*/

import java.io.*;


import java.util.Enumeration;


import java.util.jar.JarEntry;

import java.util.jar.JarFile;


public class Main {

    /**

     * Unzip files to path.

     * 

     * @param zipFileName the zip file name

     * @param fileExtractPath the file extract path

     * @throws IOException Signals that an I/O exception has occurred.

     */

    public static void unzipFilesToPath(String jarPath,

            String destinationDir) throws IOException {

        File file = new File(jarPath);

        JarFile jar = new JarFile(file);
        /*来自 
         N o w J a v a . c o m*/


        // fist get all directories,

        // then make those directory on the destination Path

        for (Enumeration<JarEntry> enums = jar.entries(); enums

                .hasMoreElements();) {

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


            String fileName = destinationDir + File.separator

                    + entry.getName();

            File f = new File(fileName);


            if (fileName.endsWith("/")) {

                f.mkdirs();

            }


        }


        //now create all files

        for (Enumeration<JarEntry> enums = jar.entries(); enums

                .hasMoreElements();) {

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


            String fileName = destinationDir + File.separator

                    + entry.getName();

            File f = new File(fileName);


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

                InputStream is = jar.getInputStream(entry);

                FileOutputStream fos = new FileOutputStream(f
展开阅读全文