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

/**
 * Unpack the jar file to a directory, till teaf files
 * @param file The source file name/*from   w w  w. j  av a 2 s. c o m*/
 * @param file1 The target directory
 * @author wangxp
 * @version 2003/11/07
 */
public static void unpackAppJar(String file, String file1) throws IOException {

    // m_log.debug("unpackAppJar begin. file="+file+"|||file1="+file1);

    BufferedInputStream bufferedinputstream = new BufferedInputStream(new FileInputStream(file));
    ZipInputStream zipinputstream = new ZipInputStream(bufferedinputstream);
    byte abyte0[] = new byte[1024];
    for (ZipEntry zipentry = zipinputstream.getNextEntry(); zipentry != null; zipentry = zipinputstream
            .getNextEntry()) {
        File file2 = new File(file1, zipentry.getName());
        if (zipentry.isDirectory()) {
            if (!file2.exists() && !file2.mkdirs())
                throw new IOException("Could not make directory " + file2.getPath());
        } else {
            File file3 = file2.getParentFile();
            if (file3 != null && !file3.exists() && !file3.mkdirs())
                throw new IOException("Could not make directory " + file3.getPath());
            BufferedOutputStream bufferedoutputstream = new BufferedOutputStream(new FileOutputStream(file2));
            int i;
            try {
                while ((i = zipinputstream.read(abyte0, 0, abyte0.length)) != -1)
                    bufferedoutputstream.write(abyte0, 0, i);
            } catch (IOException ie) {
                ie.printStackTrace();
                // m_logger.error(ie);
                throw ie;
            }
            bufferedoutputstream.close();
        }
    }
    zipinputstream.close();
    bufferedinputstream.close();
    // m_log.debug("unpackAppJar end.");
}

From source file:org.apache.axis2.deployment.DeploymentClassLoader.java

/**
 * Get a specific entry's content as a byte array
 * /* w  ww  . ja v a2  s  . co  m*/
 * @param in
 * @param resource
 * @return
 * @throws Exception
 */
private byte[] getBytes(InputStream in, String resource) throws Exception {
    ZipInputStream zin = new ZipInputStream(in);
    ZipEntry entry;
    String entryName;
    while ((entry = zin.getNextEntry()) != null) {
        entryName = entry.getName();
        if (entryName != null && entryName.endsWith(resource)) {
            byte[] raw = IOUtils.toByteArray(zin);
            zin.close();
            return raw;
        }
    }
    return null;
}

From source file:org.jjdltc.cordova.plugin.zip.decompressZip.java

/**
 * Extracts a zip file to a given path/*w  w w .ja  v  a  2s. c  om*/
 * @param actualTargetPath  Path to un-zip
 * @throws IOException
 */
public boolean doUnZip(String actualTargetPath) throws IOException {
    File target = new File(actualTargetPath);
    if (!target.exists()) {
        target.mkdir();
    }

    ZipInputStream zipFl = new ZipInputStream(new FileInputStream(this.sourceEntry));
    ZipEntry entry = zipFl.getNextEntry();

    while (entry != null) {
        String filePath = actualTargetPath + File.separator + entry.getName();
        if (entry.isDirectory()) {
            File path = new File(filePath);
            path.mkdir();
        } else {
            extractFile(zipFl, filePath);
        }
        zipFl.closeEntry();
        entry = zipFl.getNextEntry();
    }
    zipFl.close();
    return true;
}

From source file:com.wso2mobile.mam.packageExtractor.ZipFileReading.java

public String readAndroidManifestFile(String filePath) {
    String xml = "";
    try {/*from   w w w  .  jav  a  2  s .  c  om*/
        ZipInputStream stream = new ZipInputStream(new FileInputStream(filePath));
        try {
            ZipEntry entry;
            while ((entry = stream.getNextEntry()) != null) {
                if (entry.getName().equals("AndroidManifest.xml")) {
                    StringBuilder builder = new StringBuilder();
                    xml = AndroidXMLParsing.decompressXML(IOUtils.toByteArray(stream));
                }
            }
        } finally {
            stream.close();
        }
        Document doc = loadXMLFromString(xml);
        doc.getDocumentElement().normalize();
        JSONObject obj = new JSONObject();
        obj.put("version", doc.getDocumentElement().getAttribute("versionName"));
        obj.put("package", doc.getDocumentElement().getAttribute("package"));
        xml = obj.toJSONString();
    } catch (Exception e) {
        xml = "Exception occured " + e;
    }
    return xml;
}

