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:gdt.data.entity.ArchiveHandler.java

private static boolean hasEntitiesDirInZipStream(ZipInputStream zis) {
    try {/*  w ww  .j a  v a2  s . c om*/
        ZipEntry entry = null;
        String entryName$;
        while ((entry = zis.getNextEntry()) != null) {
            entryName$ = entry.getName();
            if (entryName$.startsWith(Entigrator.ENTITY_BASE)) {
                zis.close();
                return true;
            }
        }
        zis.close();
        return false;
    } catch (Exception e) {
        Logger.getLogger(ArchiveHandler.class.getName()).severe(e.toString());
        return false;
    }
}

From source file:org.jenkinsci.plugins.fortifycloudscan.util.ArchiveUtil.java

public static void unzip(File directory, File zipFile) throws FileNotFoundException, IOException {
    if (!directory.exists()) {
        directory.mkdirs();/*from ww  w  .  j  a va  2s  .  com*/
    }
    byte[] buffer = new byte[2048];

    FileInputStream fInput = null;
    ZipInputStream zipInput = null;
    try {
        fInput = new FileInputStream(zipFile);
        zipInput = new ZipInputStream(fInput);
        ZipEntry entry = zipInput.getNextEntry();
        while (entry != null) {
            String entryName = entry.getName();
            File file = new File(directory.getAbsolutePath() + File.separator + entryName);
            if (entry.isDirectory()) {
                File newDir = new File(file.getAbsolutePath());
                if (!newDir.exists()) {
                    newDir.mkdirs();
                }
            } else {
                if (!file.getParentFile().isDirectory() && !file.getParentFile().exists()) {
                    file.getParentFile().mkdirs();
                }
                FileOutputStream fOutput = new FileOutputStream(file);
                int count;
                while ((count = zipInput.read(buffer)) > 0) {
                    fOutput.write(buffer, 0, count);
                }
                fOutput.close();
            }
            zipInput.closeEntry();
            entry = zipInput.getNextEntry();
        }
        zipInput.closeEntry();
        zipInput.close();
        fInput.close();
    } catch (FileNotFoundException e) {
        throw new FileNotFoundException(e.getMessage());
    } catch (IOException e) {
        throw new IOException(e);
    } finally {
        try {
            zipInput.closeEntry();
        } catch (IOException e) {
        }
        IOUtils.closeQuietly(zipInput);
        IOUtils.closeQuietly(fInput);
    }
}

From source file:org.dita2indesign.cmdline.Docx2Xml.java

/**
* @param zipInFile//from   w ww.  j a  v  a  2 s  .c o m
*            Zip file to be unzipped
* @param outputDir
*            Directory to which the zipped files will be unpacked.
* @throws Exception 
* @throws ZipException 
*/
public static void unzip(File zipInFile, File outputDir) throws Exception {

    Enumeration<? extends ZipEntry> entries;
    ZipFile zipFile = new ZipFile(zipInFile);
    ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipInFile));
    ZipEntry entry = (ZipEntry) zipInputStream.getNextEntry();

    File curOutDir = outputDir;
    while (entry != null) {

        if (entry.isDirectory()) {
            // This is not robust, just for demonstration purposes.
            curOutDir = new File(curOutDir, entry.getName());
            curOutDir.mkdirs();
            continue;
        }

        File outFile = new File(curOutDir, entry.getName());
        File tempDir = outFile.getParentFile();
        if (!tempDir.exists())
            tempDir.mkdirs();
        outFile.createNewFile();
        BufferedOutputStream outstream = new BufferedOutputStream(new FileOutputStream(outFile));

        int n;
        byte[] buf = new byte[1024];
        while ((n = zipInputStream.read(buf, 0, 1024)) > -1)
            outstream.write(buf, 0, n);
        outstream.flush();
        outstream.close();
        zipInputStream.closeEntry();
        entry = zipInputStream.getNextEntry();
    }
    zipInputStream.close();

    zipFile.close();
}

From source file:com.googlecode.dex2jar.reader.ZipExtractor.java

public byte[] extract(byte[] data, String name) throws IOException {
    ZipInputStream zis = null;
    try {/*www. j ava2 s. c o m*/
        zis = new ZipInputStreamHack(new ByteArrayInputStream(data));
        for (ZipEntry entry = zis.getNextEntry(); entry != null; entry = zis.getNextEntry()) {
            if (entry.getName().equals(name)) {
                data = IOUtils.toByteArray(zis);
                zis.close();
                return data;
            }
        }
    } finally {
        IOUtils.closeQuietly(zis);
    }
    throw new IOException("can't find classes.dex in the zip");
}

From source file:es.juntadeandalucia.panelGestion.negocio.utiles.Utils.java

public static InputStream getShapefileFromCompressed(InputStream is, ShpFileType type) {
    InputStream shapefile = null;
    ZipInputStream zis = new ZipInputStream(is);
    ZipEntry ze;/*www . ja v  a  2 s .com*/
    try {
        while ((ze = zis.getNextEntry()) != null) {
            if (!ze.isDirectory()) {
                String fileName = ze.getName().toLowerCase();
                String baseShapefileName = type.toBase(fileName);
                if (baseShapefileName != null) {
                    shapefile = zis;
                    break;
                }
            }
        }
    } catch (IOException e) {
        try {
            zis.closeEntry();
        } catch (IOException e2) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            zis.close();
        } catch (IOException e2) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return shapefile;
}

From source file:ZipUtil.java

