Extracts entry from ZipFile into dest - Java File Path IO

Java examples for File Path IO:Zip File

Description

Extracts entry from ZipFile into dest

Demo Code


//package com.java2s;
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.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Main {
    /**//from www.j  av  a 2 s . c  om
     * 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 FileOutputStream(dest);
        BufferedOutputStream out = new BufferedOutputStream(fos, bufSize);

        int count = 0;
        byte[] buffer = new byte[bufSize];
        while ((count = in.read(buffer, 0, buffer.length)) != -1) {
            out.write(buffer, 0, count);
        }
        out.flush();
        out.close();
        is.close();
    }
}

Related Tutorials