Example usage for java.util.zip ZipFile ZipFile

List of usage examples for java.util.zip ZipFile ZipFile

Introduction

In this page you can find the example usage for java.util.zip ZipFile ZipFile.

Prototype

public ZipFile(File file) throws ZipException, IOException 

Source Link

Document

Opens a ZIP file for reading given the specified File object.

Usage

From source file:com.opengamma.util.ZipUtils.java

/**
 * Unzips a ZIP archive./*from www . j a  v  a  2 s  . co  m*/
 * 
 * @param archive  the archive file, not null
 * @param outputDir  the output directory, not null
 */
public static void unzipArchive(final File archive, final File outputDir) {
    ArgumentChecker.notNull(archive, "archive");
    ArgumentChecker.notNull(outputDir, "outputDir");

    s_logger.debug("Unzipping file:{} to {}", archive, outputDir);
    try {
        FileUtils.forceMkdir(outputDir);
        unzipArchive(new ZipFile(archive), outputDir);
    } catch (Exception ex) {
        throw new OpenGammaRuntimeException("Error while extracting file: " + archive + " to: " + outputDir,
                ex);
    }
}

From source file:Utils.java

/**
 * Unpack a zip file/*w  w  w.j  ava  2  s  .  com*/
 * 
 * @param theFile
 * @param targetDir
 * @return the file
 * @throws IOException
 */
public static File unpackArchive(File theFile, File targetDir) throws IOException {
    if (!theFile.exists()) {
        throw new IOException(theFile.getAbsolutePath() + " does not exist");
    }
    if (!buildDirectory(targetDir)) {
        throw new IOException("Could not create directory: " + targetDir);
    }
    ZipFile zipFile = new ZipFile(theFile);
    for (Enumeration entries = zipFile.entries(); entries.hasMoreElements();) {
        ZipEntry entry = (ZipEntry) entries.nextElement();
        File file = new File(targetDir, File.separator + entry.getName());
        if (!buildDirectory(file.getParentFile())) {
            throw new IOException("Could not create directory: " + file.getParentFile());
        }
        if (!entry.isDirectory()) {
            copyInputStream(zipFile.getInputStream(entry),
                    new BufferedOutputStream(new FileOutputStream(file)));
        } else {
            if (!buildDirectory(file)) {
                throw new IOException("Could not create directory: " + file);
            }
        }
    }
    zipFile.close();
    return theFile;
}

From source file:net.sourceforge.atunes.utils.ZipUtils.java

/**
 * Unzips a file in a directory// w  w w  .  ja  va 2  s .com
 * 
 * @param archive
 * @param outputDir
 * @throws IOException
 */
public static void unzipArchive(File archive, File outputDir) throws IOException {
    ZipFile zipfile = null;
    try {
        zipfile = new ZipFile(archive);
        for (Enumeration<? extends ZipEntry> e = zipfile.entries(); e.hasMoreElements();) {
            ZipEntry entry = e.nextElement();
            unzipEntry(zipfile, entry, outputDir);
        }
    } finally {
        ClosingUtils.close(zipfile);
    }
}

From source file:net.sourceforge.floggy.maven.ZipUtils.java

/**
 * DOCUMENT ME!//  www.  j a v a 2s . c o  m
*
* @param file DOCUMENT ME!
* @param directory DOCUMENT ME!
*
* @throws IOException DOCUMENT ME!
*/
public static void unzip(File file, File directory) throws IOException {
    ZipFile zipFile = new ZipFile(file);
    Enumeration entries = zipFile.entries();

    if (!directory.exists() && !directory.mkdirs()) {
        throw new IOException("Unable to create the " + directory + " directory!");
    }

    while (entries.hasMoreElements()) {
        File temp;
        ZipEntry entry = (ZipEntry) entries.nextElement();

        if (entry.isDirectory()) {
            temp = new File(directory, entry.getName());

            if (!temp.exists() && !temp.mkdirs()) {
                throw new IOException("Unable to create the " + temp + " directory!");
            }
        } else {
            temp = new File(directory, entry.getName());
            IOUtils.copy(zipFile.getInputStream(entry), new FileOutputStream(temp));
        }
    }

    zipFile.close();
}

From source file:com.vamonossoftware.core.ZipUtil.java

public static int countFiles(File zip) {
    try {// w  ww . j  ava2  s  . c o  m
        ZipFile zf = new ZipFile(zip);
        int count = 0;
        for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {
            final ZipEntry nextElement = (ZipEntry) entries.nextElement();
            if (!nextElement.isDirectory()) {
                count++;
            }
        }
        return count;
    } catch (IOException e) {
        return -1;
    }
}

From source file:de.rub.syssec.saaf.analysis.steps.extract.ApkUnzipper.java

/**
 * Extracts the given archive into the given destination directory
 * //from   w  ww . j  ava 2  s  .  com
 * @param archive
 *            - the file to extract
 * @param dest
 *            - the destination directory
 * @throws Exception
 */
public static void extractArchive(File archive, File destDir) throws IOException {

    if (!destDir.exists()) {
        destDir.mkdir();
    }

    ZipFile zipFile = new ZipFile(archive);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();

    byte[] buffer = new byte[16384];
    int len;
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();

        String entryFileName = entry.getName();

        File dir = buildDirectoryHierarchyFor(entryFileName, destDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        if (!entry.isDirectory()) {

            File file = new File(destDir, entryFileName);

            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));

            BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));

            while ((len = bis.read(buffer)) > 0) {
                bos.write(buffer, 0, len);
            }

            bos.flush();
            bos.close();
            bis.close();
        }
    }
    zipFile.close();
}

