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:org.arquillian.warp.ftest.installation.ContainerInstaller.java

private void unzip(InputStream inputStream, File destination, boolean overwrite) {
    try {//from  www. j av  a 2s  . com
        byte[] buf = new byte[1024];
        ZipInputStream zipinputstream = null;
        ZipEntry zipentry;
        zipinputstream = new ZipInputStream(inputStream);

        zipentry = zipinputstream.getNextEntry();
        while (zipentry != null) {
            int n;
            FileOutputStream fileoutputstream;
            File newFile = new File(destination, zipentry.getName());
            if (zipentry.isDirectory()) {
                newFile.mkdirs();
                zipentry = zipinputstream.getNextEntry();
                continue;
            }

            if (newFile.exists() && overwrite) {
                log.info("Overwriting " + newFile);
                newFile.delete();
            }

            fileoutputstream = new FileOutputStream(newFile);

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

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

        }

        zipinputstream.close();
    } catch (Exception e) {
        throw new IllegalStateException("Can't unzip input stream", e);
    }
}

From source file:org.ktunaxa.referral.server.mvc.UploadGeometryController.java

private URL unzipShape(byte[] fileContent) throws IOException {
    String tempDir = System.getProperty("java.io.tmpdir");

    URL url = null;/*from   www .  j  av a2  s . c om*/
    ZipInputStream zin = new ZipInputStream(new ByteArrayInputStream(fileContent));
    try {
        ZipEntry entry;
        while ((entry = zin.getNextEntry()) != null) {
            log.info("Extracting: " + entry);
            String name = tempDir + "/" + entry.getName();
            tempFiles.add(name);
            if (name.endsWith(".shp")) {
                url = new URL("file://" + name);
            }
            int count;
            byte[] data = new byte[BUFFER];
            // write the files to the disk
            deleteFileIfExists(name);
            FileOutputStream fos = new FileOutputStream(name);
            BufferedOutputStream destination = new BufferedOutputStream(fos, BUFFER);
            try {
                while ((count = zin.read(data, 0, BUFFER)) != -1) {
                    destination.write(data, 0, count);
                }
                destination.flush();
            } finally {
                destination.close();
            }
        }
    } finally {
        zin.close();
    }
    if (url == null) {
        throw new IllegalArgumentException("Missing .shp file");
    }
    return url;
}

From source file:org.geoserver.wfs.response.ShapeZipTest.java

/**
 * Asserts the contents for the file named {@code fileName} contained in the zip file given by the {@code zippedIn}
 * matched the {@code expectedContent}/*from   ww w .  j  a v  a2  s .c o  m*/
 */
private void checkFileContent(final String fileName, final InputStream zippedIn, final String expectedContent)
        throws IOException {

    ZipInputStream zis = new ZipInputStream(zippedIn);
    ZipEntry entry = null;
    try {
        while ((entry = zis.getNextEntry()) != null) {
            try {
                final String name = entry.getName();
                if (name.toLowerCase().endsWith(fileName.toLowerCase())) {
                    String unzippedFileContents = IOUtils.toString(zis);
                    assertEquals(expectedContent, unzippedFileContents);
                    return;
                }
            } finally {
                zis.closeEntry();
            }
        }
    } finally {
        zis.close();
    }
    fail(fileName + " was not found in the provided stream");
}

From source file:org.apache.axis2.deployment.repository.util.ArchiveReader.java

public void readModuleArchive(DeploymentFileData deploymentFile, AxisModule module, boolean explodedDir,
        AxisConfiguration axisConfig) throws DeploymentException {

    // get attribute values
    boolean moduleXMLFound = false;
    String shortFileName = DescriptionBuilder.getShortFileName(deploymentFile.getName());
    if (!explodedDir) {
        ZipInputStream zin;
        FileInputStream fin;/* ww  w.  j  ava2 s .  c  om*/
        try {
            fin = new FileInputStream(deploymentFile.getAbsolutePath());
            zin = new ZipInputStream(fin);
            ZipEntry entry;
            while ((entry = zin.getNextEntry()) != null) {
                if (entry.getName().equalsIgnoreCase(MODULE_XML)) {
                    moduleXMLFound = true;
                    ModuleBuilder builder = new ModuleBuilder(zin, module, axisConfig);
                    // setting module name and version
                    module.setArchiveName(shortFileName);
                    builder.populateModule();
                    break;
                }
            }
            zin.close();
            fin.close();
            if (!moduleXMLFound) {
                throw new DeploymentException(Messages.getMessage(DeploymentErrorMsgs.MODULE_XML_MISSING,
                        deploymentFile.getAbsolutePath()));
            }
        } catch (Exception e) {
            throw new DeploymentException(e);
        }
    } else {
        File file = new File(deploymentFile.getAbsolutePath(), MODULE_XML);

        if (file.exists()
                || (file = new File(deploymentFile.getAbsolutePath(), MODULE_XML.toLowerCase())).exists()) {
            InputStream in = null;
            try {
                in = new FileInputStream(file);
                ModuleBuilder builder = new ModuleBuilder(in, module, axisConfig);
                // setting module name and version
                module.setArchiveName(shortFileName);
                builder.populateModule();
            } catch (FileNotFoundException e) {
                throw new DeploymentException(
                        Messages.getMessage(DeploymentErrorMsgs.FILE_NOT_FOUND, e.getMessage()));
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        log.info(Messages.getMessage("errorininputstreamclose"));
                    }
                }
            }
        } else {
            throw new DeploymentException(Messages.getMessage(DeploymentErrorMsgs.MODULE_XML_MISSING,
                    deploymentFile.getAbsolutePath()));
        }
    }
}

