Example usage for java.util.zip ZipInputStream read

List of usage examples for java.util.zip ZipInputStream read

Introduction

In this page you can find the example usage for java.util.zip ZipInputStream read.

Prototype

public int read(byte b[]) throws IOException 

Source Link

Document

Reads up to b.length bytes of data from this input stream into an array of bytes.

Usage

From source file:com.espringtran.compressor4j.processor.GzipProcessor.java

/**
 * Read from compressed file/*from   w  w  w. ja  v  a  2s . c  o  m*/
 * 
 * @param srcPath
 *            path of compressed file
 * @param fileCompressor
 *            FileCompressor object
 * @throws Exception
 */
@Override
public void read(String srcPath, FileCompressor fileCompressor) throws Exception {
    long t1 = System.currentTimeMillis();
    byte[] data = FileUtil.convertFileToByte(srcPath);
    ByteArrayInputStream bais = new ByteArrayInputStream(data);
    GzipCompressorInputStream cis = new GzipCompressorInputStream(bais);
    ZipInputStream zis = new ZipInputStream(cis);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        byte[] buffer = new byte[1024];
        int readByte;
        ZipEntry entry = zis.getNextEntry();
        while (entry != null) {
            long t2 = System.currentTimeMillis();
            baos = new ByteArrayOutputStream();
            readByte = zis.read(buffer);
            while (readByte != -1) {
                baos.write(buffer, 0, readByte);
                readByte = zis.read(buffer);
            }
            zis.closeEntry();
            BinaryFile binaryFile = new BinaryFile(entry.getName(), baos.toByteArray());
            fileCompressor.addBinaryFile(binaryFile);
            LogUtil.createAddFileLog(fileCompressor, binaryFile, t2, System.currentTimeMillis());
            entry = zis.getNextEntry();
        }
    } catch (Exception e) {
        FileCompressor.LOGGER.error("Error on get compressor file", e);
    } finally {
        baos.close();
        zis.close();
        cis.close();
        bais.close();
    }
    LogUtil.createReadLog(fileCompressor, srcPath, data.length, t1, System.currentTimeMillis());
}

From source file:de.tudarmstadt.ukp.clarin.webanno.crowdflower.CrowdClient.java

/**
 * Unzips all elements in the file represented by byte[] data.
 * Needed by retrieveRawJudgments which will retrieve a zip-file with a single JSON
 * @param data/*from w  w  w .j  a  v a2s.  com*/
 * @return unzipped data
 * @throws IOException
 */

byte[] unzip(final byte[] data) throws IOException {
    final InputStream input = new ByteArrayInputStream(data);
    final byte[] buffer = new byte[1024];

    final ZipInputStream zip = new ZipInputStream(input);
    final ByteArrayOutputStream out = new ByteArrayOutputStream(data.length);
    int count = 0;

    if (zip.getNextEntry() != null) {
        while ((count = zip.read(buffer)) != -1) {
            out.write(buffer, 0, count);
        }
    }

    out.flush();
    zip.close();
    out.close();

    return out.toByteArray();
}

From source file:org.springframework.boot.gradle.tasks.bundling.BootZipCopyAction.java

private void writeClass(ZipArchiveEntry entry, ZipInputStream in, ZipArchiveOutputStream out)
        throws IOException {
    prepareEntry(entry, UnixStat.FILE_FLAG | UnixStat.DEFAULT_FILE_PERM);
    out.putArchiveEntry(entry);/*from w  w w .  j  av a 2  s.  co  m*/
    byte[] buffer = new byte[4096];
    int read;
    while ((read = in.read(buffer)) > 0) {
        out.write(buffer, 0, read);
    }
    out.closeArchiveEntry();
}

From source file:org.wso2.carbon.connector.FileUnzipConnector.java

/**
 * Extract each zip entry and write it into file.
 *
 * @param zipIn          Zip input stream for reading file in the ZIP file format.
 * @param remoteFilePath Location of file where zip entry needs to be extracted.
 *//*  w  w  w. jav a  2 s .c  o m*/
private void extractFile(ZipInputStream zipIn, FileObject remoteFilePath) {
    BufferedOutputStream bos = null;
    try {
        // open the zip file
        OutputStream fOut = remoteFilePath.getContent().getOutputStream();
        bos = new BufferedOutputStream(fOut);
        byte[] bytesIn = new byte[FileConstants.BUFFER_SIZE];
        int read;
        while ((read = zipIn.read(bytesIn)) != -1) {
            bos.write(bytesIn, 0, read);
        }
    } catch (IOException e) {
        throw new SynapseException("Unable to write zip entry", e);
    } finally {
        // close the zip file
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException e) {
                log.error("Error while closing the BufferedOutputStream", e);
            }
        }
        try {
            remoteFilePath.close();
        } catch (FileSystemException e) {
            log.error("Error while closing FileObject", e);
        }
    }
}