From source file:Zip.java

/**
 * Create a InputStream on a given file, by looking for a zip archive whose
 * path is the file path with ".zip" appended, and by reading the first
 * entry in this zip file./* w  w w .j av a2 s .  c  o m*/
 *
 * @param file the file (with no zip extension)
 *
 * @return a InputStream on the zip entry
 */
public static InputStream createInputStream(File file) {
    try {
        String path = file.getCanonicalPath();

        //ZipFile zf = new ZipFile(path + ".zip");
        ZipFile zf = new ZipFile(path);

        for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {
            ZipEntry entry = (ZipEntry) entries.nextElement();

            return zf.getInputStream(entry);
        }
    } catch (FileNotFoundException ex) {
        System.err.println(ex.toString());
        System.err.println(file + " not found");
    } catch (IOException ex) {
        System.err.println(ex.toString());
    }

    return null;
}

From source file:com.s2g.pst.resume.importer.UnZipHelper.java

/**
 * Unzip it//from w  ww  .j a va 2  s. c o m
 *
 * @param fileName
 * @param outputFolder
 *
 * @throws java.io.FileNotFoundException
 */
public void unZipFolder(String fileName, String outputFolder) throws IOException {

    ZipFile zipFile = new ZipFile(fileName);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        File entryDestination = new File(outputFolder, entry.getName());
        entryDestination.getParentFile().mkdirs();
        if (entry.isDirectory()) {
            //FileHelper fileHelper = new FileHelper();
            //String filename = fileHelper.getUniqueFileName(outputFolder, FilenameUtils.removeExtension(entry.getName()));
            //System.out.println("zip :" + filename);
            //new File(filename).mkdirs();

            entryDestination.mkdirs();

        } else {
            InputStream in = zipFile.getInputStream(entry);
            OutputStream out = new FileOutputStream(entryDestination);
            IOUtils.copy(in, out);
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
        }
    }

    zipFile.close();
    this.deleteZipFile(fileName);
    System.out.println("Done");

}

From source file:Main.java

public static File UpdateZipFromPath(String sZipFile, String sPath) throws Exception {
    File tmpFile = File.createTempFile("z4zip-tmp-", ".zip");
    tmpFile.deleteOnExit();//from  w ww . j a va  2  s.  co m
    ZipFile inZip = new ZipFile(sZipFile);
    ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(tmpFile.getPath()));

    Enumeration<? extends ZipEntry> entries = inZip.entries();
    while (entries.hasMoreElements()) {
        ZipEntry e = entries.nextElement();
        outZip.putNextEntry(e);
        if (!e.isDirectory()) {
            File f = new File(sPath + "/" + e.getName());
            if (f.exists()) {
                copy(new FileInputStream(f.getPath()), outZip);
            } else {
                copy(inZip.getInputStream(e), outZip);
            }
        }
        outZip.closeEntry();
    }
    inZip.close();
    outZip.close();
    return tmpFile;
}

From source file:de.lazyzero.kkMulticopterFlashTool.utils.Zip.java

public static File unzipFile(File zipFile, File file) {
    System.out.println("path to zipFile: " + zipFile.getPath());
    System.out.println("file to extract: " + file.getPath());
    String fileName = null;//from www.ja v a2  s .com

    try {
        zipFile.mkdir();
        BufferedOutputStream dest = null;
        BufferedInputStream is = null;
        ZipEntry entry;
        ZipFile zipfile = new ZipFile(zipFile);
        Enumeration e = zipfile.entries();
        while (e.hasMoreElements()) {
            entry = (ZipEntry) e.nextElement();
            // System.out.println(entry.getName());
            if (entry.getName().substring(entry.getName().indexOf("/") + 1).equals(file.getName())) {
                //   if (entry.getName().contains(file.getName())){
                System.out.println("firmware to extract found.");

                String tempFolder = System.getProperty("user.dir") + File.separatorChar + "tmp"
                        + File.separatorChar;
                if (System.getProperty("os.name").toLowerCase().contains("mac")) {
                    tempFolder = System.getProperty("user.home")
                            + "/Library/Preferences/kkMulticopterFlashTool/";
                }

                String newDir;
                if (entry.getName().indexOf("/") == -1) {
                    newDir = zipFile.getName().substring(0, zipFile.getName().indexOf("."));
                } else {
                    newDir = entry.getName().substring(0, entry.getName().indexOf("/"));
                }
                String folder = tempFolder + newDir;
                System.out.println("Create folder: " + folder);
                if ((new File(folder).mkdir())) {
                    System.out.println("Done.");
                    ;
                }

                System.out.println("Extracting: " + entry);
                is = new BufferedInputStream(zipfile.getInputStream(entry));
                int count;
                byte data[] = new byte[2048];
                fileName = tempFolder + entry.getName();
                FileOutputStream fos = new FileOutputStream(tempFolder + entry.getName());
                dest = new BufferedOutputStream(fos, 2048);
                while ((count = is.read(data, 0, 2048)) != -1) {
                    dest.write(data, 0, count);
                }
                dest.flush();
                dest.close();
                is.close();
                break;
            }
        }
    } catch (ZipException e) {
        zipFile.delete();
        kk.err(Translatrix._("error.zipfileDamaged"));
        kk.err(Translatrix._("reportProblem"));
    } catch (Exception e) {
        e.printStackTrace();
    }

    return new File(fileName);
}