From source file:eu.scape_project.up2ti.container.ZipContainer.java

/**
 * Extracts a zip file specified by the zipFilePath to a directory specified by
 * destDirectory (will be created if does not exists)
 * @param containerFileName//from  w w w .jav  a2s  . co m
 * @param destDirectory
 * @throws IOException
 */
public void unzip(String containerFileName, InputStream containerFileStream) throws IOException {
    extractDirectoryName = "/tmp/up2ti_" + RandomStringUtils.randomAlphabetic(10) + "/";
    File destDir = new File(extractDirectoryName);
    destDir.mkdir();
    String subDestDirStr = extractDirectoryName + containerFileName + "/";
    File subDestDir = new File(subDestDirStr);
    subDestDir.mkdir();
    ZipInputStream zipIn = new ZipInputStream(containerFileStream);
    ZipEntry entry = zipIn.getNextEntry();
    while (entry != null) {
        String filePath = subDestDirStr + entry.getName();
        if (!entry.isDirectory()) {
            extractFile(zipIn, filePath);
        } else {
            File dir = new File(filePath);
            dir.mkdir();
        }
        zipIn.closeEntry();
        entry = zipIn.getNextEntry();
    }
    zipIn.close();
}

From source file:org.apache.nutch.parse.oo.OOParser.java

public ParseResult getParse(Content content) {
    String text = null;//from   www  .java  2s . co m
    String title = null;
    Metadata metadata = new Metadata();
    ArrayList outlinks = new ArrayList();

    try {
        byte[] raw = content.getContent();
        String contentLength = content.getMetadata().get("Content-Length");
        if (contentLength != null && raw.length != Integer.parseInt(contentLength)) {
            return new ParseStatus(ParseStatus.FAILED, ParseStatus.FAILED_TRUNCATED,
                    "Content truncated at " + raw.length + " bytes. Parser can't handle incomplete files.")
                            .getEmptyParseResult(content.getUrl(), conf);
        }
        ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(raw));
        ZipEntry ze = null;
        while ((ze = zis.getNextEntry()) != null) {
            if (ze.getName().equals("content.xml")) {
                text = parseContent(ze, zis, outlinks);
            } else if (ze.getName().equals("meta.xml")) {
                parseMeta(ze, zis, metadata);
            }
        }
        zis.close();
    } catch (Exception e) { // run time exception
        e.printStackTrace(LogUtil.getWarnStream(LOG));
        return new ParseStatus(ParseStatus.FAILED, "Can't be handled as OO document. " + e)
                .getEmptyParseResult(content.getUrl(), conf);
    }

    title = metadata.get(Metadata.TITLE);
    if (text == null)
        text = "";

    if (title == null)
        title = "";

    Outlink[] links = (Outlink[]) outlinks.toArray(new Outlink[outlinks.size()]);
    ParseData parseData = new ParseData(ParseStatus.STATUS_SUCCESS, title, links, metadata);
    return ParseResult.createParseResult(content.getUrl(), new ParseImpl(text, parseData));
}

From source file:eu.scape_project.archiventory.container.ZipContainer.java

/**
 * Extracts a zip file specified by the zipFilePath to a directory specified by
 * destDirectory (will be created if does not exists)
 * @param containerFileName//from   www  . j av  a  2  s  .co  m
 * @param destDirectory
 * @throws IOException
 */
public void unzip(String containerFileName, InputStream containerFileStream) throws IOException {
    extractDirectoryName = "/tmp/archiventory_" + RandomStringUtils.randomAlphabetic(10) + "/";
    File destDir = new File(extractDirectoryName);
    destDir.mkdir();
    String subDestDirStr = extractDirectoryName + containerFileName + "/";
    File subDestDir = new File(subDestDirStr);
    subDestDir.mkdir();
    ZipInputStream zipIn = new ZipInputStream(containerFileStream);
    ZipEntry entry = zipIn.getNextEntry();
    while (entry != null) {
        String filePath = subDestDirStr + entry.getName();
        if (!entry.isDirectory()) {
            extractFile(zipIn, filePath);
        } else {
            File dir = new File(filePath);
            dir.mkdir();
        }
        zipIn.closeEntry();
        entry = zipIn.getNextEntry();
    }
    zipIn.close();
}