From source file:org.curriki.xwiki.plugin.asset.attachment.SankoreAssetManager.java

public void updateSubAssetClass(XWikiDocument assetDoc, String filetype, String category,
        XWikiAttachment attachment, XWikiContext context) {
    if (filetype.equals("ubz") || filetype.equals("ubw")) {
        String contents = new String();
        ZipEntry ze = null;/* w ww  . j a  v  a2 s .co m*/
        try {
            ZipInputStream zin = new ZipInputStream(attachment.getContentInputStream(context));
            while ((ze = zin.getNextEntry()) != null) {
                if (ze.getName().equals("metadata.rdf")) {
                    byte[] buffer = new byte[8192];
                    int len;
                    while ((len = zin.read(buffer)) != -1) {
                        String buffersz = new String(buffer, 0, len, "UTF-8");
                        contents += buffersz;
                    }
                    break;
                }
            }
            zin.close();
            DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = domFactory.newDocumentBuilder();
            Document dDoc = builder.parse(new ByteArrayInputStream(contents.getBytes("UTF8")));
            XPath xPath = XPathFactory.newInstance().newXPath();
            xPath.setNamespaceContext(new UniversalNamespaceResolver(dDoc));
            assetDoc.setTitle(xPath.evaluate("RDF/Description/sessionTitle", dDoc));
            assetDoc.setTags(xPath.evaluate("RDF/Description/sessionKeywords", dDoc), context);
            BaseObject assetObject = assetDoc.getObject(Constants.ASSET_CLASS);
            BaseObject licenceObject = assetDoc.getObject(Constants.ASSET_LICENCE_CLASS);
            assetObject.setLargeStringValue("description",
                    xPath.evaluate("RDF/Description/sessionObjectives", dDoc));
            assetObject.setStringValue("keywords", xPath.evaluate("RDF/Description/sessionKeywords", dDoc));
            assetObject.setStringValue("language", "fr");
            assetObject.setStringValue("education_system", "AssetMetadata.FranceEducation");
            String xvalue = xPath.evaluate("RDF/Description/sessionGradeLevel", dDoc);
            if (!xvalue.isEmpty() && context.getWiki().exists("SankoreCode.GradeLevelMapping", context)) {
                String mappingString = context.getWiki().getDocument("SankoreCode.GradeLevelMapping", context)
                        .getContent();
                Scanner scanner = new Scanner(mappingString);
                while (scanner.hasNextLine()) {
                    String line = scanner.nextLine();
                    String[] pair = line.split("=");
                    String key = pair[0];
                    if (key.equals(xvalue)) {
                        xvalue = pair[1];
                        assetObject.setDBStringListValue("educational_level", Arrays.asList(xvalue.split(",")));
                        break;
                    }
                }
            }
            xvalue = xPath.evaluate("RDF/Description/sessionSubjects", dDoc);
            if (!xvalue.isEmpty() && context.getWiki().exists("SankoreCode.SubjectsMapping", context)) {
                String mappingString = context.getWiki().getDocument("SankoreCode.SubjectsMapping", context)
                        .getContent();
                Scanner scanner = new Scanner(mappingString);
                while (scanner.hasNextLine()) {
                    String line = scanner.nextLine();
                    String[] pair = line.split("=");
                    String key = pair[0];
                    if (key.equals(xvalue)) {
                        xvalue = pair[1];
                        assetObject.setDBStringListValue("fw_items", Arrays.asList(xvalue.split(",")));
                        break;
                    }
                }
            }
            xvalue = xPath.evaluate("RDF/Description/sessionType", dDoc);
            if (!xvalue.isEmpty() && context.getWiki().exists("SankoreCode.TypeMapping", context)) {
                String mappingString = context.getWiki().getDocument("SankoreCode.TypeMapping", context)
                        .getContent();
                Scanner scanner = new Scanner(mappingString);
                while (scanner.hasNextLine()) {
                    String line = scanner.nextLine();
                    String[] pair = line.split("=");
                    String key = pair[0];
                    if (key.equals(xvalue)) {
                        xvalue = pair[1];
                        assetObject.setDBStringListValue("instructional_component",
                                Arrays.asList(xvalue.split(",")));
                        break;
                    }
                }
            }
            xvalue = xPath.evaluate("RDF/Description/sessionLicence", dDoc);
            if (!xvalue.isEmpty() && context.getWiki().exists("SankoreCode.LicenseMapping", context)) {
                String mappingString = context.getWiki().getDocument("SankoreCode.LicenseMapping", context)
                        .getContent();
                Scanner scanner = new Scanner(mappingString);
                while (scanner.hasNextLine()) {
                    String line = scanner.nextLine();
                    String[] pair = line.split("=");
                    String key = pair[0];
                    if (key.equals(xvalue)) {
                        xvalue = pair[1];
                        licenceObject.setStringValue("licenseType", xvalue);
                        break;
                    }
                }
            }
            licenceObject.setStringValue("rightsHolder",
                    xPath.evaluate("RDF/Description/sessionAuthors", dDoc));
        } catch (IOException ioe) {
            LOG.error("Unexpected exception ", ioe);
        } catch (ParserConfigurationException pce) {
            LOG.error("Unexpected exception ", pce);
        } catch (SAXException saxe) {
            LOG.error("Unexpected exception ", saxe);
        } catch (XPathException xpe) {
            LOG.error("Unexpected exception ", xpe);
        } catch (XWikiException xe) {
            LOG.error("Unexpected exception ", xe);
        }
    }
}

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;// ww w. j a v  a 2  s  .  co  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();
}

