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.sakaiproject.search.util.FileUtils.java

/**
 * unpack a segment from a zip/*from ww  w  . j  a  v  a  2s  .c om*/
 * 
 * @param addsi
 * @param packetStream
 * @param version
 */
public static void unpack(InputStream source, File destination) throws IOException {
    ZipInputStream zin = new ZipInputStream(source);
    ZipEntry zipEntry = null;
    FileOutputStream fout = null;
    try {
        byte[] buffer = new byte[4096];
        while ((zipEntry = zin.getNextEntry()) != null) {

            long ts = zipEntry.getTime();
            // the zip entry needs to be a full path from the
            // searchIndexDirectory... hence this is correct

            File f = new File(destination, zipEntry.getName());
            if (log.isDebugEnabled())
                log.debug("         Unpack " + f.getAbsolutePath());
            if (!f.getParentFile().exists()) {
                if (!f.getParentFile().mkdirs()) {
                    log.warn("unpack(): Failed to create parent folder: " + f.getParentFile().getPath());
                }
            }

            fout = new FileOutputStream(f);
            int len;
            while ((len = zin.read(buffer)) > 0) {
                fout.write(buffer, 0, len);
            }
            zin.closeEntry();
            fout.close();
            if (!f.setLastModified(ts)) {
                log.warn("upack(): failes to set modified date on " + f.getPath());
            }
        }
    } finally {
        try {
            fout.close();
        } catch (Exception ex) {
            log.warn("Exception closing file output stream", ex);
        }
    }
}

From source file:org.solmix.commons.util.Files.java

/**
 * zip/*w w w . ja v a 2s.c  o  m*/
 * :?ZipOutputStreamZipInputStreamzip.
 * :java.util.zip??,zip??,
 * :"Exception  in thread "main " java.lang.IllegalArgumentException 
 * at java.util.zip.ZipInputStream.getUTF8String(ZipInputStream.java:285)
 * @param srcDir
 *            ?
 * @param destDir
 *            ?
 * @throws Exception
 */
public static void unZip(String srcDir, String destDir) throws IOException {
    int leng = 0;
    byte[] b = new byte[1024 * 2];
    /** ?zip? **/
    File[] zipFiles = new ExtensionFileFilter("zip").getFiles(srcDir);
    if (zipFiles != null && !"".equals(zipFiles)) {
        for (int i = 0; i < zipFiles.length; i++) {
            File file = zipFiles[i];
            /** ? * */
            ZipInputStream zis = new ZipInputStream(new FileInputStream(file));
            ZipEntry entry = null;
            while ((entry = zis.getNextEntry()) != null) {
                File destFile = null;
                if (destDir.endsWith(File.separator)) {
                    destFile = new File(destDir + entry.getName());
                } else {
                    destFile = new File(destDir + File.separator + entry.getName());
                }
                /** ? * */
                FileOutputStream fos = new FileOutputStream(destFile);
                while ((leng = zis.read(b)) != -1) {
                    fos.write(b, 0, leng);
                }
                fos.close();
            }
            zis.close();
        }
    }
}

From source file:com.hazelcast.stabilizer.Utils.java

public static void unzip(byte[] content, final File destinationDir) throws IOException {
    byte[] buffer = new byte[1024];

    ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(content));
    ZipEntry zipEntry = zis.getNextEntry();

    while (zipEntry != null) {

        String fileName = zipEntry.getName();
        File file = new File(destinationDir + File.separator + fileName);

        //            log.finest("Unzipping: " + file.getAbsolutePath());

        if (zipEntry.isDirectory()) {
            file.mkdirs();/*from   w  w w . ja v a  2 s  .  co  m*/
        } else {
            file.getParentFile().mkdirs();

            FileOutputStream fos = new FileOutputStream(file);
            try {
                int len;
                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }
            } finally {
                closeQuietly(fos);
            }
        }

        zipEntry = zis.getNextEntry();
    }

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

From source file:org.ngrinder.common.util.CompressionUtils.java

/**
 * Unzip the given input stream into destination directory with the given
 * character set./*  w  ww  . j av  a  2s  .  c om*/
 *
 * @param is          input stream
 * @param destDir     destination directory
 * @param charsetName character set name
 */
public static void unzip(InputStream is, File destDir, String charsetName) {
    byte[] buffer = new byte[1024];
    ZipInputStream zis = null;
    FileOutputStream fos = null;
    try {
        File folder = destDir;
        if (!folder.exists()) {
            folder.mkdir();
        }

        zis = new ZipInputStream(is);
        ZipEntry ze = zis.getNextEntry();

        while (ze != null) {
            String fileName = ze.getName();

            File newFile = new File(destDir.getAbsolutePath(), fileName);
            if (newFile.getPath().contains("..")) {
                throw new IllegalArgumentException("zip entry should not contain .. in the path.");
            }
            if (ze.isDirectory()) {
                newFile.mkdirs();
            } else {
                fos = new FileOutputStream(newFile);
                int len;
                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }
                IOUtils.closeQuietly(fos);
            }

            ze = zis.getNextEntry();
        }
    } catch (Exception e) {
        throw processException(e);
    } finally {
        IOUtils.closeQuietly(fos);
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(zis);
    }
}

From source file:eu.scape_project.archiventory.container.ZipContainer.java

