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:Main.java

public static void unZip(String path) {
    int count = -1;
    int index = -1;

    String savepath = "";

    savepath = path.substring(0, path.lastIndexOf("."));
    try {/* ww  w  . j av  a 2 s. c  o  m*/
        BufferedOutputStream bos = null;
        ZipEntry entry = null;
        FileInputStream fis = new FileInputStream(path);
        ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));

        while ((entry = zis.getNextEntry()) != null) {
            byte data[] = new byte[buffer];

            String temp = entry.getName();
            index = temp.lastIndexOf("/");
            if (index > -1)
                temp = temp.substring(index + 1);
            String tempDir = savepath + "/zip/";
            File dir = new File(tempDir);
            dir.mkdirs();
            temp = tempDir + temp;
            File f = new File(temp);
            f.createNewFile();

            FileOutputStream fos = new FileOutputStream(f);
            bos = new BufferedOutputStream(fos, buffer);

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

            bos.flush();
            bos.close();
        }

        zis.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:au.com.jwatmuff.eventmanager.util.ZipUtils.java

public static void unzipFile(File destFolder, File zipFile) throws IOException {
    ZipInputStream zipStream = null;
    try {/*from w  w  w .  j  a  v a2 s  . co m*/
        if (!destFolder.exists()) {
            destFolder.mkdirs();
        }

        BufferedInputStream in = new BufferedInputStream(new FileInputStream(zipFile));
        zipStream = new ZipInputStream(in);
        ZipEntry entry;
        while ((entry = zipStream.getNextEntry()) != null) {
            // get output file
            String name = entry.getName();
            if (name.startsWith("/") || name.startsWith("\\"))
                name = name.substring(1);
            File file = new File(destFolder, name);
            // ensure directory exists
            File dir = file.getParentFile();
            if (!dir.exists())
                dir.mkdirs();
            IOUtils.copy(zipStream, new FileOutputStream(file));
        }
    } finally {
        if (zipStream != null)
            zipStream.close();
    }
}

From source file:edu.clemson.cs.nestbed.common.util.ZipUtils.java

public static File unzip(byte[] zipData, File directory) throws IOException {
    ByteArrayInputStream bais = new ByteArrayInputStream(zipData);
    ZipInputStream zis = new ZipInputStream(bais);
    ZipEntry entry = zis.getNextEntry();
    File root = null;/*from   w w w.jav  a  2 s  .c  o m*/

    while (entry != null) {
        if (entry.isDirectory()) {
            File f = new File(directory, entry.getName());
            f.mkdir();

            if (root == null) {
                root = f;
            }
        } else {
            BufferedOutputStream out;
            out = new BufferedOutputStream(new FileOutputStream(new File(directory, entry.toString())),
                    BUFFER_SIZE);

            // ZipInputStream can only give us one byte at a time...
            for (int data = zis.read(); data != -1; data = zis.read()) {
                out.write(data);
            }
            out.close();
        }

        zis.closeEntry();
        entry = zis.getNextEntry();
    }
    zis.close();

    return root;
}

From source file:org.adl.samplerte.server.LMSPackageHandler.java

/****************************************************************************
**
** Method:   findManifest()/*ww w. j a  v  a 2s  .  co  m*/
** Input:  String zipFileName  --  The name of the zip file to be used
** Output:   Boolean  --  Signifies whether or not the manifest was found.
**
** Description: This method takes in the name of a zip file and tries to 
**              locate the imsmanifest.xml file
**              
*****************************************************************************/
public static boolean findManifest(String zipFileName) {
    if (_Debug) {
        System.out.println("***********************");
        System.out.println("in findManifest()      ");
        System.out.println("***********************\n");
    }

    boolean rtn = false;

    try {
        ZipInputStream in = new ZipInputStream(new FileInputStream(zipFileName));

        ZipEntry entry;
        int flag = 0;

        while ((flag != 1) && (in.available() != 0)) {
            entry = in.getNextEntry();

            if (in.available() != 0) {
                if ((entry.getName()).equalsIgnoreCase("imsmanifest.xml")) {
                    if (_Debug) {
                        System.out.println("Located manifest.... returning true");
                    }
                    flag = 1;
                    rtn = true;
                }
            }
        }

        in.close();
    } catch (IOException e) {
        if (_Debug) {
            System.out.println("IO Exception Caught: " + e);
        }
    }
    return rtn;
}

From source file:Main.java

/** Returns true if these zip files act like equivalent sets.
 * The order of the zip entries is not important: if they contain
 * exactly the same contents, this returns true.
 * @param zip1 one zip file //from   w  w w.jav a  2s.  c  o  m
 * @param zip2 another zip file
 * @return true if the two zip archives are equivalent sets
 * @throws IOException
 */
