Example usage for java.util.zip ZipInputStream getNextEntry

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

Introduction

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

Prototype

public ZipEntry getNextEntry() throws IOException 

Source Link

Document

Reads the next ZIP file entry and positions the stream at the beginning of the entry data.

Usage

From source file:com.sqli.liferay.imex.util.xml.ZipUtils.java

public void _unzipArchive(File archive, File outputDir) throws IOException {
    BufferedOutputStream dest = null;
    FileInputStream fis = new FileInputStream(archive);
    CheckedInputStream checksum = new CheckedInputStream(fis, new Adler32());
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(checksum));
    ZipEntry entry;//  w ww  .j  av  a 2 s.  c o m
    while ((entry = zis.getNextEntry()) != null) {
        log.debug("Extracting: " + entry);
        int count;
        byte data[] = new byte[BUFFER];
        // write the files to the disk
        FileOutputStream fos = new FileOutputStream(entry.getName());
        dest = new BufferedOutputStream(fos, BUFFER);
        while ((count = zis.read(data, 0, BUFFER)) != -1) {
            dest.write(data, 0, count);
        }
        dest.flush();
        dest.close();
    }
    zis.close();
    log.debug("Checksum:" + checksum.getChecksum().getValue());

}

From source file:be.fedict.eid.applet.service.signer.asic.AbstractASiCSignatureService.java

@Override
protected Document getEnvelopingDocument() throws ParserConfigurationException, IOException, SAXException {
    FileInputStream fileInputStream = new FileInputStream(this.tmpFile);
    ZipInputStream zipInputStream = new ZipInputStream(fileInputStream);
    ZipEntry zipEntry;//from  ww  w . j  a va  2s . c  om
    while (null != (zipEntry = zipInputStream.getNextEntry())) {
        if (ASiCUtil.isSignatureZipEntry(zipEntry)) {
            Document documentSignaturesDocument = ODFUtil.loadDocument(zipInputStream);
            return documentSignaturesDocument;
        }
    }
    Document document = ASiCUtil.createNewSignatureDocument();
    return document;
}

From source file:com.thoughtworks.go.util.ZipUtil.java

public String getFileContentInsideZip(ZipInputStream zipInputStream, String file) throws IOException {
    ZipEntry zipEntry = zipInputStream.getNextEntry();
    while (zipEntry != null) {
        if (new File(zipEntry.getName()).getName().equals(file)) {
            return IOUtils.toString(zipInputStream, UTF_8);
        }/*  www. j a  v a  2  s . c o  m*/
        zipEntry = zipInputStream.getNextEntry();
    }
    return null;
}

From source file:com.clank.launcher.install.ZipExtract.java

@Override
public void run() {
    Closer closer = Closer.create();/*from w  ww .  ja  v  a  2s.  c o  m*/

    try {
        InputStream is = closer.register(source.openBufferedStream());
        ZipInputStream zis = closer.register(new ZipInputStream(is));
        ZipEntry entry;

        destination.getParentFile().mkdirs();

        while ((entry = zis.getNextEntry()) != null) {
            if (matches(entry)) {
                File file = new File(getDestination(), entry.getName());
                writeEntry(zis, file);
            }
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            closer.close();
        } catch (IOException e) {
        }
    }
}

From source file:net.famzangl.minecraft.minebot.settings.MinebotDirectoryCreator.java

