Example usage for java.util.zip ZipInputStream getNextEntry

List of usage examples for java.util.zip ZipInputStream getNextEntry

Introduction

In this page you can find the example usage for java.util.zip ZipInputStream getNextEntry.

Prototype

public ZipEntry getNextEntry() throws IOException 

Source Link

Document

Reads the next ZIP file entry and positions the stream at the beginning of the entry data.

Usage

From source file:Main.java

/**
 * Extract a zip resource into real files and directories
 * //from w  ww.  j  a  v a2 s  . c om
 * @param in typically given as getResources().openRawResource(R.raw.something)
 * @param directory target directory
 * @param overwrite indicates whether to overwrite existing files
 * @return list of files that were unpacked (if overwrite is false, this list won't include files
 *         that existed before)
 * @throws IOException
 */
public static List<File> extractZipResource(InputStream in, File directory, boolean overwrite)
        throws IOException {
    final int BUFSIZE = 2048;
    byte buffer[] = new byte[BUFSIZE];
    ZipInputStream zin = new ZipInputStream(new BufferedInputStream(in, BUFSIZE));
    List<File> files = new ArrayList<File>();
    ZipEntry entry;
    directory.mkdirs();
    while ((entry = zin.getNextEntry()) != null) {
        File file = new File(directory, entry.getName());
        files.add(file);
        if (overwrite || !file.exists()) {
            if (entry.isDirectory()) {
                file.mkdirs();
            } else {
                file.getParentFile().mkdirs(); // Necessary because some zip files lack directory entries.
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file), BUFSIZE);
                int nRead;
                while ((nRead = zin.read(buffer, 0, BUFSIZE)) > 0) {
                    bos.write(buffer, 0, nRead);
                }
                bos.flush();
                bos.close();
            }
        }
    }
    zin.close();
    return files;
}

From source file:com.formkiq.core.util.Zips.java

/**
 * Extract Zip file to Map.//  ww w.ja  v a2 s.  c  o  m
 * @param bytes byte[]
 * @return {@link Map}
 * @throws IOException IOException
 */
public static Map<String, byte[]> extractZipToMap(final byte[] bytes) throws IOException {

    Map<String, byte[]> map = new HashMap<>();
    ByteArrayInputStream is = new ByteArrayInputStream(bytes);
    ZipInputStream zipStream = new ZipInputStream(is);

    try {

        ZipEntry entry = null;

        while ((entry = zipStream.getNextEntry()) != null) {

            String filename = entry.getName();
            byte[] data = IOUtils.toByteArray(zipStream);
            map.put(filename, data);
        }

    } finally {

        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(zipStream);
    }

    return map;
}

From source file:eu.openanalytics.rsb.message.MultiFilesJob.java

/**
 * Adds all the files contained in a Zip archive to a job. Rejects Zips that
 * contain sub-directories.// w  w  w. jav a2  s. c o m
 * 
 * @param data
 * @param job
 * @throws IOException
 */
public static void addZipFilesToJob(final InputStream data, final MultiFilesJob job) throws IOException {
    final ZipInputStream zis = new ZipInputStream(data);
    ZipEntry ze = null;

    while ((ze = zis.getNextEntry()) != null) {
        if (ze.isDirectory()) {
            job.destroy();
            throw new IllegalArgumentException("Invalid zip archive: nested directories are not supported");
        }
        job.addFile(ze.getName(), zis);
        zis.closeEntry();
    }

    IOUtils.closeQuietly(zis);
}

From source file:cd.education.data.collector.android.utilities.ZipUtils.java

public static File extractFirstZipEntry(File zipFile, boolean deleteAfterUnzip) throws IOException {
    ZipInputStream zipInputStream = null;
    File targetFile = null;//from w  w  w. j av a 2 s  .  c om
    try {
        zipInputStream = new ZipInputStream(new FileInputStream(zipFile));
        ZipEntry zipEntry = zipInputStream.getNextEntry();
        if (zipEntry != null) {
            targetFile = doExtractInTheSameFolder(zipFile, zipInputStream, zipEntry);
        }
    } finally {
        IOUtils.closeQuietly(zipInputStream);
    }

    if (deleteAfterUnzip && targetFile != null && targetFile.exists()) {
        FileUtils.deleteAndReport(zipFile);
    }

    return targetFile;
}

From source file:ch.rgw.compress.CompEx.java

