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:nl.nn.adapterframework.util.FileUtils.java

public static void unzipStream(InputStream inputStream, File dir) throws IOException {
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(inputStream));
    try {/*from  w ww  .  j a  va 2s .c  o  m*/
        ZipEntry ze;
        while ((ze = zis.getNextEntry()) != null) {
            String filename = ze.getName();
            File zipFile = new File(dir, filename);
            if (ze.isDirectory()) {
                if (!zipFile.exists()) {
                    log.debug("creating directory [" + zipFile.getPath() + "] for ZipEntry [" + ze.getName()
                            + "]");
                    if (!zipFile.mkdir()) {
                        throw new IOException(zipFile.getPath() + " could not be created");
                    }
                }
            } else {
                File zipParentFile = zipFile.getParentFile();
                if (!zipParentFile.exists()) {
                    log.debug("creating directory [" + zipParentFile.getPath() + "] for ZipEntry ["
                            + ze.getName() + "]");
                    if (!zipParentFile.mkdir()) {
                        throw new IOException(zipParentFile.getPath() + " could not be created");
                    }
                }
                FileOutputStream fos = new FileOutputStream(zipFile);
                log.debug("writing ZipEntry [" + ze.getName() + "] to file [" + zipFile.getPath() + "]");
                Misc.streamToStream(zis, fos, false);
                fos.close();
            }
        }
    } finally {
        try {
            zis.close();
        } catch (IOException e) {
            log.warn("exception closing unzip", e);
        }
    }
}

From source file:com.joliciel.csvLearner.CSVLearner.java

private void doCommandWriteModelToCSV() throws IOException {
    if (maxentModelFilePath == null)
        throw new RuntimeException("Missing argument: maxentModel");

    LOG.info("Evaluating test events...");
    ZipInputStream zis = new ZipInputStream(new FileInputStream(maxentModelFilePath));
    ZipEntry ze;//from   w  w w  .  ja v a  2 s .c o  m
    while ((ze = zis.getNextEntry()) != null) {
        if (ze.getName().endsWith(".bin"))
            break;
    }
    MaxentModel model = new MaxentModelReader(zis).getModel();
    zis.close();

    String csvFilePath = maxentModelFilePath + ".model.csv";

    MaxEntModelCSVWriter writer = new MaxEntModelCSVWriter();
    writer.setModel(model);
    writer.setCsvFilePath(csvFilePath);
    writer.setTop100(top100);
    writer.writeCSVFile();
}

From source file:net.sf.sveditor.core.tests.utils.BundleUtils.java

public void unpackBundleZipToFS(String bundle_path, File fs_path) {
    URL zip_url = fBundle.getEntry(bundle_path);
    TestCase.assertNotNull(zip_url);//  w ww .  j a  v  a  2  s .co  m

    if (!fs_path.isDirectory()) {
        TestCase.assertTrue(fs_path.mkdirs());
    }

    try {
        InputStream in = zip_url.openStream();
        TestCase.assertNotNull(in);
        byte tmp[] = new byte[4 * 1024];
        int cnt;

        ZipInputStream zin = new ZipInputStream(in);
        ZipEntry ze;

        while ((ze = zin.getNextEntry()) != null) {
            // System.out.println("Entry: \"" + ze.getName() + "\"");
            File entry_f = new File(fs_path, ze.getName());
            if (ze.getName().endsWith("/")) {
                // Directory
                continue;
            }
            if (!entry_f.getParentFile().exists()) {
                TestCase.assertTrue(entry_f.getParentFile().mkdirs());
            }
            FileOutputStream fos = new FileOutputStream(entry_f);
            BufferedOutputStream bos = new BufferedOutputStream(fos, tmp.length);

            while ((cnt = zin.read(tmp, 0, tmp.length)) > 0) {
                bos.write(tmp, 0, cnt);
            }
            bos.flush();
            bos.close();
            fos.close();

            zin.closeEntry();
        }
        zin.close();
    } catch (IOException e) {
        e.printStackTrace();
        TestCase.fail("Failed to unpack zip file: " + e.getMessage());
    }
}

From source file:io.sledge.core.impl.extractor.SledgeApplicationPackageExtractor.java