public static void unZipZipFileToLocation(File zipFile, File targetDir) throws IOException {
    if (!targetDir.isDirectory()) {
        throw new Exception("Target is not a directory.");
    }/*from  w  w w.j  av  a 2s.c om*/
    FileInputStream flInStr = new FileInputStream(zipFile);
    try {
        ZipInputStream zis = new ZipInputStream(flInStr);
        try {
            ZipEntry entry = null;
            while ((entry = zis.getNextEntry()) != null) {
                String name = entry.getName();
                File newFile = new File(targetDir, name);
                if (entry.isDirectory() && !newFile.exists()) {
                    newFile.mkdirs();
                } else if (!entry.isDirectory()) {
                    if (newFile.exists()) {
                        newFile.delete();
                    }
                    File parentDir = newFile.getParentFile();
                    if (!parentDir.exists()) {
                        parentDir.mkdirs();
                    }
                    FileOutputStream stmOut = new FileOutputStream(newFile);
                    try {
                        simpleInputStreamToOutputStream(zis, stmOut);
                    } finally {
                        stmOut.close();
                    }
                }
            } //end while.
        } finally {
            zis.close();
        }
    } finally {
        flInStr.close();
    }
}

From source file:ZipTest.java

/**
 * Scans the contents of the ZIP archive and populates the combo box.
 *///from w ww  . java  2 s.c o  m
public void scanZipFile() {
    new SwingWorker<Void, String>() {
        protected Void doInBackground() throws Exception {
            ZipInputStream zin = new ZipInputStream(new FileInputStream(zipname));
            ZipEntry entry;
            while ((entry = zin.getNextEntry()) != null) {
                publish(entry.getName());
                zin.closeEntry();
            }
            zin.close();
            return null;
        }

        protected void process(List<String> names) {
            for (String name : names)
                fileCombo.addItem(name);

        }
    }.execute();
}

From source file:functionalTests.vfsprovider.TestProActiveProvider.java

private void setUpTestDir() throws URISyntaxException, IOException {
    // create dir 
    if (testDir.exists()) {
        removeTestDir();// ww w  .j  a v a2 s.  co m
    }
    Assert.assertFalse(testDir.exists());
    Assert.assertTrue(testDir.mkdirs());

    // extract files from archive with VFS provider test data
    final ZipInputStream zipInputStream = new ZipInputStream(
            new BufferedInputStream(TEST_DATA_SRC_ZIP_URL.openStream()));
    try {
        extractZip(zipInputStream, testDir);
    } finally {
        zipInputStream.close();
    }

    // set VFS tests property
    System.setProperty("test.basedir", testDir.getAbsolutePath());
}

From source file:org.flowerplatform.communication.public_resources.PublicResourcesServlet.java

protected Pair<InputStream, Closeable> getInputStreamForFileWithinZip(final InputStream fileInputStream,
        String fileWithinZip) throws IOException {
    final BufferedInputStream bis = new BufferedInputStream(fileInputStream, DEFAULT_BUFFER_SIZE);
    final ZipInputStream zis = new ZipInputStream(bis);

    Closeable closeable = new Closeable() {

        @Override/*from  w w w . j  ava2 s  .  c o  m*/
        public void close() throws IOException {
            zis.close();
            bis.close();
            fileInputStream.close();
        }
    };

    for (ZipEntry ze = zis.getNextEntry(); ze != null; ze = zis.getNextEntry()) {
        if (fileWithinZip.equals(ze.getName())) {
            return new Pair<InputStream, Closeable>(zis, closeable);
        }
    }

    closeable.close();
    return null;
}

From source file:com.cip.crane.agent.utils.FileExtractUtils.java

/**
 * Unzip an input file into an output file.
 * <p>//from  w  ww  . ja v  a 2  s.c  om
 * The output file is created in the output folder, having the same name
 * as the input file, minus the '.zip' extension. 
 * 
 * @param inputFile     the input .zip file
 * @param outputDir     the output directory file. 
 * @throws IOException 
 * @throws FileNotFoundException
 *  
 * @return  The {@File} with the ungzipped content.
 */
public static void unZip(final File inputFile, final File outputDir) throws FileNotFoundException, IOException {
    LOG.debug(
            String.format("Unzipping %s to dir %s.", inputFile.getAbsolutePath(), outputDir.getAbsolutePath()));

    byte[] buf = new byte[1024];
    ZipInputStream zipinputstream = null;
    ZipEntry zipentry;
    zipinputstream = new ZipInputStream(new FileInputStream(inputFile));

    zipentry = zipinputstream.getNextEntry();
    while (zipentry != null) {
        //for each entry to be extracted
        String entryName = outputDir + "/" + zipentry.getName();
        entryName = entryName.replace('/', File.separatorChar);
        entryName = entryName.replace('\\', File.separatorChar);
        int n;
        FileOutputStream fileoutputstream;
        File newFile = new File(entryName);

        if (zipentry.isDirectory()) {
            if (!newFile.mkdirs()) {
                break;
            }
            zipentry = zipinputstream.getNextEntry();
            continue;
        }
        fileoutputstream = new FileOutputStream(entryName);

        while ((n = zipinputstream.read(buf, 0, 1024)) > -1) {
            fileoutputstream.write(buf, 0, n);
        }

        fileoutputstream.close();
        zipinputstream.closeEntry();
        zipentry = zipinputstream.getNextEntry();

    } //while
    zipinputstream.close();
}