public static byte[] expand(InputStream in) {
    ByteArrayOutputStream baos;/*from w w w. j a va  2s  .  c  o  m*/
    byte[] siz = new byte[4];
    try {
        in.read(siz);
        long size = BinConverter.byteArrayToInt(siz, 0);
        long typ = size & ~0x1fffffff;
        size &= 0x1fffffff;
        byte[] ret = new byte[(int) size];

        switch ((int) typ) {
        case BZIP2:
            CBZip2InputStream bzi = new CBZip2InputStream(in);
            int off = 0;
            int l = 0;
            while ((l = bzi.read(ret, off, ret.length - off)) > 0) {
                off += l;
            }

            bzi.close();
            in.close();
            return ret;
        case GLZ:
            GLZ glz = new GLZ();
            baos = new ByteArrayOutputStream();
            glz.expand(in, baos);
            return baos.toByteArray();
        case HUFF:
            HuffmanInputStream hin = new HuffmanInputStream(in);
            off = 0;
            l = 0;
            while ((l = hin.read(ret, off, ret.length - off)) > 0) {
                off += l;
            }
            hin.close();
            return ret;
        case ZIP:
            ZipInputStream zi = new ZipInputStream(in);
            zi.getNextEntry();
            off = 0;
            l = 0;
            while ((l = zi.read(ret, off, ret.length - off)) > 0) {
                off += l;
            }

            zi.close();
            return ret;
        default:
            throw new Exception("Invalid compress format");
        }
    } catch (Exception ex) {
        ExHandler.handle(ex);
        return null;
    }

}

From source file:com.nuvolect.deepdive.probe.ApkZipUtil.java

public static boolean unzip(OmniFile zipOmni, OmniFile targetDir, ProgressStream progressStream) {

    String volumeId = zipOmni.getVolumeId();
    String rootFolderPath = targetDir.getPath();
    boolean DEBUG = true;

    ZipInputStream zis = new ZipInputStream(zipOmni.getFileInputStream());
    ZipEntry entry = null;//from  ww w. j av a 2  s  .  c  o m
    try {
        while ((entry = zis.getNextEntry()) != null) {

            if (entry.isDirectory()) {

                OmniFile dir = new OmniFile(volumeId, entry.getName());
                if (dir.mkdir()) {

                    if (DEBUG)
                        LogUtil.log(LogUtil.LogType.OMNI_ZIP, "dir created: " + dir.getPath());
                }
            } else {

                String path = rootFolderPath + "/" + entry.getName();
                OmniFile file = new OmniFile(volumeId, path);

                // Create any necessary directories
                file.getParentFile().mkdirs();

                OutputStream out = file.getOutputStream();

                OmniFiles.copyFileLeaveInOpen(zis, out);
                if (DEBUG)
                    LogUtil.log(LogUtil.LogType.OMNI_ZIP, "file created: " + file.getPath());

                progressStream.putStream("Unpacked: " + entry.getName());
            }
        }
        zis.close();

    } catch (IOException e) {
        LogUtil.logException(LogUtil.LogType.OMNI_ZIP, e);
        return false;
    }

    return true;
}

From source file:com.nuvolect.deepdive.probe.ApkZipUtil.java

/**
 * Unzip while excluding XML files into the target directory.
 * The added structure is updated to reflect any new files and directories created.
 * Overwrite files when requested.//from w w w.  j  av a  2  s  . c om
 * @param zipOmni
 * @param targetDir
 * @param progressStream
 * @return
 */
public static boolean unzipAllExceptXML(OmniFile zipOmni, OmniFile targetDir, ProgressStream progressStream) {

    String volumeId = zipOmni.getVolumeId();
    String rootFolderPath = targetDir.getPath();
    boolean DEBUG = true;

    ZipInputStream zis = new ZipInputStream(zipOmni.getFileInputStream());
    ZipEntry entry = null;
    try {
        while ((entry = zis.getNextEntry()) != null) {

            if (entry.isDirectory()) {

                OmniFile dir = new OmniFile(volumeId, entry.getName());
                if (dir.mkdir()) {

                    if (DEBUG)
                        LogUtil.log(LogUtil.LogType.OMNI_ZIP, "dir created: " + dir.getPath());
                }
            } else {

                String path = rootFolderPath + "/" + entry.getName();
                OmniFile file = new OmniFile(volumeId, path);

                if (!FilenameUtils.getExtension(file.getName()).contentEquals("xml")) {

                    // Create any necessary directories
                    file.getParentFile().mkdirs();

                    OutputStream out = file.getOutputStream();

                    OmniFiles.copyFileLeaveInOpen(zis, out);
                    if (DEBUG)
                        LogUtil.log(LogUtil.LogType.OMNI_ZIP, "file created: " + file.getPath());

                    progressStream.putStream("Unpacked: " + entry.getName());
                }
            }
        }
        zis.close();

    } catch (IOException e) {
        LogUtil.logException(LogUtil.LogType.OMNI_ZIP, e);
        return false;
    }

    return true;
}

