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:org.pdfgal.pdfgalweb.utils.impl.ZipUtilsImpl.java

@Override
public List<String> saveFilesFromZip(final InputStream inputStream) throws IOException {

    final List<String> result = new ArrayList<String>();

    if (inputStream != null) {
        final ZipInputStream zis = new ZipInputStream(inputStream);

        ZipEntry entry = zis.getNextEntry();

        while (entry != null) {
            final byte[] buffer = new byte[1024];
            final String fileName = this.fileUtils.getAutogeneratedName(entry.getName());
            final File file = new File(fileName);

            final FileOutputStream fos = new FileOutputStream(file);

            int len;
            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }/*from  w w  w  .  j a v  a 2s . c om*/

            fos.close();
            result.add(fileName);
            entry = zis.getNextEntry();
        }

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

    return result;
}

From source file:com.obnsoft.ptcm3.MyApplication.java

private boolean downloadZipFile(String url, String target, String fileName, boolean force) {
    if (!force && getFileStreamPath(fileName).exists()) {
        return true;
    }/*  w  w  w .j  av  a  2s . c o m*/
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse httpResponse = httpclient.execute(new HttpGet(url));
        ZipInputStream zin = new ZipInputStream(httpResponse.getEntity().getContent());
        for (ZipEntry entry = zin.getNextEntry(); entry != null; entry = zin.getNextEntry()) {
            if (target.equals(entry.getName())) {
                OutputStream out = openFileOutput(fileName, MODE_PRIVATE);
                byte[] buffer = new byte[1024 * 1024];
                int length;
                while ((length = zin.read(buffer)) >= 0) {
                    out.write(buffer, 0, length);
                }
                out.close();
                break;
            }
        }
        zin.close();
    } catch (Exception e) {
        e.printStackTrace();
        getFileStreamPath(fileName).delete();
        return false;
    }
    return true;
}

From source file:com.taobao.android.builder.tools.zip.ZipUtils.java

/**
 * zip//from ww  w. ja va 2 s. c om
 *
 * @param zipFile
 * @param file
 * @param destPath
 * @param overwrite ?
 * @throws java.io.IOException
 */