public static boolean zipEquals(File zip1, File zip2) throws IOException {
    if (zip1.equals(zip2))
        return true;

    InputStream in = null;
    ZipInputStream zipIn = null;
    try {
        in = new FileInputStream(zip1);
        zipIn = new ZipInputStream(in);
        ZipEntry e = zipIn.getNextEntry();
        Vector<String> entries = new Vector<String>();
        while (e != null) {
            entries.add(e.getName());

            InputStream other = getZipEntry(zip2, e.getName());
            if (other == null) {
                return false;
            }

            if (equals(zipIn, other) == false) {
                return false;
            }
            e = zipIn.getNextEntry();
        }
        //now we've established everything in zip1 is in zip2

        //but what if zip2 has entries zip1 doesn't?
        zipIn.close();
        in.close();

        in = new FileInputStream(zip2);
        zipIn = new ZipInputStream(in);
        e = zipIn.getNextEntry();
        while (e != null) {
            if (entries.contains(e.getName()) == false) {
                return false;
            }
            e = zipIn.getNextEntry();
        }

        //the entries are exactly the same
        return true;
    } finally {
        try {
            zipIn.close();
        } catch (Throwable t) {
        }
        try {
            in.close();
        } catch (Throwable t) {
        }
    }
}

From source file:Main.java

/**
 * Extract a zip resource into real files and directories
 * /*from  w ww.j av  a 2s  . 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:gabi.FileUploadServlet.java

private static void unzip(String zipFilePath, String destDir) {
    File dir = new File(destDir);
    // create output directory if it doesn't exist
    if (!dir.exists())
        dir.mkdirs();//from  w  ww  . j av  a2 s .c  o m
    FileInputStream fis;
    //buffer for read and write data to file
    byte[] buffer = new byte[1024];
    try {
        fis = new FileInputStream(zipFilePath);
        ZipInputStream zis = new ZipInputStream(fis);
        ZipEntry ze = zis.getNextEntry();
        while (ze != null) {
            String fileName = ze.getName();
            File newFile = new File(destDir + File.separator + fileName);
            System.out.println("Unzipping to " + newFile.getAbsolutePath());
            //create directories for sub directories in zip
            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();
            //close this ZipEntry
            zis.closeEntry();
            ze = zis.getNextEntry();
        }
        //close last ZipEntry
        zis.closeEntry();
        zis.close();
        fis.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:JarMaker.java

/**
 * @param f_name : source zip file path name
 * @param dir_name : target dir file path
 *///from www  .j a  v a 2s . c  o m
public static void unpackJar(String f_name, String dir_name) throws IOException {

    BufferedInputStream bism = new BufferedInputStream(new FileInputStream(f_name));

    ZipInputStream zism = new ZipInputStream(bism);

    for (ZipEntry z = zism.getNextEntry(); z != null; z = zism.getNextEntry()) {
        File f = new File(dir_name, z.getName());
        if (z.isDirectory()) {
            f.mkdirs();
        } else {
            f.getParentFile().mkdirs();

            BufferedOutputStream bosm = new BufferedOutputStream(new FileOutputStream(f));
            int i;
            byte[] buffer = new byte[1024 * 512];
            try {
                while ((i = zism.read(buffer, 0, buffer.length)) != -1)
                    bosm.write(buffer, 0, i);
            } catch (IOException ie) {
                throw ie;
            }
            bosm.close();
        }
    }
    zism.close();
    bism.close();
}

From source file:Main.java

public static void unzip(final InputStream input, final File destFolder) {
    try {//from w  w w. j a v a 2  s .c om
        byte[] buffer = new byte[4096];
        int read;
        ZipInputStream is = new ZipInputStream(input);
        ZipEntry entry;
        while ((entry = is.getNextEntry()) != null) {
            if (!entry.isDirectory()) {
                String fileName = entry.getName();
                File fileFolder = destFolder;
                int lastSep = entry.getName().lastIndexOf(File.separatorChar);
                if (lastSep != -1) {
                    String dirPath = fileName.substring(0, lastSep);
                    fileFolder = new File(fileFolder, dirPath);
                    fileName = fileName.substring(lastSep + 1);
                }
                fileFolder.mkdirs();
                File file = new File(fileFolder, fileName);
                FileOutputStream os = new FileOutputStream(file);
                while ((read = is.read(buffer)) != -1) {
                    os.write(buffer, 0, read);
                }
                os.flush();
                os.close();
            }
        }
        is.close();
    } catch (Exception ex) {
        throw new RuntimeException("Cannot unzip stream to " + destFolder.getAbsolutePath(), ex);
    }
}

From source file:Main.java

public static void unzip(InputStream fin, String targetPath, Context context) throws IOException {
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fin));
    try {//w w  w .j a  va2s  . c om
        ZipEntry zipEntry;
        int count;
        byte[] buffer = new byte[8192];
        while ((zipEntry = zis.getNextEntry()) != null) {
            File file = new File(targetPath, zipEntry.getName());
            File dir = zipEntry.isDirectory() ? file : file.getParentFile();
            if (!dir.isDirectory() && !dir.mkdirs()) {
                throw new FileNotFoundException("Failed to get directory: " + dir.getAbsolutePath());
            }
            if (zipEntry.isDirectory()) {
                continue;
            }
            FileOutputStream fout = new FileOutputStream(file);
            try {
                while ((count = zis.read(buffer)) != -1)
                    fout.write(buffer, 0, count);
            } finally {
                fout.close();
            }
            Log.d("TEST", "Unzipped " + file.getAbsolutePath());
        }
    } finally {
        zis.close();
    }
}