Java Unzip File unzip(File zip, String folder, File into)

Here you can find the source of unzip(File zip, String folder, File into)

Description

unzip

License

Open Source License

Declaration

public static void unzip(File zip, String folder, File into) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import com.google.common.io.Files;

public class Main {
    public static final String BACKUP_EXT = ".bck";

    public static void unzip(File zip, String folder, File into) throws IOException {
        into.mkdirs();// www.  j  a  va 2  s  . c  o m

        ZipInputStream stream = new ZipInputStream(new FileInputStream(zip));
        ZipEntry entry = stream.getNextEntry();

        byte[] buffer = new byte[1024];

        while (entry != null) {
            String path = entry.getName();

            if (path.startsWith(folder)) { //Copy files only from the target folder

                //Cut the source folder prefix
                path = path.substring(folder.length(), path.length());

                //Target file
                File target = new File(into, path);

                if (target.isDirectory()) {

                    //Create parent directories
                    target.mkdirs();

                } else {

                    //Overwrite outdated, or conflicted files
                    if (target.exists()) {
                        long desiredSize = entry.getSize();

                        if (desiredSize > 0 && target.length() != desiredSize)
                            makeBackupFile(target);

                    } else {
                        target.createNewFile();
                    }

                    //Copy file contents
                    FileOutputStream fOut = new FileOutputStream(target);

                    int read = 0;
                    while ((read = stream.read(buffer)) > -1)
                        fOut.write(buffer, 0, read);

                    fOut.flush();
                    fOut.close();
                }
            }

            stream.closeEntry();
            entry = stream.getNextEntry();
        }

        stream.close();
    }

    public static void makeBackupFile(File file) throws IOException {
        String path = file.getAbsolutePath();
        File target = new File(path + BACKUP_EXT);

        int suffix = 0;
        while (target.exists())
            target = new File(path + BACKUP_EXT + (++suffix));

        Files.copy(file, target);
    }
}

Related

  1. unzip(File zip, File extractTo)
  2. unzip(File zip, File location)
  3. unzip(File zip, File targetDir)
  4. unzip(File zip, File toDir)
  5. unzip(File zip, File toDir)
  6. unZip(File zipf, String targetDir)
  7. unzip(File zipFile, File dest)
  8. unzip(File zipFile, File destDir)
  9. unzip(File zipFile, File destDir)