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.bonitasoft.console.common.server.utils.FormsResourcesUtils.java

private static void unzipContentToFolder(final byte[] zipContent, final File targetFolder) throws IOException {
    ByteArrayInputStream is = null;
    ZipInputStream zis = null;
    FileOutputStream out = null;/*from   w w w  .  j av a  2 s .c  o  m*/
    try {
        is = new ByteArrayInputStream(zipContent);
        zis = new ZipInputStream(is);
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            final String entryName = entry.getName();
            if (entryName.endsWith(".jar")) {
                final File file = new File(targetFolder, entryName);
                if (file.exists()) {
                    file.delete();
                }
                file.createNewFile();
                out = new FileOutputStream(file);
                int len = 0;
                final byte[] buffer = new byte[1024];
                while ((len = zis.read(buffer)) > 0) {
                    out.write(buffer, 0, len);
                }
                out.close();
            }
        }
    } finally {
        if (is != null) {
            is.close();
        }
        if (zis != null) {
            zis.close();
        }
        if (out != null) {
            out.close();
        }
    }
}

From source file:com.alcatel_lucent.nz.wnmsextract.reader.FileSelector.java

/**
 * The nested unzip method. Given a zip stream, decompress and store in file in temp_dir.
 * Replaced by unzip3.//w w  w .  j  a v a2 s  . c  o  m
 */
protected File unzip2(File zf) throws FileNotFoundException {
    //File f = null;
    String rename = zf.getAbsolutePath().replaceFirst("\\.zip", identifier + ".xml");//.replaceFirst("\\.gz", ".xml");
    File f = new File(rename);
    try {
        FileInputStream fis = new FileInputStream(zf);
        FileOutputStream fos = new FileOutputStream(rename);
        ZipInputStream zin = new ZipInputStream(fis);
        final byte[] content = new byte[BUFFER];
        int n = 0;
        while (-1 != (n = zin.read(content))) {
            fos.write(content, 0, n);
        }

        fos.flush();
        fos.close();

        fis.close();
        zin.close();
    } catch (IOException ioe) {
        jlog.error("Error processing Zip " + zf + " Excluding! :: " + ioe);
        return null;
    }
    //try again... what could go wrong
    if (checkMinFileSize(f) && retry_counter < MAX_UNGZIP_RETRIES) {
        retry_counter++;
        f.delete();
        f = unzip2(zf);
    }
    return f;
}

From source file:hudson.jbpm.PluginImpl.java

/**
 * Method supporting upload from the designer at /plugin/jbpm/upload
 *//*from w ww. jav a  2s . c  o m*/