public File createDirectory(File dir) throws IOException {
    CodeSource src = MinebotDirectoryCreator.class.getProtectionDomain().getCodeSource();
    if (src != null) {
        URL jar = src.getLocation();
        if (jar.getFile().endsWith("class") && !jar.getFile().contains(".jar!")) {
            System.out.println("WARNING: Using the dev directory for settings.");
            // We are in a dev enviroment.
            return new File(new File(jar.getFile()).getParentFile(), "minebot");
        }/*from  w ww  .  j a v  a 2  s.  c  o  m*/
        if (dir.isFile()) {
            dir.delete();
        }

        dir.mkdirs();
        ZipInputStream zip = new ZipInputStream(jar.openStream());
        try {
            ZipEntry zipEntry;
            while ((zipEntry = zip.getNextEntry()) != null) {
                String name = zipEntry.getName();
                if (name.startsWith(BASE)) {
                    String[] localName = name.substring(BASE.length()).split("/");
                    File currentDir = dir;
                    for (int i = 0; i < localName.length; i++) {
                        currentDir = new File(currentDir, localName[i]);
                        currentDir.mkdir();
                    }
                    File copyTo = new File(currentDir, localName[localName.length - 1]);
                    extract(zip, copyTo);
                }
            }
        } finally {
            zip.close();
        }
        return dir;
    } else {
        throw new IOException("Could not find minebot directory to extract.");
    }
}

From source file:com.oprisnik.semdroid.Semdroid.java

protected void extract(InputStream source, File targetDir) {
    if (!targetDir.exists()) {
        targetDir.mkdirs();//from   w  w w  .ja  va  2  s .  c  o  m
    }
    if (BuildConfig.DEBUG) {
        Log.d(TAG, "Extracting to " + targetDir);
    }

    try {
        ZipInputStream zip = new ZipInputStream(source);
        ZipEntry entry = null;

        while ((entry = zip.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                File dir = new File(targetDir, entry.getName());
                if (!dir.exists()) {
                    dir.mkdirs();
                }
            } else {
                File newFile = new File(targetDir, entry.getName());
                FileOutputStream fout = new FileOutputStream(newFile);
                if (BuildConfig.DEBUG) {
                    Log.d(TAG, "Extracting " + entry.getName() + " to " + newFile);
                }

                byte[] data = new byte[BUFFER];
                int count;
                while ((count = zip.read(data, 0, BUFFER)) != -1) {
                    fout.write(data, 0, count);
                }
                zip.closeEntry();
                fout.close();
            }

        }
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
    } finally {
        IOUtils.closeQuietly(source);
    }
}

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

