Java Unzip ZipEntry unzipEntry(ZipFile zipFile, ZipEntry entry, File entryTarget)

Here you can find the source of unzipEntry(ZipFile zipFile, ZipEntry entry, File entryTarget)

Description

unzip Entry

License

Open Source License

Declaration

public static void unzipEntry(ZipFile zipFile, ZipEntry entry, File entryTarget) 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.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Main {
    public static void unzipEntry(ZipFile zipFile, ZipEntry entry, File entryTarget) throws IOException {
        if (entry.isDirectory()) {
            forceMkdir(entryTarget);// www. j  av  a 2 s . c  o  m
        } else {
            InputStream is = zipFile.getInputStream(entry);
            copyInputStreamToFile(is, entryTarget, false);
        }
    }

    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 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)));
    }

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

    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();
        }
    }

    public static void delete(File file, boolean recursive) throws IOException {
        if (file.exists()) {
            if (file.isDirectory() && recursive) {
                for (File childFile : file.listFiles()) {
                    delete(childFile, recursive);
                }
            }
            if (!file.delete()) {
                throw new IOException("Cannot delete file " + file);
            }
        }
    }
}

Related

  1. unzipEntry(ZipFile zipfile, ZipEntry entry, File outputDir)
  2. unzipEntry(ZipFile zipfile, ZipEntry entry, File outputDir)
  3. unzipEntry(ZipFile zipfile, ZipEntry entry, File outputDirectory)
  4. unzipEntry(ZipInputStream zin, File f)