From source file:com.taobao.android.tpatch.utils.JarSplitUtils.java

public static void splitZipToFolder(File inputFile, File outputFolder, Set<String> includeEnties)
        throws IOException {
    if (!outputFolder.exists()) {
        outputFolder.mkdirs();//from   w  w  w .  j  a v  a 2s .c  om
    }
    if (null == includeEnties || includeEnties.size() < 1) {
        return;
    }
    final byte[] buffer = new byte[8192];
    FileInputStream fis = new FileInputStream(inputFile);
    ZipInputStream zis = new ZipInputStream(fis);

    try {
        // loop on the entries of the jar file package and put them in the final jar
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            // do not take directories or anything inside a potential META-INF folder.
            if (entry.isDirectory()) {
                continue;
            }
            String name = entry.getName();
            if (!includeEnties.contains(name)) {
                continue;
            }
            File destFile = new File(outputFolder, name);
            destFile.getParentFile().mkdirs();
            // read the content of the entry from the input stream, and write it into the archive.
            int count;
            FileOutputStream fout = FileUtils.openOutputStream(destFile);
            while ((count = zis.read(buffer)) != -1) {
                fout.write(buffer, 0, count);
            }
            fout.close();
            zis.closeEntry();
        }
    } finally {
        zis.close();
    }
    fis.close();
}

From source file:org.sakaiproject.scorm.service.standalone.impl.StandaloneResourceService.java

@Override
protected String newItem(String uuid, ZipInputStream zipStream, ZipEntry entry) {
    File file = new File(getContentPackageDirectory(uuid), entry.getName());

    FileOutputStream fileStream = null;
    try {//from w w  w .  j  a  v  a  2 s .  c  o m
        fileStream = new FileOutputStream(file);
        byte[] buffer = new byte[1024];
        int length;

        while ((length = zipStream.read(buffer)) > 0) {
            fileStream.write(buffer, 0, length);
        }
    } catch (FileNotFoundException fnfe) {
        log.error("Could not write to file " + file.getAbsolutePath(), fnfe);
    } catch (IOException ioe) {
        log.error("Could not read from zip stream ", ioe);
    } finally {
        if (null != fileStream) {
            try {
                fileStream.close();
            } catch (IOException e) {
                log.error(e);
            }
        }
    }

    return file.getAbsolutePath();
}

From source file:org.fusesource.cloudmix.agent.jbi.JBIInstallerAgent.java

private void extractZipEntry(ZipInputStream zip, OutputStream os) throws IOException {
    final byte[] buffer = new byte[FileUtils.BUFFER_SIZE];

    try {//  w  ww .  ja  v  a2  s  .  c  om
        long total = 0;
        int n = zip.read(buffer);
        while (n != -1) {
            total += n;
            os.write(buffer, 0, n);
            n = zip.read(buffer);
        }
        LOGGER.info("Copied " + total + " bytes");
    } finally {
        zip.closeEntry();
        os.close();
    }
}

From source file:org.wso2.carbon.connector.util.FileUnzipUtil.java

/**
 * @param zipIn    :Input zip stream/*from www .jav a 2 s .  c  o m*/
 * @param filePath :Location of each entry of the file.
 */
private void extractFile(ZipInputStream zipIn, String filePath, FileSystemOptions opts) {
    BufferedOutputStream bos = null;
    try {
        // Create remote object
        FileObject remoteFilePath = manager.resolveFile(filePath, opts);
        //open the zip file
        OutputStream fOut = remoteFilePath.getContent().getOutputStream();
        bos = new BufferedOutputStream(fOut);
        byte[] bytesIn = new byte[FileConstants.BUFFER_SIZE];
        int read;
        while ((read = zipIn.read(bytesIn)) != -1) {
            bos.write(bytesIn, 0, read);
        }
    } catch (IOException e) {
        log.error("Unable to read an entry: " + e.getMessage(), e);
    } finally {
        //we must always close the zip file
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException e) {
                log.error("Error while closing the BufferedOutputStream: " + e.getMessage(), e);
            }
        }
    }
}