public static void addFileToZipFile(File zipFile, File outZipFile, File file, String destPath,
        boolean overwrite) throws IOException {
    byte[] buf = new byte[1024];
    ZipInputStream zin = new ZipInputStream(new FileInputStream(zipFile));
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outZipFile));
    ZipEntry entry = zin.getNextEntry();
    boolean addFile = true;
    while (entry != null) {
        boolean addEntry = true;
        String name = entry.getName();
        if (StringUtils.equalsIgnoreCase(name, destPath)) {
            if (overwrite) {
                addEntry = false;
            } else {
                addFile = false;
            }
        }
        if (addEntry) {
            ZipEntry zipEntry = null;
            if (STORED == entry.getMethod()) {
                zipEntry = new ZipEntry(entry);
            } else {
                zipEntry = new ZipEntry(name);
            }
            out.putNextEntry(zipEntry);
            int len;
            while ((len = zin.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        }
        entry = zin.getNextEntry();
    }

    if (addFile) {
        InputStream in = new FileInputStream(file);
        // Add ZIP entry to output stream.
        ZipEntry zipEntry = new ZipEntry(destPath);
        out.putNextEntry(zipEntry);
        // Transfer bytes from the file to the ZIP file
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        // Complete the entry
        out.closeEntry();
        in.close();
    }
    // Close the streams
    zin.close();
    out.close();
}

From source file:org.olat.course.assessment.bulk.DataStepForm.java

private void processReturnFiles(VFSLeaf target, List<BulkAssessmentRow> rows) {
    Map<String, BulkAssessmentRow> assessedIdToRow = new HashMap<>();
    for (BulkAssessmentRow row : rows) {
        assessedIdToRow.put(row.getAssessedId(), row);
    }//from   ww w . ja v  a 2s . co  m

    if (target.exists()) {
        InputStream is = target.getInputStream();
        File parentTarget = ((LocalImpl) target).getBasefile().getParentFile();
        ZipInputStream zis = new ZipInputStream(is);

        ZipEntry entry;
        try {
            byte[] b = new byte[FileUtils.BSIZE];
            while ((entry = zis.getNextEntry()) != null) {
                if (!entry.isDirectory()) {
                    while (zis.read(b) > 0) {
                        //continue
                    }

                    Path op = new File(parentTarget, entry.getName()).toPath();
                    if (!Files.isHidden(op) && !Files.isDirectory(op)) {
                        Path parentDir = op.getParent();
                        String assessedId = parentDir.getFileName().toString();
                        String filename = op.getFileName().toString();

                        BulkAssessmentRow row;
                        if (assessedIdToRow.containsKey(assessedId)) {
                            row = assessedIdToRow.get(assessedId);
                        } else {
                            row = new BulkAssessmentRow();
                            row.setAssessedId(assessedId);
                            assessedIdToRow.put(assessedId, row);
                            rows.add(row);
                        }

                        if (row.getReturnFiles() == null) {
                            row.setReturnFiles(new ArrayList<String>(2));
                        }
                        row.getReturnFiles().add(filename);
                    }
                }
            }
        } catch (Exception e) {
            logError("", e);
        } finally {
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(zis);
        }
    }
}

From source file:org.nuxeo.template.processors.docx.WordXMLRawTemplateProcessor.java

public String readPropertyFile(InputStream in) throws IOException {
    ZipInputStream zIn = new ZipInputStream(in);
    ZipEntry zipEntry = zIn.getNextEntry();
    String xmlContent = null;/*from  w w w .ja  v  a2  s.  c  o  m*/
    while (zipEntry != null) {
        if (zipEntry.getName().equals("docProps/custom.xml")) {
            StringBuilder sb = new StringBuilder();
            byte[] buffer = new byte[BUFFER_SIZE];
            int read;
            while ((read = zIn.read(buffer)) != -1) {
                sb.append(new String(buffer, 0, read));
            }
            xmlContent = sb.toString();
            break;
        }
        zipEntry = zIn.getNextEntry();
    }
    zIn.close();
    return xmlContent;
}

From source file:org.junitee.testngee.ant.AntTask.java

public void execute() throws BuildException {
    validateOptions();/*  w w w.  j a  va 2s .  c  o  m*/

    HttpClient client = new HttpClient();

    MultipartPostMethod method = new MultipartPostMethod(runnerUrl);

    if (null != m_isJUnit) {
        if (m_isJUnit.booleanValue()) {
            method.addParameter("isJunit", "true");
        }
    }

    //    if (null != m_verbose) {
    //      cmd.createArgument().setValue(TestNGCommandLineArgs.LOG);
    //      cmd.createArgument().setValue(m_verbose.toString());
    //    }

    if ((null != m_outputDir)) {
        if (!m_outputDir.exists()) {
            m_outputDir.mkdirs();
        }
        if (!m_outputDir.isDirectory()) {
            throw new BuildException("Output directory is not a directory: " + m_outputDir);
        }
    }

    if ((null != m_testjar)) {
        method.addParameter("testjar", m_testjar.getName());
    }

    if (null != m_groups) {
        method.addParameter("groups", m_groups);
    }

    if (m_classFilesets.size() > 0) {
        for (String file : fileset(m_classFilesets)) {
            method.addParameter("classfile", file);
        }
    }

    if (m_xmlFilesets.size() > 0) {
        for (String file : fileset(m_xmlFilesets)) {
            try {
                method.addParameter("file", new File(file));
            } catch (FileNotFoundException e) {
                throw new BuildException(e);
            }
        }
    }

    int exitValue = -1;

    try {
        client.executeMethod(method);
        InputStream in = method.getResponseBodyAsStream();
        ZipInputStream zipIn = new ZipInputStream(in);
        ZipEntry zipEntry;

        while ((zipEntry = zipIn.getNextEntry()) != null) {
            byte[] buffer = new byte[4096];
            File file = new File(m_outputDir, zipEntry.getName());
            file.getParentFile().mkdirs();
            FileOutputStream out = new FileOutputStream(file);
            int r;

            while ((r = zipIn.read(buffer)) != -1) {
                out.write(buffer, 0, r);
            }
            out.close();
            zipIn.closeEntry();
        }

        zipIn.close();
        exitValue = 0;
    } catch (IOException e) {
        throw new BuildException(e);
    }
    actOnResult(exitValue);
}

From source file:org.apache.axis2.rmi.deploy.RMIServiceDeployer.java

private InputStream getConfigFileInputStream(String zipFilePath) throws IOException {

    ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipFilePath));
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ZipEntry zipEntry;/*from w  w w  . j  a v  a  2s.c  o m*/
    byte[] buffer = new byte[1024];
    int read;
    while ((zipEntry = zipInputStream.getNextEntry()) != null) {
        if (zipEntry.getName().equals("META-INF" + File.separator + "config.xml")) {
            while ((read = zipInputStream.read(buffer)) > 0) {
                byteArrayOutputStream.write(buffer, 0, read);
            }
        }

    }
    return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
}