public void doUpload(StaplerRequest req, StaplerResponse rsp)
        throws FileUploadException, IOException, ServletException {
    try {
        ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());

        // Parse the request
        FileItem fileItem = (FileItem) upload.parseRequest(req).get(0);

        if (fileItem.getContentType().indexOf("application/x-zip-compressed") == -1) {
            throw new IOException("Not a process archive");
        }

        log.fine("Deploying process archive " + fileItem.getName());
        ZipInputStream zipInputStream = new ZipInputStream(fileItem.getInputStream());
        JbpmContext jbpmContext = getCurrentJbpmContext();
        log.fine("Preparing to parse process archive");
        ProcessDefinition processDefinition = ProcessDefinition.parseParZipInputStream(zipInputStream);
        log.fine("Created a processdefinition : " + processDefinition.getName());
        jbpmContext.deployProcessDefinition(processDefinition);
        zipInputStream.close();
        rsp.forwardToPreviousPage(req);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:org.openmrs.contrib.metadatarepository.service.impl.PackageManagerImpl.java

public MetadataPackage deserializePackage(byte[] file) {
    Map<String, String> files = new LinkedHashMap<String, String>();

    InputStream input = new ByteArrayInputStream(file);

    ZipInputStream zip = new ZipInputStream(input);
    try {//from w  w w  .  ja va  2  s . com
        for (ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry()) {
            String file1 = IOUtils.toString(zip, ENCODING);
            files.put(entry.getName(), file1);
        }
    } catch (IOException e) {
        throw new APIException("error", e);
    } finally {
        if (zip != null) {
            try {
                zip.close();
            } catch (IOException e) {

                log.error(e);
            }
        }
    }
    String header = files.get(HEADER_FILE);

    XStream xstream = new XStream(new DomDriver());

    MetadataPackage deserializedPackage = new MetadataPackage();
    xstream.registerConverter(new DateTimeConverter());
    xstream.alias("package", MetadataPackage.class);
    xstream.omitField(MetadataPackage.class, "file");
    xstream.omitField(MetadataPackage.class, "user");
    xstream.omitField(MetadataPackage.class, "downloadCount");
    xstream.omitField(MetadataPackage.class, "serializedPackage");
    xstream.omitField(MetadataPackage.class, "modules");
    xstream.omitField(MetadataPackage.class, "items");
    xstream.omitField(MetadataPackage.class, "relatedItems");
    deserializedPackage = (MetadataPackage) xstream.fromXML(header);

    return deserializedPackage;
}

From source file:com.engine.biomine.indexing.tasks.IndexDocsTask.java

private void processZipFile(File archiveFile, ZipInputStream zis) {
    try {/*from w w w  .  j a v  a  2 s  .  c o m*/
        for (ZipEntry entry = zis.getNextEntry(); entry != null;) {
            if (!entry.isDirectory()) {
                File currentfile = IOUtil.getINSTANCE().retrieveCompressedFile(zis, entry.getName());
                if (currentfile != null) {
                    Runnable docThread = new DocThread(indexManager, monitor, currentfile, collection);
                    exec.execute(docThread);
                }
            }
            entry = zis.getNextEntry();
        }
        zis.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    logger.warn("Done reading file {}: . Sent to process.", archiveFile, monitor.getNbDocsToProcess());
}

From source file:edu.mit.lib.bagit.Loader.java

private void inflate(InputStream in, String fmt) throws IOException {
    switch (fmt) {
    case "zip":
        ZipInputStream zin = new ZipInputStream(in);
        ZipEntry entry = null;// w  w  w.ja  v  a2 s . co  m
        while ((entry = zin.getNextEntry()) != null) {
            File outFile = new File(base.getParent(), entry.getName());
            outFile.getParentFile().mkdirs();
            Files.copy(zin, outFile.toPath());
        }
        zin.close();
        break;
    case "tgz":
        TarArchiveInputStream tin = new TarArchiveInputStream(new GzipCompressorInputStream(in));
        TarArchiveEntry tentry = null;
        while ((tentry = tin.getNextTarEntry()) != null) {
            File outFile = new File(base.getParent(), tentry.getName());
            outFile.getParentFile().mkdirs();
            Files.copy(tin, outFile.toPath());
        }
        tin.close();
        break;
    default:
        throw new IOException("Unsupported archive format: " + fmt);
    }
}

From source file:nodomain.freeyourgadget.gadgetbridge.devices.pebble.PBWReader.java

public InputStream getInputStreamFile(String filename) {
    if (isLanguage) {
        try {//w  ww.  j a v a 2s.c o  m
            return uriHelper.openInputStream();
        } catch (FileNotFoundException e) {
            LOG.warn("file not found: " + e);
            return null;
        }
    }
    ZipInputStream zis = null;
    ZipEntry ze;
    try {
        zis = new ZipInputStream(uriHelper.openInputStream());
        while ((ze = zis.getNextEntry()) != null) {
            if (ze.getName().equals(filename)) {
                return zis; // return WITHOUT closing the stream!
            }
        }
        zis.close();
    } catch (Throwable e) {
        try {
            if (zis != null) {
                zis.close();
            }
        } catch (IOException e1) {
            // ignore
        }
        e.printStackTrace();
    }
    return null;
}

From source file:coral.reef.web.FileUploadController.java

/**
 * Extracts a zip file specified by the zipFilePath to a directory specified
 * by destDirectory (will be created if does not exists)
 * //from  w ww . j a v a2  s.co m
 * @param zipFilePath
 * @param destDirectory
 * @throws IOException
 */
public void unzip(InputStream zipFilePath, File destDir) throws IOException {
    if (!destDir.exists()) {
        destDir.mkdir();
    }
    ZipInputStream zipIn = new ZipInputStream(zipFilePath);
    ZipEntry entry = zipIn.getNextEntry();
    // iterates over entries in the zip file
    while (entry != null) {
        File filePath = new File(destDir, 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
            filePath.mkdir();
        }
        zipIn.closeEntry();
        entry = zipIn.getNextEntry();
    }
    zipIn.close();
}

From source file:org.eclipse.dirigible.repository.local.ZipRepository.java

protected void unpackZip(InputStream zip, String folder) throws IOException {
    ZipInputStream zipInputStream = new ZipInputStream(zip);
    try {/*from w ww.j a  va 2s.  c  om*/
        ZipEntry entry;
        while ((entry = zipInputStream.getNextEntry()) != null) {
            File entryDestination = new File(folder, entry.getName());
            if (entry.isDirectory()) {
                entryDestination.mkdirs();
            } else {
                entryDestination.getParentFile().mkdirs();
                OutputStream out = new FileOutputStream(entryDestination);
                IOUtils.copy(zipInputStream, out);
                out.close();
            }
        }
    } finally {
        zipInputStream.close();
    }
}

From source file:com.ibm.liberty.starter.ProjectConstructor.java

public void initializeMap() throws IOException {
    log.log(Level.INFO, "Entering method ProjectConstructor.initializeMap()");
    InputStream skeletonIS = this.getClass().getClassLoader().getResourceAsStream(SKELETON_FILENAME);
    ZipInputStream zis = new ZipInputStream(skeletonIS);
    ZipEntry ze;//w w  w .  j  av a2 s  .c o m
    while ((ze = zis.getNextEntry()) != null) {
        String path = ze.getName();
        int length = 0;
        byte[] bytes = new byte[1024];
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while ((length = zis.read(bytes)) != -1) {
            baos.write(bytes, 0, length);
        }
        putFileInMap(path, baos.toByteArray());
    }
    zis.close();
}