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, int off, int len) throws IOException 

Source Link

Document

Reads from the current ZIP entry into an array of bytes.

Usage

From source file:com.sg2net.utilities.ListaCAP.ListaComuniCapFromInternet.java

public Collection<Comune> donwloadAndParse() {
    try {//from w  w w.ja  v a  2s. 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:org.etudes.mneme.tool.UploadXml.java

/**
 * write zip file to disk/*from  w  ww.j a  v a2s  .  c o m*/
 * 
 * @param zis
 * @param name
 * @throws IOException
 */
private void unzip(ZipInputStream zis, String name) throws Exception {
    BufferedOutputStream dest = null;
    int count;
    byte data[] = new byte[2048];
    try {
        // write the files to the disk
        FileOutputStream fos = new FileOutputStream(name);
        dest = new BufferedOutputStream(fos, 2048);
        while ((count = zis.read(data, 0, 2048)) != -1) {
            dest.write(data, 0, count);
        }
        dest.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        throw e;
    } catch (IOException e) {
        throw e;
    } finally {
        try {
            if (dest != null)
                dest.close();
        } catch (IOException e1) {
        }
    }
}

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;/*from   w w  w.jav  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:JarResources.java

public JarResources(String jarFileName) throws Exception {
    this.jarFileName = jarFileName;
    ZipFile zf = new ZipFile(jarFileName);
    Enumeration e = zf.entries();
    while (e.hasMoreElements()) {
        ZipEntry ze = (ZipEntry) e.nextElement();

        htSizes.put(ze.getName(), new Integer((int) ze.getSize()));
    }/* w w w . j ava2  s.com*/
    zf.close();

    // extract resources and put them into the hashtable.
    FileInputStream fis = new FileInputStream(jarFileName);
    BufferedInputStream bis = new BufferedInputStream(fis);
    ZipInputStream zis = new ZipInputStream(bis);
    ZipEntry ze = null;
    while ((ze = zis.getNextEntry()) != null) {
        if (ze.isDirectory()) {
            continue;
        }

        int size = (int) ze.getSize();
        // -1 means unknown size.
        if (size == -1) {
            size = ((Integer) htSizes.get(ze.getName())).intValue();
        }

        byte[] b = new byte[(int) size];
        int rb = 0;
        int chunk = 0;
        while (((int) size - rb) > 0) {
            chunk = zis.read(b, rb, (int) size - rb);
            if (chunk == -1) {
                break;
            }
            rb += chunk;
        }

        htJarContents.put(ze.getName(), b);
    }
}

From source file:edu.ku.brc.helpers.ZipFileHelper.java

/**
 * @param zipFile/*from w w w  .  j  a  v  a 2 s  .c o  m*/
 * @return
 * @throws ZipException
 * @throws IOException
 */
public List<File> unzipToFiles(final File zipFile) throws ZipException, IOException {
    Vector<File> files = new Vector<File>();

    final int bufSize = 65535;

    File dir = UIRegistry.getAppDataSubDir(Long.toString(System.currentTimeMillis()) + "_zip", true);
    dirsToRemoveList.add(dir);

    File outFile = null;
    FileOutputStream fos = null;
    try {
        ZipInputStream zin = new ZipInputStream(new FileInputStream(zipFile));
        ZipEntry entry = zin.getNextEntry();
        while (entry != null) {
            if (zin.available() > 0) {
                outFile = new File(dir.getCanonicalPath() + File.separator + entry.getName());
                fos = new FileOutputStream(outFile);

                byte[] bytes = new byte[bufSize]; // 64k
                int bytesRead = zin.read(bytes, 0, bufSize);
                while (bytesRead > 0) {
                    fos.write(bytes, 0, bytesRead);
                    bytesRead = zin.read(bytes, 0, bufSize);
                }

                files.insertElementAt(outFile.getCanonicalFile(), 0);
            }
            entry = zin.getNextEntry();
        }

    } finally {
        if (fos != null) {
            fos.close();
        }
    }
    return files;
}

From source file:org.orbisgis.commons.utils.FileUtils.java

/**
 * Unzip an archive into the directory destDir.
 * @param zipFile/* w  w w.  j  a  va 2  s  . c o m*/
 * @param destDir
 * @throws IOException
 */
public static void unzip(File zipFile, File destDir) throws IOException {
    ZipInputStream zis = null;

    try {
        FileInputStream fis = new FileInputStream(zipFile);
        zis = new ZipInputStream(new BufferedInputStream(fis));
        ZipEntry entry = zis.getNextEntry();
        while (entry != null) {
            byte data[] = new byte[BUF_SIZE];
            // write the files to the disk
            File newFile = new File(destDir, entry.getName());
            File parentFile = newFile.getParentFile();
            if (!parentFile.exists() && !parentFile.mkdirs()) {
                throw new IOException("Cannot create directory:" + parentFile);
            }
            if (!entry.isDirectory()) {
                BufferedOutputStream dest = null;
                try {
                    FileOutputStream fos = new FileOutputStream(newFile);
                    dest = new BufferedOutputStream(fos, BUF_SIZE);
                    int count = zis.read(data, 0, BUF_SIZE);
                    while (count != -1) {
                        dest.write(data, 0, count);
                        count = zis.read(data, 0, BUF_SIZE);
                    }
                    dest.flush();
                } finally {
                    if (dest != null) {
                        dest.close();
                    }
                }
            }
            entry = zis.getNextEntry();
        }
    } finally {
        if (zis != null) {
            zis.close();
        }
    }
}

From source file:nl.nn.adapterframework.webcontrol.pipes.TestIfsaService.java

private String processZipFile(IPipeLineSession session, InputStream inputStream, String fileEncoding,
        String applicationId, String serviceId, String messageProtocol) throws IOException {
    String result = "";
    String lastState = null;/*from   ww w .  jav  a  2 s .  c  o m*/
    ZipInputStream archive = new ZipInputStream(inputStream);
    for (ZipEntry entry = archive.getNextEntry(); entry != null; entry = archive.getNextEntry()) {
        String name = entry.getName();
        int size = (int) entry.getSize();
        if (size > 0) {
            byte[] b = new byte[size];
            int rb = 0;
            int chunk = 0;
            while (((int) size - rb) > 0) {
                chunk = archive.read(b, rb, (int) size - rb);
                if (chunk == -1) {
                    break;
                }
                rb += chunk;
            }
            String message = XmlUtils.readXml(b, 0, rb, fileEncoding, false);
            if (StringUtils.isNotEmpty(result)) {
                result += "\n";
            }
            try {
                processMessage(applicationId, serviceId, messageProtocol, message);
                lastState = "success";
            } catch (Exception e) {
                lastState = "error";
            }
            result += name + ":" + lastState;
        }
        archive.closeEntry();
    }
    archive.close();
    session.put("result", result);
    return "";
}

From source file:nl.nn.adapterframework.webcontrol.api.SendJmsMessage.java

private void processZipFile(InputStream file, JmsSender qms, String replyTo) throws IOException {
    ZipInputStream archive = new ZipInputStream(file);
    for (ZipEntry entry = archive.getNextEntry(); entry != null; entry = archive.getNextEntry()) {
        String name = entry.getName();
        int size = (int) entry.getSize();
        if (size > 0) {
            byte[] b = new byte[size];
            int rb = 0;
            int chunk = 0;
            while ((size - rb) > 0) {
                chunk = archive.read(b, rb, size - rb);
                if (chunk == -1) {
                    break;
                }//from  ww  w.ja va 2  s.  c  o  m
                rb += chunk;
            }
            String currentMessage = XmlUtils.readXml(b, 0, rb, Misc.DEFAULT_INPUT_STREAM_ENCODING, false);
            // initiate MessageSender
            if ((replyTo != null) && (replyTo.length() > 0))
                qms.setReplyToName(replyTo);

            processMessage(qms, name + "_" + Misc.createSimpleUUID(), currentMessage);
        }
        archive.closeEntry();
    }
    archive.close();
}

From source file:edu.ku.brc.helpers.ZipFileHelper.java

/**
 * Unzips a a zip file cntaining just one file.
 * @param zipFile the backup file/*from   www.j ava 2s  .  c o m*/
 * @return the file of the new uncompressed back up file.
 */
public File unzipToSingleFile(final File zipFile) {
    final int bufSize = 102400;

    File outFile = null;
    try {
        ZipInputStream zin = new ZipInputStream(new FileInputStream(zipFile));
        ZipEntry entry = zin.getNextEntry();
        if (entry == null) {
            return null;
        }
        if (zin.available() == 0) {
            return null;
        }
        outFile = File.createTempFile("zip_", "sql");
        FileOutputStream fos = new FileOutputStream(outFile);

        byte[] bytes = new byte[bufSize]; // 10K
        int bytesRead = zin.read(bytes, 0, bufSize);
        while (bytesRead > 0) {
            fos.write(bytes, 0, bytesRead);
            bytesRead = zin.read(bytes, 0, 100);
        }

    } catch (ZipException ex) {
        edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
        edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(ZipFileHelper.class, ex);
        return null; //I think this means it is not a zip file.

    } catch (Exception ex) {
        edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
        edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(ZipFileHelper.class, ex);
        return null;
    }
    return outFile;
}

From source file:edu.harvard.i2b2.eclipse.plugins.metadataLoader.util.FileUtil.java

public static void unzip(String zipname) throws IOException {

    FileInputStream fis = new FileInputStream(zipname);
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
    //??    GZIPInputStream gzis = new GZIPInputStream(new BufferedInputStream(fis));

    // get directory of the zip file
    if (zipname.contains("\\"))
        zipname = zipname.replace("\\", "/");
    //   String zipDirectory = zipname.substring(0, zipname.lastIndexOf("/")+1) ;
    String zipDirectory = zipname.substring(0, zipname.lastIndexOf("."));
    new File(zipDirectory).mkdir();

    RunData.getInstance().setMetadataDirectory(zipDirectory);

    ZipEntry entry;/*from w  w w  .  j a va  2  s . com*/
    while ((entry = zis.getNextEntry()) != null) {
        System.out.println("Unzipping: " + entry.getName());
        if (entry.getName().contains("metadata")) {
            RunData.getInstance().setMetadataFile(zipDirectory + "/" + entry.getName());
        } else if (entry.getName().contains("schemes")) {
            RunData.getInstance().setSchemesFile(zipDirectory + "/" + entry.getName());
        } else if (entry.getName().contains("access")) {
            RunData.getInstance().setTableAccessFile(zipDirectory + "/" + entry.getName());
        }

        int size;
        byte[] buffer = new byte[2048];

        FileOutputStream fos = new FileOutputStream(zipDirectory + "/" + entry.getName());
        BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length);

        while ((size = zis.read(buffer, 0, buffer.length)) != -1) {
            bos.write(buffer, 0, size);
        }
        bos.flush();
        bos.close();
    }
    zis.close();
    fis.close();
}