Java Unzip File unzip(File zippedFile)

Here you can find the source of unzip(File zippedFile)

Description

Uncompresses zipped files

License

Open Source License

Parameter

Parameter Description
zippedFile The file to uncompress

Exception

Parameter Description

Return

list of unzipped files

Declaration

public static List<File> unzip(File zippedFile) throws IOException 

Method Source Code


//package com.java2s;
/*//from w ww.ja v  a2  s.  c  o m
 * Copyright (c) 2002-2006 The European Bioinformatics Institute, and others.
 * All rights reserved. Please see the file LICENSE
 * in the root directory of this distribution.
 */

import java.io.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.*;

public class Main {
    /**
     * Uncompresses zipped files
     * @param zippedFile The file to uncompress
     * @return list of unzipped files
     * @throws java.io.IOException thrown if there is a problem finding or writing the files
     */
    public static List<File> unzip(File zippedFile) throws IOException {
        return unzip(zippedFile, null);
    }

    /**
     * Uncompresses zipped files
     * @param zippedFile The file to uncompress
     * @param destinationDir Where to put the files
     * @return  list of unzipped files
     * @throws java.io.IOException thrown if there is a problem finding or writing the files
     */
    public static List<File> unzip(File zippedFile, File destinationDir) throws IOException {
        int buffer = 2048;

        List<File> unzippedFiles = new ArrayList<File>();

        BufferedOutputStream dest;
        BufferedInputStream is;
        ZipEntry entry;
        ZipFile zipfile = new ZipFile(zippedFile);
        Enumeration e = zipfile.entries();
        while (e.hasMoreElements()) {
            entry = (ZipEntry) e.nextElement();

            is = new BufferedInputStream(zipfile.getInputStream(entry));
            int count;
            byte data[] = new byte[buffer];

            File destFile;

            if (destinationDir != null) {
                destFile = new File(destinationDir, entry.getName());
            } else {
                destFile = new File(entry.getName());
            }

            FileOutputStream fos = new FileOutputStream(destFile);
            dest = new BufferedOutputStream(fos, buffer);
            try {
                while ((count = is.read(data, 0, buffer)) != -1) {
                    dest.write(data, 0, count);
                }

                unzippedFiles.add(destFile);
            } finally {
                dest.flush();
                dest.close();
                is.close();
            }
        }

        return unzippedFiles;
    }
}

Related

  1. unZip(File zipFile, String desdir)
  2. unZip(File zipFile, String extPlace, boolean reservZipFile)
  3. unzip(File zipFileName, File targetDir)
  4. unzip(File zipName, File destDir)
  5. unZip(File zipPath, File destPath)
  6. unzip(File zippedFile, File outputDirectory)
  7. unzip(File zippedFile, File targetDir)
  8. unzip(final byte[] zippedContent)
  9. unzip(final File dir, final ZipInputStream from)