From source file:org.apache.hadoop.hbase.util.MigrationTest.java

private void unzip(ZipInputStream zip, FileSystem dfs, Path rootDir) throws IOException {
    ZipEntry e = null;/* w  w w .  ja  v a 2s . com*/
    while ((e = zip.getNextEntry()) != null) {
        if (e.isDirectory()) {
            dfs.mkdirs(new Path(rootDir, e.getName()));
        } else {
            FSDataOutputStream out = dfs.create(new Path(rootDir, e.getName()));
            byte[] buffer = new byte[4096];
            int len;
            do {
                len = zip.read(buffer);
                if (len > 0) {
                    out.write(buffer, 0, len);
                }
            } while (len > 0);
            out.close();
        }
        zip.closeEntry();
    }
}

From source file:com.cloudera.recordservice.tests.ClusterConfiguration.java

/**
 * This method unzips a conf dir return through the CM api and returns the
 * path of the unzipped directory as a string
 *//*from ww w .j  a  v  a 2  s.co m*/
private String unzipConf(String zipFileName) throws IOException {
    int num = rand_.nextInt(10000000);
    String outDir = "/tmp/" + "confDir" + "_" + num + "/";
    byte[] buffer = new byte[4096];
    FileInputStream fis;
    String filename = "";
    fis = new FileInputStream(zipFileName);
    ZipInputStream zis = new ZipInputStream(fis);
    ZipEntry ze = zis.getNextEntry();
    while (ze != null) {
        filename = ze.getName();
        File unzippedFile = new File(outDir + filename);
        // Ensure that parent directories exist
        new File(unzippedFile.getParent()).mkdirs();
        FileOutputStream fos = new FileOutputStream(unzippedFile);
        int len;
        while ((len = zis.read(buffer)) > 0) {
            fos.write(buffer, 0, len);
        }
        fos.close();
        zis.closeEntry();
        ze = zis.getNextEntry();
    }
    zis.closeEntry();
    zis.close();
    fis.close();
    return outDir + "recordservice-conf/";
}

From source file:org.vosao.business.impl.plugin.PluginLoader.java

private Map<String, WarItem> readWar(byte[] data) throws IOException {
    ByteArrayInputStream inputData = new ByteArrayInputStream(data);
    ZipInputStream in = new ZipInputStream(inputData);
    Map<String, WarItem> map = new HashMap<String, WarItem>();
    ZipEntry entry;//ww w .  ja  v a2  s . com
    byte[] buffer = new byte[4096];
    while ((entry = in.getNextEntry()) != null) {
        if (!entry.isDirectory()) {
            ByteArrayOutputStream itemData = new ByteArrayOutputStream();
            int len = 0;
            while ((len = in.read(buffer)) > 0) {
                itemData.write(buffer, 0, len);
            }
            WarItem item = new WarItem(entry.getName(), itemData);
            map.put(entry.getName(), item);
        }
    }
    in.close();
    return map;
}