public String readAndroidManifestFile(String filePath) {
    String xml = "";
    try {/*from   w  w  w.j av  a2s  . 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:com.sg2net.utilities.ListaCAP.ListaComuniCapFromInternet.java

public Collection<Comune> donwloadAndParse() {
    try {/*  w  w w  .  ja va  2  s .c o  m*/
        List<Comune> comuni = new ArrayList<>();
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(ZIP_URL);
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            try (InputStream instream = entity.getContent();) {
                ZipInputStream zipInputStream = new ZipInputStream(instream);
                ZipEntry entry = zipInputStream.getNextEntry();

                if (entry != null) {
                    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                    byte data[] = new byte[BUFFER];
                    int count = 0;
                    while ((count = zipInputStream.read(data, 0, BUFFER)) != -1) {
                        outputStream.write(data, 0, count);
                    }
                    StringReader reader = new StringReader(
                            new String(outputStream.toByteArray(), HTML_ENCODING));
                    LineNumberReader lineReader = new LineNumberReader(reader);
                    String line;
                    int lineNumber = 0;
                    while ((line = lineReader.readLine()) != null) {
                        logger.trace("line " + (lineNumber + 1) + " from zip file=" + line);
                        if (lineNumber > 0) {
                            String[] values = line.split(";");
                            if (values.length >= 9) {
                                Comune comune = new Comune(values[CODICE_ISTAT_POS],
                                        values[CODICE_CATASTALE_POS], values[NOME_POS], values[PROVINCIA_POS]);
                                comuni.add(comune);
                                String capStr = values[CAP_POS];
                                Collection<String> capList;
                                if (capStr.endsWith("x")) {
                                    capList = getListaCAPFromHTMLPage(values[URL_COMUNE_POS]);
                                } else {
                                    capList = new HashSet<>();
                                    capList.add(capStr);
                                }
                                comune.setCodiciCap(capList);
                            }
                        }
                        lineNumber++;
                    }
                }
            }
        }
        return comuni;
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:com.ikon.util.ReportUtils.java

/**
 * Execute report//from  w ww.  j  a  v  a 2s . c om
 */
public static ByteArrayOutputStream execute(Report rp, final Map<String, Object> params, final int format)
        throws JRException, IOException, EvalError {
    ByteArrayOutputStream baos = null;
    ByteArrayInputStream bais = null;
    ZipInputStream zis = null;
    Session dbSession = null;

    try {
        baos = new ByteArrayOutputStream();
        bais = new ByteArrayInputStream(SecureStore.b64Decode(rp.getFileContent()));
        JasperReport jr = null;

        // Obtain or compile report
        if (ReportUtils.MIME_JRXML.equals(rp.getFileMime())) {
            log.debug("Report type: JRXML");
            jr = JasperCompileManager.compileReport(bais);
        } else if (ReportUtils.MIME_JASPER.equals(rp.getFileMime())) {
            log.debug("Report type: JASPER");
            jr = (JasperReport) JRLoader.loadObject(bais);
        } else if (ReportUtils.MIME_REPORT.equals(rp.getFileMime())) {
            zis = new ZipInputStream(bais);
            ZipEntry zi = null;

            while ((zi = zis.getNextEntry()) != null) {
                if (!zi.isDirectory()) {
                    String mimeType = MimeTypeConfig.mimeTypes.getContentType(zi.getName());

                    if (ReportUtils.MIME_JRXML.equals(mimeType)) {
                        log.debug("Report type: JRXML inside ARCHIVE");
                        jr = JasperCompileManager.compileReport(zis);
                        break;
                    } else if (ReportUtils.MIME_JASPER.equals(mimeType)) {
                        log.debug("Report type: JASPER inside ARCHIVE");
                        jr = (JasperReport) JRLoader.loadObject(zis);
                        break;
                    }
                }
            }
        }

        // Guess if SQL or BSH
        if (jr != null) {
            JRQuery query = jr.getQuery();
            String tq = query.getText().trim();

            if (tq.toUpperCase().startsWith("SELECT")) {
                log.debug("Report type: DATABASE");
                dbSession = HibernateUtil.getSessionFactory().openSession();
                executeDatabase(dbSession, baos, jr, params, format);
            } else {
                log.debug("Report type: SCRIPTING");
                ReportUtils.generateReport(baos, jr, params, format);
            }
        }

        return baos;
    } finally {
        HibernateUtil.close(dbSession);
        IOUtils.closeQuietly(bais);
        IOUtils.closeQuietly(baos);
        IOUtils.closeQuietly(zis);
    }
}

From source file:com.jbrisbin.vpc.jobsched.batch.BatchMessageConverter.java

public Object fromMessage(Message message) throws MessageConversionException {
    MessageProperties props = message.getMessageProperties();
    if (isAuthorized(props) && props.getContentType().equals("application/zip")) {
        BatchMessage msg = new BatchMessage();
        msg.setId(new String(props.getCorrelationId()));
        String timeout = "2";
        if (props.getHeaders().containsKey("timeout")) {
            timeout = props.getHeaders().get("timeout").toString();
        }//from   ww  w .j ava2 s .  c  o  m
        msg.setTimeout(Integer.parseInt(timeout));

        ZipInputStream zin = new ZipInputStream(new ByteArrayInputStream(message.getBody()));
        ZipEntry zentry;
        try {
            while (null != (zentry = zin.getNextEntry())) {
                byte[] buff = new byte[4096];
                StringWriter json = new StringWriter();
                for (int bytesRead = 0; bytesRead > -1; bytesRead = zin.read(buff)) {
                    json.write(new String(buff, 0, bytesRead));
                }
                msg.getMessages().put(zentry.getName(), json.toString());
            }
        } catch (IOException e) {
            throw new MessageConversionException(e.getMessage(), e);
        }

        return msg;
    } else {
        throw new MessageConversionException("Invalid security key.");
    }
}