From source file:org.apache.stratos.python.cartridge.agent.integration.tests.PythonAgentIntegrationTest.java

private void unzip(String zipFilePath, String destDirectory) throws IOException {
    File destDir = new File(destDirectory);
    if (!destDir.exists()) {
        destDir.mkdir();//from   w  ww  . j a v  a 2  s  .c  o m
    }
    ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
    ZipEntry entry = zipIn.getNextEntry();
    // iterates over entries in the zip file
    while (entry != null) {
        String filePath = destDirectory + File.separator + entry.getName();
        if (!entry.isDirectory()) {
            // if the entry is a file, extracts it
            extractFile(zipIn, filePath);
        } else {
            // if the entry is a directory, make the directory
            File dir = new File(filePath);
            dir.mkdir();
        }
        zipIn.closeEntry();
        entry = zipIn.getNextEntry();
    }
    zipIn.close();
}

From source file:com.github.nethad.clustermeister.integration.JPPFTestNode.java

private void unzipNode(InputStream fileToUnzip, File targetDir) {
    //        Enumeration entries;
    ZipInputStream zipFile;
    try {/*from www.  j av  a2s.c  o m*/
        zipFile = new ZipInputStream(fileToUnzip);
        ZipEntry entry;
        while ((entry = zipFile.getNextEntry()) != null) {
            //                ZipEntry entry = (ZipEntry) entries.nextElement();
            if (entry.isDirectory()) {
                // Assume directories are stored parents first then children.
                System.err.println("Extracting directory: " + entry.getName());
                // This is not robust, just for demonstration purposes.
                (new File(targetDir, entry.getName())).mkdir();
                continue;
            }
            System.err.println("Extracting file: " + entry.getName());
            File targetFile = new File(targetDir, entry.getName());
            copyInputStream_notClosing(zipFile, new BufferedOutputStream(new FileOutputStream(targetFile)));
            //                zipFile.closeEntry();
        }
        zipFile.close();
    } catch (IOException ioe) {
        System.err.println("Unhandled exception:");
        ioe.printStackTrace();
    }
}

From source file:es.juntadeandalucia.panelGestion.negocio.utiles.file.shape.LocalShapeReader.java

