集册 Java实例教程 从Zip提取全部

从Zip提取全部

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

531
从Zip提取全部
/*from N o w J a v a . c o m*/

//package com.nowjava;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;


import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.security.InvalidParameterException;

import java.util.Enumeration;

import java.util.zip.ZipEntry;

import java.util.zip.ZipFile;


public class Main {

    public static void ExtractAllFromZip(ZipFile zf, File dest)

            throws IOException {/**时 代 J a v a - nowjava.com**/

        if (!dest.isDirectory())

            throw new InvalidParameterException(

                    "Destination must be a directory!");

        Enumeration<? extends ZipEntry> entries = zf.entries();


        while (entries.hasMoreElements()) {

            ZipEntry entry = entries.nextElement();

            //         System.out.println("Extracting " + entry.toString());

            ExtractFromZip(zf, entry, new File(dest, entry.getName()));

        }

    }


    /**

     * Extracts entry from zf into dest

     * @param zf the zip file to extract from

     * @param entry the entry in the zip to extract

     * @param dest the destination to extract to

     */

    public static void ExtractFromZip(ZipFile zf, ZipEntry entry, File dest)

            throws IOException {

        if (entry.isDirectory()) {

            dest.mkdirs();

            return;

        }


        //if (!dest.getParentFile().exists())

        dest.getParentFile().mkdirs();


        if (!dest.exists())

            dest.createNewFile();


        int bufSize = 1024;


        InputStream is = zf.getInputStream(entry);

        BufferedInputStream in = new BufferedInputStream(is, bufSize);


        FileOutputStream fos = new 
展开阅读全文