Java Unzip File unzip(File archiveFile, File targetDir, boolean skipRoot)

Here you can find the source of unzip(File archiveFile, File targetDir, boolean skipRoot)

Description

unzip

License

Open Source License

Declaration

public static void unzip(File archiveFile, File targetDir, boolean skipRoot) throws IOException 

Method Source Code

//package com.java2s;
//it under the terms of the GNU Affero General Public License as published by

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.util.Enumeration;

import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Main {
    public static void unzip(File archiveFile, File targetDir, boolean skipRoot) throws IOException {
        forceMkdir(targetDir);//from   w  w  w  .ja v a2  s  .co  m
        ZipFile zipFile = new ZipFile(archiveFile);
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            File entryTarget;
            if (skipRoot) {
                String name = entry.getName();
                name = name.substring(name.indexOf('/') + 1);
                if (name.length() == 0)
                    entryTarget = targetDir;
                else
                    entryTarget = new File(targetDir, name);
            } else {
                entryTarget = new File(targetDir, entry.getName());
            }
            unzipEntry(zipFile, entry, entryTarget);
        }
        zipFile.close();
    }

    public static void forceMkdir(File dir) throws IOException {
        boolean success = dir.isDirectory();
        if (!success) {
            success = dir.mkdirs();
        }
        checkSuccess(success, "Cannot create directory " + dir);
    }

    public static void unzipEntry(ZipFile zipFile, ZipEntry entry, File entryTarget) throws IOException {
        if (entry.isDirectory()) {
            forceMkdir(entryTarget);
        } else {
            InputStream is = zipFile.getInputStream(entry);
            copyInputStreamToFile(is, entryTarget, false);
        }
    }

    private static void checkSuccess(boolean success, String message) throws IOException {
        if (!success) {
            throw new IOException(message);
        }
    }

    public static void copyInputStreamToFile(InputStream is, File targetFile, boolean backup) throws IOException {
        if (targetFile.exists() && backup) {
            renameToBackupName(targetFile);
        } else if (!targetFile.getParentFile().exists()) {
            forceMkdir(targetFile.getParentFile());
        }
        copy(is, new BufferedOutputStream(new FileOutputStream(targetFile)));
    }

    public static void renameToBackupName(File file) throws IOException {
        File backupFile = new File(file.getParentFile(), file.getName() + "~");
        if (backupFile.exists()) {
            boolean deleted = backupFile.delete();
            if (!deleted) {
                throw new IOException("Cannot delete backup file " + backupFile);
            }
        }
        boolean renamed = file.renameTo(backupFile);
        if (!renamed) {
            throw new IOException("Cannot create backup file for " + file);
        }
    }

    public static void copy(InputStream is, OutputStream os) throws IOException {
        try {
            byte[] buf = new byte[10240];
            int len;
            while ((len = is.read(buf)) != -1) {
                os.write(buf, 0, len);
            }
        } finally {
            os.close();
            is.close();
        }
    }
}

Related

  1. unzip(File aFile)
  2. unzip(File archive)
  3. unzip(File archive, File output)
  4. unzip(File archiveFile, File destination)
  5. unzip(File archiveFile, File destination)
  6. unzip(File dest, String jar)
  7. unzip(File destDir, InputStream is)
  8. unzip(File epubfile, File destination)
  9. unzip(File file, File dest)