private void unzipShapefiles(File shapefileCompressedData) throws IOException {
    IOException error = null;//from w ww. j  a v  a2  s. co  m
    if (!shapefilesFolder.exists()) {
        shapefilesFolder.mkdirs();
    }
    InputStream bis = new FileInputStream(shapefileCompressedData);
    ZipInputStream zis = new ZipInputStream(bis);
    ZipEntry ze;
    try {
        while ((ze = zis.getNextEntry()) != null) {
            if (!ze.isDirectory()) {
                String fileName = ze.getName();
                File unzippedFile = new File(shapefilesFolder, fileName);
                byte[] buffer = new byte[1024];
                FileOutputStream fos = new FileOutputStream(unzippedFile);
                int len;
                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }
                fos.close();
                if (fileName.toLowerCase().endsWith(".shp")) {
                    shapefilePath = unzippedFile.toURI();
                }
            }
        }
    } catch (IOException e) {
        error = e;
    } finally {
        try {
            zis.closeEntry();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            zis.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    if (error != null) {
        throw error;
    }
}

From source file:org.jahia.utils.zip.JahiaArchiveFileHandler.java

/**
 * Decompresses the file in it's current location
 *///ww w.j  av  a 2  s  .  co  m
public void unzip() throws JahiaException {

    try {

        File f = new File(m_FilePath);

        //JahiaConsole.println(CLASS_NAME + ".upzip"," Start Decompressing " + f.getName() );

        String parentPath = f.getParent() + File.separator;
        String path = null;

        FileInputStream fis = new FileInputStream(m_FilePath);
        BufferedInputStream bis = new BufferedInputStream(fis);
        ZipInputStream zis = new ZipInputStream(bis);
        ZipFile zf = new ZipFile(m_FilePath);
        ZipEntry ze = null;
        String zeName = null;

        try {

            while ((ze = zis.getNextEntry()) != null) {
                zeName = ze.getName();
                path = parentPath + genPathFile(zeName);
                File fo = new File(path);

                if (ze.isDirectory()) {
                    fo.mkdirs();
                } else {
                    copyStream(zis, new FileOutputStream(fo));
                }
                zis.closeEntry();
            }
        } finally {

            // Important !!!
            zf.close();
            fis.close();
            zis.close();
            bis.close();
        }

        //JahiaConsole.println(CLASS_NAME+".unzip"," Decompressing " + f.getName() + " done ! ");

    } catch (IOException ioe) {

        logger.error(" fail unzipping " + ioe.getMessage(), ioe);

        throw new JahiaException("JahiaArchiveFileHandler", "faile processing unzip",
                JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY, ioe);

    }

}

From source file:org.geon.XMLToADN.java

public static String unzip(java.io.File dst, java.io.File zip) throws Exception {

    System.out.println("dst folder = " + dst.getAbsolutePath() + " , zip file = " + zip.getAbsolutePath());
    int BUFFER = 2048;
    BufferedOutputStream dest = null;
    FileInputStream fis = new FileInputStream(zip);
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
    ZipEntry entry;/*from  ww  w .  j a v a  2 s . co  m*/
    String name = null;
    boolean error = false;
    while ((entry = zis.getNextEntry()) != null) {

        if (entry.isDirectory()) {
            continue;
        }

        String tmp = entry.getName();
        String nm = tmp;

        int index = tmp.lastIndexOf("/");
        if (index != -1) {
            tmp = tmp.substring(index + 1);
            nm = tmp;
        }

        if (tmp.endsWith("dbf") || tmp.endsWith("shp") || tmp.endsWith("shx")) {

            tmp = tmp.substring(0, tmp.length() - 4);
            if (name == null) {
                name = tmp;
            } else if (name.equals(tmp)) {

            } else {
                continue;
            }
        }

        int count;
        byte data[] = new byte[BUFFER];

        // write the files to the disk
        java.io.File file = new java.io.File(dst, nm);
        FileOutputStream fos = new FileOutputStream(file);
        dest = new BufferedOutputStream(fos, BUFFER);
        while ((count = zis.read(data, 0, BUFFER)) != -1) {
            dest.write(data, 0, count);
        }
        dest.flush();
        dest.close();
    }
    zis.close();
    fis.close();

    return name;

}

From source file:com.taobao.android.builder.tools.zip.ZipUtils.java

/**
 * zip//w w w .j  a  v  a  2  s  .  c  o m
 *
 * @param zipFile
 * @param file
 * @param destPath
 * @param overwrite ?
 * @throws java.io.IOException
 */
public static void addFileToZipFile(File zipFile, File outZipFile, File file, String destPath,
        boolean overwrite) throws IOException {
    byte[] buf = new byte[1024];
    ZipInputStream zin = new ZipInputStream(new FileInputStream(zipFile));
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outZipFile));
    ZipEntry entry = zin.getNextEntry();
    boolean addFile = true;
    while (entry != null) {
        boolean addEntry = true;
        String name = entry.getName();
        if (StringUtils.equalsIgnoreCase(name, destPath)) {
            if (overwrite) {
                addEntry = false;
            } else {
                addFile = false;
            }
        }
        if (addEntry) {
            ZipEntry zipEntry = null;
            if (STORED == entry.getMethod()) {
                zipEntry = new ZipEntry(entry);
            } else {
                zipEntry = new ZipEntry(name);
            }
            out.putNextEntry(zipEntry);
            int len;
            while ((len = zin.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        }
        entry = zin.getNextEntry();
    }

    if (addFile) {
        InputStream in = new FileInputStream(file);
        // Add ZIP entry to output stream.
        ZipEntry zipEntry = new ZipEntry(destPath);
        out.putNextEntry(zipEntry);
        // Transfer bytes from the file to the ZIP file
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        // Complete the entry
        out.closeEntry();
        in.close();
    }
    // Close the streams
    zin.close();
    out.close();
}