Example usage for java.util.zip ZipInputStream close

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

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes this input stream and releases any system resources associated with the stream.

Usage

From source file:de.pksoftware.springstrap.sapi.util.WebappZipper.java

/**
 * Unzip it/*from   ww w.ja  va2s  .c  o  m*/
 * @param zipFile input zip file
 * @param output zip file output folder
 */
public static void unzip(String zipFile, String outputFolder) {
    logger.info("Unzipping {} into {}.", zipFile, outputFolder);

    byte[] buffer = new byte[1024];

    try {

        //get the zip file content
        ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
        //get the zipped file list entry
        ZipEntry ze = zis.getNextEntry();

        while (ze != null) {
            if (ze.isDirectory()) {
                ze = zis.getNextEntry();
                continue;
            }

            String fileName = ze.getName();
            File newFile = new File(outputFolder + File.separator + fileName);

            logger.debug("Unzipping: {}", newFile.getAbsoluteFile());

            //create all non exists folders
            //else you will hit FileNotFoundException for compressed folder
            new File(newFile.getParent()).mkdirs();

            FileOutputStream fos = new FileOutputStream(newFile);

            int len;
            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }

            fos.close();
            ze = zis.getNextEntry();
        }

        zis.closeEntry();
        zis.close();

        logger.debug("Unzipping {} completed.", zipFile);

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

/**
 * kudos: http://www.jondev.net/articles/Unzipping_Files_with_Android_(Programmatically)
 * @param zipFile//from  w  w  w.ja v  a  2 s  .  c  o  m
 * @param destinationDirectory
 */
public static void unZip(String zipFile, String destinationDirectory) {
    try {
        FileInputStream fin = new FileInputStream(zipFile);
        ZipInputStream zin = new ZipInputStream(fin);
        ZipEntry ze = null;
        while ((ze = zin.getNextEntry()) != null) {
            Log.v("Decompress", "Unzipping " + ze.getName());
            String destinationPath = destinationDirectory + File.separator + ze.getName();
            if (ze.isDirectory()) {
                dirChecker(destinationPath);
            } else {
                FileOutputStream fout;
                try {
                    File outputFile = new File(destinationPath);
                    if (!outputFile.getParentFile().exists()) {
                        dirChecker(outputFile.getParentFile().getPath());
                    }
                    fout = new FileOutputStream(destinationPath);
                    for (int c = zin.read(); c != -1; c = zin.read()) {
                        fout.write(c);
                    }
                    zin.closeEntry();
                    fout.close();
                } catch (Exception e) {
                    // ok for now.
                    Log.v("Decompress", "Error: " + e.getMessage());
                }
            }
        }
        zin.close();
    } catch (Exception e) {
        Log.e("Decompress", "unzip", e);
    }
}

From source file:JarMaker.java

/**
 * Combine several jar files and some given files into one jar file.
 * @param outJar The output jar file's filename.
 * @param inJarsList The jar files to be combined
 * @param filter A filter that exclude some entries of input jars. User
 *        should implement the EntryFilter interface.
 * @param inFileList The files to be added into the jar file.
 * @param prefixs The prefixs of files to be added into the jar file.
 *        inFileList and prefixs should be paired.
 * @throws FileNotFoundException/*w  w w  .j  a v  a  2 s.co  m*/
 * @throws IOException
 */
@SuppressWarnings("unchecked")
public static void combineJars(String outJar, String[] inJarsList, EntryFilter filter, String[] inFileList,
        String[] prefixs) throws FileNotFoundException, IOException {

    JarOutputStream jout = new JarOutputStream(new FileOutputStream(outJar));
    ArrayList entryList = new ArrayList();
    for (int i = 0; i < inJarsList.length; i++) {
        BufferedInputStream bufferedinputstream = new BufferedInputStream(new FileInputStream(inJarsList[i]));
        ZipInputStream zipinputstream = new ZipInputStream(bufferedinputstream);
        byte abyte0[] = new byte[1024 * 4];
        for (ZipEntry zipentry = zipinputstream.getNextEntry(); zipentry != null; zipentry = zipinputstream
                .getNextEntry()) {
            if (filter.accept(zipentry)) {
                if (!entryList.contains(zipentry.getName())) {
                    jout.putNextEntry(zipentry);
                    if (!zipentry.isDirectory()) {
                        int j;
                        try {
                            while ((j = zipinputstream.read(abyte0, 0, abyte0.length)) != -1)
                                jout.write(abyte0, 0, j);
                        } catch (IOException ie) {
                            throw ie;
                        }
                    }
                    entryList.add(zipentry.getName());
                }
            }
        }
        zipinputstream.close();
        bufferedinputstream.close();
    }
    for (int i = 0; i < inFileList.length; i++) {
        add(jout, new File(inFileList[i]), prefixs[i]);
    }
    jout.close();
}

From source file:com.bukanir.android.utils.Utils.java

public static String unzipSubtitle(String zip, String path) {
    InputStream is;/*from  ww  w .  java 2  s.  c  om*/
    ZipInputStream zis;
    try {
        String filename = null;
        is = new FileInputStream(zip);
        zis = new ZipInputStream(new BufferedInputStream(is));
        ZipEntry ze;
        byte[] buffer = new byte[1024];
        int count;

        while ((ze = zis.getNextEntry()) != null) {
            filename = ze.getName();

            if (ze.isDirectory()) {
                File fmd = new File(path + "/" + filename);
                fmd.mkdirs();
                continue;
            }

            if (filename.endsWith(".srt") || filename.endsWith(".sub")) {
                FileOutputStream fout = new FileOutputStream(path + "/" + filename);
                while ((count = zis.read(buffer)) != -1) {
                    fout.write(buffer, 0, count);
                }
                fout.close();
                zis.closeEntry();
                break;
            }
            zis.closeEntry();
        }
        zis.close();

        File z = new File(zip);
        z.delete();

        return path + "/" + filename;

    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

}

From source file:com.matze5800.paupdater.Functions.java

public static void addFilesToExistingZip(File zipFile, File[] files) throws IOException {
    File tempFile = new File(Environment.getExternalStorageDirectory() + "/pa_updater", "temp_kernel.zip");
    tempFile.delete();/*from  w ww . j  a va 2  s .  c o  m*/

    boolean renameOk = zipFile.renameTo(tempFile);
    if (!renameOk) {
        throw new RuntimeException(
                "could not rename the file " + zipFile.getAbsolutePath() + " to " + tempFile.getAbsolutePath());
    }
    byte[] buf = new byte[1024];

    ZipInputStream zin = new ZipInputStream(new FileInputStream(tempFile));
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));

    ZipEntry entry = zin.getNextEntry();
    while (entry != null) {
        String name = entry.getName();
        boolean notInFiles = true;
        for (File f : files) {
            if (f.getName().equals(name)) {
                notInFiles = false;
                break;
            }
        }
        if (notInFiles) {

            out.putNextEntry(new ZipEntry(name));

            int len;
            while ((len = zin.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        }
        entry = zin.getNextEntry();
    }
    zin.close();

    for (int i = 0; i < files.length; i++) {
        InputStream in = new FileInputStream(files[i]);
        out.putNextEntry(new ZipEntry(files[i].getName()));

        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        out.closeEntry();
        in.close();
    }
    out.close();
    tempFile.delete();
}

From source file:Main.java

public static void unZipFolder(InputStream input, String outPathString) throws Exception {
    java.util.zip.ZipInputStream inZip = new java.util.zip.ZipInputStream(input);
    java.util.zip.ZipEntry zipEntry = null;
    String szName = "";

    while ((zipEntry = inZip.getNextEntry()) != null) {
        szName = zipEntry.getName();/*  w ww  . j  ava 2 s.c  om*/

        if (zipEntry.isDirectory()) {

            // get the folder name of the widget
            szName = szName.substring(0, szName.length() - 1);
            java.io.File folder = new java.io.File(outPathString + java.io.File.separator + szName);
            folder.mkdirs();

        } else {

            java.io.File file = new java.io.File(outPathString + java.io.File.separator + szName);
            file.createNewFile();
            // get the output stream of the file
            java.io.FileOutputStream out = new java.io.FileOutputStream(file);
            int len;
            byte[] buffer = new byte[1024];
            // read (len) bytes into buffer
            while ((len = inZip.read(buffer)) != -1) {
                // write (len) byte from buffer at the position 0
                out.write(buffer, 0, len);
                out.flush();
            }
            out.close();
        }
    } //end of while

    inZip.close();
}

From source file:net.ftb.util.FileUtils.java

public static void backupExtract(String zipLocation, String outputLocation) {
    Logger.logInfo("Extracting (Backup way)");
    byte[] buffer = new byte[1024];
    ZipInputStream zis = null;
    ZipEntry ze;/* ww  w .j  a  v  a2  s . c  o m*/
    try {
        File folder = new File(outputLocation);
        if (!folder.exists()) {
            folder.mkdir();
        }
        zis = new ZipInputStream(new FileInputStream(zipLocation));
        ze = zis.getNextEntry();
        while (ze != null) {
            File newFile = new File(outputLocation, ze.getName());
            newFile.getParentFile().mkdirs();
            if (!ze.isDirectory()) {
                FileOutputStream fos = new FileOutputStream(newFile);
                int len;
                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }
                fos.flush();
                fos.close();
            }
            ze = zis.getNextEntry();
        }
    } catch (IOException ex) {
        Logger.logError("Error while extracting zip", ex);
    } finally {
        try {
            zis.closeEntry();
            zis.close();
        } catch (IOException e) {
        }
    }
}

From source file:Main.java

public static void unCompress(String zipPath, String toPath) throws IOException {
    File zipfile = new File(zipPath);
    if (!zipfile.exists())
        return;/*www  .  j  a va 2 s  .co  m*/

    if (!toPath.endsWith("/"))
        toPath += "/";

    File destFile = new File(toPath);
    if (!destFile.exists())
        destFile.mkdirs();

    ZipInputStream zis = new ZipInputStream(new FileInputStream(zipfile));
    ZipEntry entry = null;

    try {

        while ((entry = zis.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                File file = new File(toPath + entry.getName() + "/");
                file.mkdirs();
            } else {
                File file = new File(toPath + entry.getName());
                if (!file.getParentFile().exists())
                    file.getParentFile().mkdirs();

                FileOutputStream fos = null;

                try {
                    fos = new FileOutputStream(file);
                    byte buf[] = new byte[1024];
                    int len = -1;
                    while ((len = zis.read(buf, 0, 1024)) != -1) {
                        fos.write(buf, 0, len);
                    }
                } finally {

                    if (fos != null) {
                        try {
                            fos.close();
                        } catch (Exception e) {
                        }
                    }
                }
            }
        }

    } finally {
        zis.close();
    }

}

From source file:hu.sztaki.lpds.pgportal.services.dspace.DSpaceUtil.java

/**
 * Extracts any file from <code>inputFile</code> that begins with
 * 'bitstream' to <code>outputFile</code>.
 * Adapted from examples on/*w  ww . ja va 2 s.c  om*/
 * http://java.sun.com/developer/technicalArticles/Programming/compression/
 *
 * @param inputFile  File to extract 'bitstream...' from
 * @param outputFile  File to extract 'bitstream...' to
 */
private static void unzipBitstream(InputStream is, File outputFile) throws IOException {
    final int BUFFER = 2048;
    BufferedOutputStream dest = null;
    ZipInputStream zis = new ZipInputStream(is);
    ZipEntry entry;
    try {

        while ((entry = zis.getNextEntry()) != null) {
            if (entry.getName().matches("bitstream.*")) {
                int count;
                byte data[] = new byte[BUFFER];

                // write the files to the disk
                FileOutputStream fos = new FileOutputStream(outputFile.getAbsoluteFile());
                dest = new BufferedOutputStream(fos, BUFFER);
                while ((count = zis.read(data, 0, BUFFER)) != -1) {
                    dest.write(data, 0, count);
                    //System.out.println("Writing: " + outputFile.getAbsoluteFile());
                }
                dest.flush();
                dest.close();
            }
        }
    } finally {
        zis.close();
        try {
            dest.close();
        } catch (Exception e) {
        }
    }
}

From source file:gdt.data.entity.ArchiveHandler.java

private static boolean hasPropertyIndexInZipStream(ZipInputStream zis) {
    try {//from   w w w.j av  a 2 s . c  om
        ZipEntry entry = null;
        String entryName$;
        while ((entry = zis.getNextEntry()) != null) {
            entryName$ = entry.getName();
            if (entryName$.equals(Entigrator.PROPERTY_INDEX)) {
                zis.close();
                return true;
            }
        }
        zis.close();
        return false;
    } catch (Exception e) {
        Logger.getLogger(ArchiveHandler.class.getName()).severe(e.toString());
        return false;
    }
}