@Override
public Map<String, InputStream> getPackages(ApplicationPackage appPackage) {
    Map<String, InputStream> packages = new HashMap<>();
    ZipInputStream zipStream = getNewUtf8ZipInputStream(appPackage);

    try {//from   w  w w  .  ja  v a  2 s .  com
        byte[] buffer = new byte[2048];
        ZipEntry zipEntry = null;

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

            if (zipEntry.isDirectory()) {
                zipStream.closeEntry();
                continue;
            }

            if (zipEntry.getName().startsWith("packages/")) {
                ByteArrayOutputStream output = new ByteArrayOutputStream();

                int length;
                while ((length = zipStream.read(buffer, 0, buffer.length)) >= 0) {
                    output.write(buffer, 0, length);
                }

                String packageFileName = zipEntry.getName().replace("packages/", "");
                packages.put(packageFileName, new ByteArrayInputStream(output.toByteArray()));

                zipStream.closeEntry();
            }
        }
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    } finally {
        try {
            zipStream.close();
            appPackage.getPackageFile().reset();
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }

    return packages;
}

From source file:nl.nn.adapterframework.webcontrol.pipes.TestIfsaService.java

private String processZipFile(IPipeLineSession session, InputStream inputStream, String fileEncoding,
        String applicationId, String serviceId, String messageProtocol) throws IOException {
    String result = "";
    String lastState = null;/*from  w  ww.  ja v  a 2  s . co  m*/
    ZipInputStream archive = new ZipInputStream(inputStream);
    for (ZipEntry entry = archive.getNextEntry(); entry != null; entry = archive.getNextEntry()) {
        String name = entry.getName();
        int size = (int) entry.getSize();
        if (size > 0) {
            byte[] b = new byte[size];
            int rb = 0;
            int chunk = 0;
            while (((int) size - rb) > 0) {
                chunk = archive.read(b, rb, (int) size - rb);
                if (chunk == -1) {
                    break;
                }
                rb += chunk;
            }
            String message = XmlUtils.readXml(b, 0, rb, fileEncoding, false);
            if (StringUtils.isNotEmpty(result)) {
                result += "\n";
            }
            try {
                processMessage(applicationId, serviceId, messageProtocol, message);
                lastState = "success";
            } catch (Exception e) {
                lastState = "error";
            }
            result += name + ":" + lastState;
        }
        archive.closeEntry();
    }
    archive.close();
    session.put("result", result);
    return "";
}

From source file:link.kjr.file_manager.MainActivity.java

public void decompressZipFile(String path) throws IOException {
    FileInputStream fis = new FileInputStream(path);
    ZipInputStream zis = new ZipInputStream(fis);
    ZipEntry ze = zis.getNextEntry();
    while (ze != null) {

        File f = new File(currentPath + "/" + ze.getName());

        File parentfile = f.getParentFile();
        parentfile.mkdirs();/*from  w w w .j ava 2  s  .c o m*/

        if (!f.exists()) {
            f.createNewFile();
        }
        FileOutputStream fos = new FileOutputStream(currentPath + "/" + ze.getName());
        byte[] data = new byte[1024];
        int length = 0;
        length = zis.read(data, 0, 1024);
        fos.write(data, 0, length);
        fos.close();
        zis.closeEntry();
        ze = zis.getNextEntry();
    }
    zis.close();
    fis.close();
}

From source file:it.geosolutions.tools.compress.file.Extractor.java

public static void unZip(File inputZipFile, File outputDir) throws IOException, CompressorException {
    if (inputZipFile == null || outputDir == null) {
        throw new CompressorException("Unzip: with null parameters");
    }/*w w  w  .  j a v a  2  s  .  c o m*/

    if (!outputDir.exists()) {
        if (!outputDir.mkdirs()) {
            throw new CompressorException("Unzip: Unable to create directory structure: " + outputDir);
        }
    }

    final int BUFFER = Conf.getBufferSize();

    ZipInputStream zipInputStream = null;
    try {
        // Open Zip file for reading
        zipInputStream = new ZipInputStream(new FileInputStream(inputZipFile));
    } catch (FileNotFoundException fnf) {
        throw new CompressorException("Unzip: Unable to find the input zip file named: " + inputZipFile);
    }

    // extract file if not a directory
    BufferedInputStream bis = new BufferedInputStream(zipInputStream);
    // grab a zip file entry
    ZipEntry entry = null;
    while ((entry = zipInputStream.getNextEntry()) != null) {
        // Process each entry

        File currentFile = new File(outputDir, entry.getName());

        FileOutputStream fos = null;
        BufferedOutputStream dest = null;
        try {
            int currentByte;
            // establish buffer for writing file
            byte data[] = new byte[BUFFER];

            // write the current file to disk
            fos = new FileOutputStream(currentFile);
            dest = new BufferedOutputStream(fos, BUFFER);

            // read and write until last byte is encountered
            while ((currentByte = bis.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, currentByte);
            }
        } catch (IOException ioe) {

        } finally {
            try {
                if (dest != null) {
                    dest.flush();
                    dest.close();
                }
                if (fos != null)
                    fos.close();
            } catch (IOException ioe) {
                throw new CompressorException(
                        "Unzip: unable to close the zipInputStream: " + ioe.getLocalizedMessage());
            }
        }
    }
    try {
        if (zipInputStream != null)
            zipInputStream.close();
    } catch (IOException ioe) {
        throw new CompressorException(
                "Unzip: unable to close the zipInputStream: " + ioe.getLocalizedMessage());
    }
    try {
        if (bis != null)
            bis.close();
    } catch (IOException ioe) {
        throw new CompressorException(
                "Unzip: unable to close the Buffered zipInputStream: " + ioe.getLocalizedMessage());
    }
}

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

