All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jbake.app.ZipUtil Maven / Gradle / Ivy

Go to download

JBake is a Java based open source static site/blog generator for developers.

There is a newer version: 2.7.0-rc.7
Show newest version
package org.jbake.app;

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.ZipInputStream;

/**
 * Provides Zip file related functions
 *
 * @author Jonathan Bullock [email protected]
 *
 */
public class ZipUtil {

    /**
     * Extracts content of Zip file to specified output path.
     *
     * @param is             {@link InputStream} InputStream of Zip file
     * @param outputFolder    folder where Zip file should be extracted to
     * @throws IOException    if IOException occurs
     */
    public static void extract(InputStream is, File outputFolder) throws IOException {
        ZipInputStream zis = new ZipInputStream(is);
        ZipEntry entry;
        byte[] buffer = new byte[1024];

        while ((entry = zis.getNextEntry()) != null) {
            File outputFile = new File(outputFolder.getCanonicalPath() + File.separatorChar + entry.getName());
            File outputParent = new File(outputFile.getParent());
            outputParent.mkdirs();

            if (entry.isDirectory()) {
                if (!outputFile.exists()) {
                    outputFile.mkdir();
                }
            } else {
                try (FileOutputStream fos = new FileOutputStream(outputFile)) {
                    int len;
                    while ((len = zis.read(buffer)) > 0) {
                        fos.write(buffer, 0, len);
                    }
                }
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy