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:es.juntadeandalucia.panelGestion.negocio.utiles.file.shape.LocalShapeReader.java

private void unzipShapefiles(File shapefileCompressedData) throws IOException {
    IOException error = null;// w  w w  . j  av a  2 s .  c om
    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.apache.axis2.deployment.resolver.AARFileBasedURIResolver.java

public InputSource resolveEntity(String targetNamespace, String schemaLocation, String baseUri) {
    //no issue with abloslute schemas 
    // this schema can be in a relative location for another base scheama. so first
    // try to see the proper location

    lastImportLocation = URI.create(baseUri).resolve(schemaLocation);
    if (isAbsolute(lastImportLocation.toString())) {
        return super.resolveEntity(targetNamespace, schemaLocation, baseUri);
    } else {/*from w w w .  ja v a2 s .com*/
        //validate
        if ((baseUri == null || "".equals(baseUri)) && schemaLocation.startsWith("..")) {
            throw new RuntimeException("Unsupported schema location " + schemaLocation);
        }

        ZipInputStream zin = null;
        try {
            zin = new ZipInputStream(new FileInputStream(aarFile));

            ZipEntry entry;
            byte[] buf = new byte[1024];
            int read;
            ByteArrayOutputStream out;
            String searchingStr = lastImportLocation.toString();
            while ((entry = zin.getNextEntry()) != null) {
                String entryName = entry.getName().toLowerCase();
                if (entryName.equalsIgnoreCase(searchingStr)) {
                    out = new ByteArrayOutputStream();
                    while ((read = zin.read(buf)) > 0) {
                        out.write(buf, 0, read);
                    }
                    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
                    InputSource inputSoruce = new InputSource(in);
                    inputSoruce.setSystemId(lastImportLocation.getPath());
                    inputSoruce.setPublicId(targetNamespace);
                    return inputSoruce;
                }
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (zin != null) {
                    zin.close();
                }
            } catch (IOException e) {
                log.debug(e);
            }
        }

    }

    log.info("AARFileBasedURIResolver: Unable to resolve" + lastImportLocation);
    return null;
}

From source file:org.wso2.carbon.webapp.list.ui.WebAppDataExtractor.java

public void getServletXML(InputStream inputStream) {
    jaxWSMap.clear();//ww  w .ja v a  2s.c  om
    jaxRSMap.clear();
    ZipInputStream zipInputStream = new ZipInputStream(inputStream);

    try {
        ZipEntry entry;

        HashMap<String, byte[]> map = new HashMap<String, byte[]>();
        while ((entry = zipInputStream.getNextEntry()) != null) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buff = new byte[1024];
            int count;

            while ((count = zipInputStream.read(buff)) != -1) {
                baos.write(buff, 0, count);
            }

            String filename = entry.getName();
            byte[] bytes = baos.toByteArray();
            map.put(filename, bytes);
        }

        String webXmlString = stripNonValidXMLCharacters(new String(map.get("WEB-INF/web.xml")));

        processWebXml(webXmlString);

        String configFile = "";

        if (map.containsKey(cxfConfigFileLocation)) {
            configFile = new String(map.get(cxfConfigFileLocation));
        } else {
            return;
        }

        configFile = stripNonValidXMLCharacters(configFile);
        OMElement element = AXIOMUtil.stringToOM(configFile);

        Iterator<OMElement> iterator = element
                .getChildrenWithName(new QName("http://cxf.apache.org/jaxws", "endpoint"));
        while (iterator.hasNext()) {
            OMElement temp = iterator.next();
            jaxWSMap.put(temp.getAttribute(new QName("id")).getAttributeValue(),
                    temp.getAttribute(new QName("address")).getAttributeValue());

        }

        iterator = element.getChildrenWithName(new QName("http://cxf.apache.org/jaxws", "server"));
        while (iterator.hasNext()) {
            OMElement temp = iterator.next();
            jaxWSMap.put(temp.getAttribute(new QName("id")).getAttributeValue(),
                    temp.getAttribute(new QName("address")).getAttributeValue());

        }

        iterator = element.getChildrenWithName(new QName("http://cxf.apache.org/jaxrs", "server"));
        while (iterator.hasNext()) {
            OMElement temp = iterator.next();
            jaxRSMap.put(temp.getAttribute(new QName("id")).getAttributeValue(),
                    temp.getAttribute(new QName("address")).getAttributeValue());

        }

        setServiceListPath(processServiceListPathWebXml(webXmlString));

    } catch (Exception ex) {
        log.error(ex.getMessage(), ex);
    } finally {
        try {
            zipInputStream.close();
        } catch (IOException e) {
            //ignore
        }
    }
}

From source file:io.fabric8.maven.generator.springboot.SpringBootGenerator.java

private void copyFilesToFatJar(List<File> libs, List<File> classes, File target) throws IOException {
    File tmpZip = File.createTempFile(target.getName(), null);
    tmpZip.delete();

    // Using Apache commons rename, because renameTo has issues across file systems
    FileUtils.moveFile(target, tmpZip);// w  ww . j  a  va 2s . c o  m

    byte[] buffer = new byte[8192];
    ZipInputStream zin = new ZipInputStream(new FileInputStream(tmpZip));
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(target));
    for (ZipEntry ze = zin.getNextEntry(); ze != null; ze = zin.getNextEntry()) {
        if (matchesFatJarEntry(libs, ze.getName(), true) || matchesFatJarEntry(classes, ze.getName(), false)) {
            continue;
        }
        out.putNextEntry(ze);
        for (int read = zin.read(buffer); read > -1; read = zin.read(buffer)) {
            out.write(buffer, 0, read);
        }
        out.closeEntry();
    }

    for (File lib : libs) {
        try (InputStream in = new FileInputStream(lib)) {
            out.putNextEntry(createZipEntry(lib, getFatJarFullPath(lib, true)));
            for (int read = in.read(buffer); read > -1; read = in.read(buffer)) {
                out.write(buffer, 0, read);
            }
            out.closeEntry();
        }
    }

    for (File cls : classes) {
        try (InputStream in = new FileInputStream(cls)) {
            out.putNextEntry(createZipEntry(cls, getFatJarFullPath(cls, false)));
            for (int read = in.read(buffer); read > -1; read = in.read(buffer)) {
                out.write(buffer, 0, read);
            }
            out.closeEntry();
        }
    }

    out.close();
    tmpZip.delete();
}

From source file:tachyon.java.manager.JavaFxManager.java

private void extractToFile(ZipInputStream sipIn, String filePath) throws IOException {
    try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath))) {
        byte[] bytesIn = new byte[4096];
        int read;
        while ((read = sipIn.read(bytesIn)) != -1) {
            bos.write(bytesIn, 0, read);
        }/*from   w  w  w .j  av  a2  s .co m*/
    }
}

From source file:org.apache.axis2.deployment.resolver.AARBasedWSDLLocator.java

/**
 * @param parentLocation//from w  w w  . ja v a  2 s  .co  m
 * @param importLocation
 */
public InputSource getImportInputSource(String parentLocation, String importLocation) {
    lastImportLocation = URI.create(parentLocation).resolve(importLocation);

    if (isAbsolute(lastImportLocation.toString())) {
        return super.resolveEntity(null, importLocation, parentLocation);
    } else {
        //we don't care about the parent location
        ZipInputStream zin = null;
        try {

            zin = new ZipInputStream(new FileInputStream(aarFile));
            ZipEntry entry;
            byte[] buf = new byte[1024];
            int read;
            ByteArrayOutputStream out;
            String searchingStr = lastImportLocation.toString();
            while ((entry = zin.getNextEntry()) != null) {
                String entryName = entry.getName().toLowerCase();
                if (entryName.equalsIgnoreCase(searchingStr)) {
                    out = new ByteArrayOutputStream();
                    while ((read = zin.read(buf)) > 0) {
                        out.write(buf, 0, read);
                    }
                    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
                    return new InputSource(in);
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (zin != null) {
                    zin.close();
                }
            } catch (IOException e) {
                log.debug(e);
            }
        }
    }

    log.info("AARBasedWSDLLocator: Unable to resolve " + lastImportLocation);
    return null;
}

From source file:sernet.verinice.service.sync.VeriniceArchive.java

/**
 * Extracts all entries of a Zip-Archive
 * //from   w  ww.  j a  va2 s  . c o  m
 * @param zipFileData Data of a zip archive
 * @throws IOException
 */
public void extractZipEntries(byte[] zipFileData) throws IOException {
    byte[] buffer = new byte[1024];
    ;
    new File(getTempDirName()).mkdirs();
    // get the zip file content
    ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(zipFileData));
    // get the zipped file list entry
    ZipEntry ze = zis.getNextEntry();

    while (ze != null) {
        if (!ze.isDirectory()) {
            String fileName = ze.getName();
            File newFile = new File(getTempDirName() + File.separator + fileName);
            new File(newFile.getParent()).mkdirs();

            FileOutputStream fos = new FileOutputStream(newFile);

            int len;
            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }

            fos.close();

            if (LOG.isDebugEnabled()) {
                LOG.debug("File unzipped: " + newFile.getAbsoluteFile());
            }
        }
        ze = zis.getNextEntry();
    }
    zis.closeEntry();
    zis.close();
}

From source file:org.kurator.validation.actors.io.DwCaReader.java

@Override
public void fireOnce() throws Exception {
    File file = new File(filePath);
    if (!file.exists()) {
        // Error/* w  w  w  . ja va2  s . co m*/
        logger.error(filePath + " not found.");
    }
    if (!file.canRead()) {
        // error
        logger.error("Unable to read " + filePath);
    }
    if (file.isDirectory()) {
        // check if it is an unzipped dwc archive.
        dwcArchive = openArchive(file);
    }
    if (file.isFile()) {
        // unzip it
        File outputDirectory = new File(file.getName().replace(".", "_") + "_content");
        if (!outputDirectory.exists()) {
            outputDirectory.mkdir();
            try {
                byte[] buffer = new byte[1024];
                ZipInputStream inzip = new ZipInputStream(new FileInputStream(file));
                ZipEntry entry = inzip.getNextEntry();
                while (entry != null) {
                    String fileName = entry.getName();
                    File expandedFile = new File(outputDirectory.getPath() + File.separator + fileName);
                    new File(expandedFile.getParent()).mkdirs();
                    FileOutputStream expandedfileOutputStream = new FileOutputStream(expandedFile);
                    int len;
                    while ((len = inzip.read(buffer)) > 0) {
                        expandedfileOutputStream.write(buffer, 0, len);
                    }

                    expandedfileOutputStream.close();
                    entry = inzip.getNextEntry();
                }
                inzip.closeEntry();
                inzip.close();
                System.out.println("Unzipped archive into " + outputDirectory.getPath());
            } catch (FileNotFoundException e) {
                logger.error(e.getMessage());
                e.printStackTrace();
            } catch (IOException e) {
                logger.error(e.getMessage());
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        // look into the unzipped directory
        dwcArchive = openArchive(outputDirectory);

        iterator = dwcArchive.iterator();
        while (iterator.hasNext()) {
            // read initial set of rows, pass downstream
            StarRecord dwcrecord = iterator.next();
            SpecimenRecord record = new SpecimenRecord(dwcrecord);
            broadcast(record);
        }
    }
    if (dwcArchive != null) {
        if (checkArchive()) {
            // good to go
        }
    } else {
        System.out.println("Problem opening archive.");
    }
}

From source file:org.onexus.resource.manager.internal.providers.ZipProjectProvider.java

@Override
protected void importProject() {

    try {/*w w  w  .  j  a  va2s .com*/

        byte[] buffer = new byte[1024];
        URL url = new URL(getProjectUrl());
        ZipInputStream zis = new ZipInputStream(url.openStream());
        ZipEntry ze = zis.getNextEntry();

        File projectFolder = getProjectFolder();
        if (!projectFolder.exists()) {
            projectFolder.mkdir();
        } else {
            FileUtils.cleanDirectory(projectFolder);
        }

        while (ze != null) {

            String fileName = ze.getName();
            File newFile = new File(projectFolder, fileName);
            if (ze.isDirectory()) {
                newFile.mkdir();
            } else {
                FileOutputStream fos = new FileOutputStream(newFile);
                int len;
                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }
                fos.close();
            }

            ze = zis.getNextEntry();

        }
        zis.close();

    } catch (Exception e) {

        LOGGER.error("Importing project '" + getProjectUrl() + "'", e);
        throw new InvalidParameterException("Invalid Onexus URL project");
    }

    //To change body of implemented methods use File | Settings | File Templates.
}

From source file:org.apache.oodt.cas.workflow.misc.WingsTask.java

private void unZipIt(String zipFile, String outputFolder) {
    byte[] buffer = new byte[1024];
    try {//from www .ja v a 2 s .c  o  m
        File folder = new File(outputFolder);
        if (!folder.exists()) {
            folder.mkdir();
        }
        ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
        ZipEntry ze = zis.getNextEntry();

        while (ze != null) {
            String fileName = ze.getName();
            File newFile = new File(outputFolder + File.separator + fileName);
            new File(newFile.getParent()).mkdirs();
            FileOutputStream fos = new FileOutputStream(newFile);
            int len;
            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }
            fos.close();
            ze = zis.getNextEntry();
        }
        zis.closeEntry();
        zis.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}