private SimpleFeatureType checkFieldsAreNotEmpty(InputStream in) throws IOException {
    ZipInputStream zis = new ZipInputStream(in);
    ZipEntry entry = null;//from w  w w  .j a va 2 s. c  o  m

    File tempFolder = createTempFolder("shp_");
    String shapeFileName = "";
    while ((entry = zis.getNextEntry()) != null) {
        final String name = entry.getName();
        String outName = tempFolder.getAbsolutePath() + File.separatorChar + name;
        // store .shp file name
        if (name.toLowerCase().endsWith("shp"))
            shapeFileName = outName;
        // copy each file to temp folder

        FileOutputStream outFile = new FileOutputStream(outName);
        copyStream(zis, outFile);
        outFile.close();
        zis.closeEntry();
    }
    zis.close();

    // create a datastore reading the uncompressed shapefile
    File shapeFile = new File(shapeFileName);
    ShapefileDataStore ds = new ShapefileDataStore(shapeFile.toURL());
    SimpleFeatureSource fs = ds.getFeatureSource();
    SimpleFeatureCollection fc = fs.getFeatures();
    SimpleFeatureType schema = fc.getSchema();

    SimpleFeatureIterator iter = fc.features();
    try {
        // check that every field has a not null or "empty" value
        while (iter.hasNext()) {
            SimpleFeature f = iter.next();
            for (Object attrValue : f.getAttributes()) {
                assertNotNull(attrValue);
                if (Geometry.class.isAssignableFrom(attrValue.getClass()))
                    assertFalse("Empty geometry", ((Geometry) attrValue).isEmpty());
                else
                    assertFalse("Empty value for attribute", attrValue.toString().trim().equals(""));
            }

        }
    } finally {
        iter.close();
        tempFolder.delete();
    }

    return schema;

}

From source file:org.apache.axis2.deployment.util.Utils.java

/**
 * Scan a JAR file and return the list of classes contained in the JAR.
 * /*  w  w w  .jav a  2  s.  co  m*/
 * @param deploymentFileData
 *            the JAR to scan
 * @return a list of Java class names
 * @throws DeploymentException
 *             if an error occurs while scanning the file
 */
public static List<String> getListOfClasses(DeploymentFileData deploymentFileData) throws DeploymentException {
    try {
        FileInputStream fin = new FileInputStream(deploymentFileData.getAbsolutePath());
        try {
            ZipInputStream zin = new ZipInputStream(fin);
            try {
                ZipEntry entry;
                List<String> classList = new ArrayList<String>();
                while ((entry = zin.getNextEntry()) != null) {
                    String name = entry.getName();
                    if (name.endsWith(".class")) {
                        classList.add(getClassNameFromResourceName(name));
                    }
                }
                return classList;
            } finally {
                zin.close();
            }
        } finally {
            fin.close();
        }
    } catch (IOException e) {
        log.debug(Messages.getMessage(DeploymentErrorMsgs.DEPLOYING_EXCEPTION, e.getMessage()), e);
        throw new DeploymentException(e);
    }
}

From source file:com.netflix.ice.processor.BillingFileProcessor.java

private void processBillingZipFile(File file, boolean withTags) throws IOException {

    InputStream input = new FileInputStream(file);
    ZipInputStream zipInput;

    zipInput = new ZipInputStream(input);

    try {//from  ww w  . jav  a 2  s .  c  o m
        ZipEntry entry;
        while ((entry = zipInput.getNextEntry()) != null) {
            if (entry.isDirectory())
                continue;

            processBillingFile(entry.getName(), zipInput, withTags);
        }
    } catch (IOException e) {
        if (e.getMessage().equals("Stream closed"))
            logger.info("reached end of file.");
        else
            logger.error("Error processing " + file, e);
    } finally {
        try {
            zipInput.close();
        } catch (IOException e) {
            logger.error("Error closing " + file, e);
        }
        try {
            input.close();
        } catch (IOException e1) {
            logger.error("Cannot close input for " + file, e1);
        }
    }
}