/**
 * Extracts a zip entry (file entry)/*from   w w  w  .ja  va 2s. c o m*/
 * @param zipIn
 * @param filePath
 * @throws IOException
 */
private void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
    byte[] bytesIn = new byte[BUFFER_SIZE];
    int read = 0;
    while ((read = zipIn.read(bytesIn)) != -1) {
        bos.write(bytesIn, 0, read);
    }
    bos.close();
    if ((new File(filePath).exists())) {
        String id = filePath.replaceFirst("[0-9]{10,30}", "");
        //id = id.replace(this.tmpDirName"/tmp", "");
        // key-value pair
        this.put(filePath, filePath);
    }
}

From source file:com.geocent.owf.openlayers.handler.KmzHandler.java

@Override
public String handleContent(HttpServletResponse response, InputStream responseStream) throws IOException {
    ZipInputStream zis = new ZipInputStream(responseStream);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];

    ZipEntry ze = zis.getNextEntry();
    while (ze != null) {
        if (ze.getName().endsWith("kml")) {
            int len;
            while ((len = zis.read(buffer)) > 0) {
                baos.write(buffer, 0, len);
            }/* w  w  w.j  a  v a  2s.c  o  m*/

            response.setContentType(ContentTypes.KML.getContentType());
            return new String(baos.toByteArray(), Charset.defaultCharset());
        }

        ze = zis.getNextEntry();
    }

    throw new IOException("Missing KML file entry.");
}

From source file:coral.reef.web.FileUploadController.java

/**
 * Extracts a zip entry (file entry)/* ww  w .jav  a2  s  .co m*/
 * 
 * @param zipIn
 * @param filePath
 * @throws IOException
 */
private void extractFile(ZipInputStream zipIn, File filePath) throws IOException {
    filePath.getParentFile().mkdirs();
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
    byte[] bytesIn = new byte[BUFFER_SIZE];
    int read = 0;
    while ((read = zipIn.read(bytesIn)) != -1) {
        bos.write(bytesIn, 0, read);
    }
    bos.close();
}

From source file:org.bonitasoft.console.common.server.utils.FormsResourcesUtils.java

private static void unzipContentToFolder(final byte[] zipContent, final File targetFolder) throws IOException {
    ByteArrayInputStream is = null;
    ZipInputStream zis = null;
    FileOutputStream out = null;/*from  w  w  w  .  j  ava 2s .  c  o m*/
    try {
        is = new ByteArrayInputStream(zipContent);
        zis = new ZipInputStream(is);
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            final String entryName = entry.getName();
            if (entryName.endsWith(".jar")) {
                final File file = new File(targetFolder, entryName);
                if (file.exists()) {
                    file.delete();
                }
                file.createNewFile();
                out = new FileOutputStream(file);
                int len = 0;
                final byte[] buffer = new byte[1024];
                while ((len = zis.read(buffer)) > 0) {
                    out.write(buffer, 0, len);
                }
                out.close();
            }
        }
    } finally {
        if (is != null) {
            is.close();
        }
        if (zis != null) {
            zis.close();
        }
        if (out != null) {
            out.close();
        }
    }
}

From source file:org.wso2.ppaas.configurator.tests.ConfiguratorTestManager.java

private void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
    byte[] bytesIn = new byte[4096];
    int read;/*from   w  ww . j  a v  a 2 s .  c o  m*/
    while ((read = zipIn.read(bytesIn)) != -1) {
        bos.write(bytesIn, 0, read);
    }
    bos.close();
}

From source file:org.activiti.designer.kickstart.eclipse.sync.SyncUtil.java

protected static IFile downloadZipFile(IProject project, Document document, final Shell shell,
        IProgressMonitor monitor) throws Exception {
    IFolder tempzipFolder = project.getFolder("tempzip");
    if (tempzipFolder.exists()) {
        tempzipFolder.delete(true, monitor);
    }//from  w w  w. j ava2s  . c  o m

    tempzipFolder.create(true, true, monitor);

    IFile file = tempzipFolder.getFile(new Path(document.getName()));
    file.create(CmisUtil.downloadDocument(document), true, null);

    IFile openFile = null;
    byte[] buffer = new byte[1024];
    if ("zip".equalsIgnoreCase(file.getFileExtension())) {
        final ZipInputStream zis = new ZipInputStream(new FileInputStream(file.getLocation().toFile()));
        boolean hasMoreEntries = true;
        IFile processFile = null;
        while (hasMoreEntries) {
            ZipEntry entry = zis.getNextEntry();
            if (entry != null) {
                IFile unzippedFile = tempzipFolder.getFile(entry.getName());
                if ("kickproc".equalsIgnoreCase(unzippedFile.getFileExtension())) {
                    processFile = unzippedFile;
                }
                String filePath = unzippedFile.getLocationURI().getPath();
                File extractFile = new File(filePath);
                FileOutputStream fos = new FileOutputStream(extractFile);
                int len;
                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }

                fos.close();
            } else {
                hasMoreEntries = false;
            }
        }
        zis.close();

        tempzipFolder.getProject().refreshLocal(IResource.DEPTH_INFINITE, monitor);
        if (processFile != null) {
            openFile = processWorkflowDefinition(processFile, tempzipFolder, document, shell, monitor);
        }
    }

    tempzipFolder.delete(true, monitor);
    project.refreshLocal(IResource.DEPTH_INFINITE, monitor);
    return openFile;
}