Java Unzip ZipFile unzip(final File zipFile, final String suffix)

Here you can find the source of unzip(final File zipFile, final String suffix)

Description

unzip

License

Open Source License

Declaration

private static File unzip(final File zipFile, final String suffix) throws Exception 

Method Source Code


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

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.nio.file.Files;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Main {
    private static final int BUFFER = 2048;

    private static File unzip(final File zipFile, final String suffix) throws Exception {
        File ret = null;/*from   w ww .  jav  a  2 s. co m*/

        try (final FileInputStream fis = new FileInputStream(zipFile);
                final ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis))) {
            ZipEntry entry;

            while ((entry = zis.getNextEntry()) != null) {
                if (entry.getName().endsWith(suffix)) {
                    ret = Files.createTempFile(entry.getName() + "-", suffix).toFile();
                    ret.deleteOnExit();

                    try (final FileOutputStream fos = new FileOutputStream(ret);
                            final BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER)) {
                        int count;
                        final byte[] data = new byte[BUFFER];

                        while ((count = zis.read(data, 0, BUFFER)) != -1) {
                            dest.write(data, 0, count);
                        }

                        dest.flush();
                    }

                    break;
                }
            }
        }

        return ret;
    }
}

Related

  1. unzip(byte[] zippedBytes)
  2. unzip(File srcFile, File toDir)
  3. unzip(final File zip, final File patchDir)
  4. unzip(ZipEntry entry, ZipFile zipfile, File explodedDir)
  5. unzip(ZipFile zip, File fileDir)
  6. unzip(ZipFile zipFile, File dest)
  7. unzip(ZipFile zipFile, File destDir)