From source file:be.fedict.eid.dss.document.zip.ZIPSignatureOutputStream.java

@Override
public void close() throws IOException {
    super.close();

    byte[] signatureData = toByteArray();

    /*/*from  w w  w.  j av  a 2  s.c o  m*/
     * Copy the original ZIP content.
     */
    ZipOutputStream zipOutputStream = new ZipOutputStream(this.targetOutputStream);
    ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(this.originalZipFile));
    ZipEntry zipEntry;
    while (null != (zipEntry = zipInputStream.getNextEntry())) {
        if (!zipEntry.getName().equals(ODFUtil.SIGNATURE_FILE)) {
            ZipEntry newZipEntry = new ZipEntry(zipEntry.getName());
            zipOutputStream.putNextEntry(newZipEntry);
            LOG.debug("copying " + zipEntry.getName());
            IOUtils.copy(zipInputStream, zipOutputStream);
        }
    }
    zipInputStream.close();

    /*
     * Add the XML signature file to the ZIP package.
     */
    zipEntry = new ZipEntry(ODFUtil.SIGNATURE_FILE);
    LOG.debug("writing " + zipEntry.getName());
    zipOutputStream.putNextEntry(zipEntry);
    IOUtils.write(signatureData, zipOutputStream);
    zipOutputStream.close();
}

From source file:com.hazelcast.stabilizer.Utils.java

public static void unzip(byte[] content, final File destinationDir) throws IOException {
    byte[] buffer = new byte[1024];

    ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(content));
    ZipEntry zipEntry = zis.getNextEntry();

    while (zipEntry != null) {

        String fileName = zipEntry.getName();
        File file = new File(destinationDir + File.separator + fileName);

        //            log.finest("Unzipping: " + file.getAbsolutePath());

        if (zipEntry.isDirectory()) {
            file.mkdirs();//  ww w.  j av a2s. com
        } else {
            file.getParentFile().mkdirs();

            FileOutputStream fos = new FileOutputStream(file);
            try {
                int len;
                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }
            } finally {
                closeQuietly(fos);
            }
        }

        zipEntry = zis.getNextEntry();
    }

    zis.closeEntry();
    zis.close();
}

From source file:org.jbpm.web.ProcessUploadServlet.java

private String handleRequest(HttpServletRequest request) {
    //check if request is multipart content
    log.debug("Handling upload request");
    if (!FileUpload.isMultipartContent(request)) {
        log.debug("Not a multipart request");
        return "Not a multipart request";
    }//from  w w  w  .j  av  a  2  s.co m

    try {
        DiskFileUpload fileUpload = new DiskFileUpload();
        List list = fileUpload.parseRequest(request);
        log.debug("Upload from GPD");
        Iterator iterator = list.iterator();
        if (!iterator.hasNext()) {
            log.debug("No process file in the request");
            return "No process file in the request";
        }
        FileItem fileItem = (FileItem) iterator.next();
        if (fileItem.getContentType().indexOf("application/x-zip-compressed") == -1) {
            log.debug("Not a process archive");
            return "Not a process archive";
        }
        try {
            log.debug("Deploying process archive " + fileItem.getName());
            ZipInputStream zipInputStream = new ZipInputStream(fileItem.getInputStream());
            JbpmContext jbpmContext = JbpmContext.getCurrentJbpmContext();
            log.debug("Preparing to parse process archive");
            ProcessDefinition processDefinition = ProcessDefinition.parseParZipInputStream(zipInputStream);
            log.debug("Created a processdefinition : " + processDefinition.getName());
            jbpmContext.deployProcessDefinition(processDefinition);
            zipInputStream.close();
            return "Deployed archive " + processDefinition.getName() + " successfully";
        } catch (IOException e) {
            log.debug("Failed to read process archive", e);
            return "IOException";
        }
    } catch (FileUploadException e) {
        log.debug("Failed to parse HTTP request", e);
        return "FileUploadException";
    }
}