From source file:com.ccoe.build.utils.CompressUtils.java

/**
 * Uncompress a zip to files//from  w w  w  .  j av  a  2s . c  o m
 * @param zip
 * @param unzipdir
 * @param isNeedClean
 * @return
 * @throws FileNotFoundException 
 * @throws IOException 
 */
public static List<File> unCompress(File zip, String unzipdir) throws IOException {
    ArrayList<File> unzipfiles = new ArrayList<File>();

    FileInputStream fi = new FileInputStream(zip);
    ZipInputStream zi = new ZipInputStream(new BufferedInputStream(fi));
    try {

        ZipEntry entry;
        while ((entry = zi.getNextEntry()) != null) {
            System.out.println("Extracting: " + entry);
            int count;
            byte data[] = new byte[BUFFER];
            File unzipfile = new File(unzipdir + File.separator + entry.getName());
            FileOutputStream fos = new FileOutputStream(unzipfile);
            BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);

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

            unzipfiles.add(unzipfile);
        }

    } catch (IOException e) {
        throw e;
    } finally {
        IOUtils.closeQuietly(zi);
    }

    return unzipfiles;
}

From source file:cd.education.data.collector.android.utilities.ZipUtils.java

public static void unzip(File[] zipFiles) {
    for (File zipFile : zipFiles) {
        ZipInputStream zipInputStream = null;
        try {/*from   w w  w.j  a v a2s  .  c  o m*/
            zipInputStream = new ZipInputStream(new FileInputStream(zipFile));
            ZipEntry zipEntry;
            while ((zipEntry = zipInputStream.getNextEntry()) != null) {
                doExtractInTheSameFolder(zipFile, zipInputStream, zipEntry);
            }
        } catch (Exception e) {
            Log.e(t, e.getMessage(), e);
        } finally {
            IOUtils.closeQuietly(zipInputStream);
        }
    }
}

From source file:com.BibleQuote.utils.FsUtils.java

public static BufferedReader getTextFileReaderFromZipArchive(String archivePath, String textFileInArchive,
        String textFileEncoding) throws FileAccessException {
    File zipFile = new File(archivePath);
    try {//from  www . j a va  2  s .c o m
        InputStream moduleStream = new FileInputStream(zipFile);
        ZipInputStream zStream = new ZipInputStream(moduleStream);
        ZipEntry entry;
        while ((entry = zStream.getNextEntry()) != null) {
            String entryName = entry.getName().toLowerCase();
            if (entryName.contains(File.separator)) {
                entryName = entryName.substring(entryName.lastIndexOf(File.separator) + 1);
            }
            String fileName = textFileInArchive.toLowerCase();
            if (entryName.equals(fileName)) {
                InputStreamReader iReader = new InputStreamReader(zStream, textFileEncoding);
                return new BufferedReader(iReader);
            }
            ;
        }
        String message = String.format("File %1$s in zip-arhive %2$s not found", textFileInArchive,
                archivePath);
        Log.e(TAG, message);
        throw new FileAccessException(message);
    } catch (UTFDataFormatException e) {
        String message = String.format("Archive %1$s contains the file names not in the UTF format",
                zipFile.getName());
        Log.e(TAG, message);
        throw new FileAccessException(message);
    } catch (FileNotFoundException e) {
        String message = String.format("File %1$s in zip-arhive %2$s not found", textFileInArchive,
                archivePath);
        throw new FileAccessException(message);
    } catch (IOException e) {
        Log.e(TAG, String.format("getTextFileReaderFromZipArchive(%1$s, %2$s, %3$s)", archivePath,
                textFileInArchive, textFileEncoding), e);
        throw new FileAccessException(e);
    }
}