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.cip.crane.agent.utils.FileExtractUtils.java

/**
 * Unzip an input file into an output file.
 * <p>/*from w  w w .  ja  v  a2  s  . co m*/
 * The output file is created in the output folder, having the same name
 * as the input file, minus the '.zip' extension. 
 * 
 * @param inputFile     the input .zip file
 * @param outputDir     the output directory file. 
 * @throws IOException 
 * @throws FileNotFoundException
 *  
 * @return  The {@File} with the ungzipped content.
 */
public static void unZip(final File inputFile, final File outputDir) throws FileNotFoundException, IOException {
    LOG.debug(
            String.format("Unzipping %s to dir %s.", inputFile.getAbsolutePath(), outputDir.getAbsolutePath()));

    byte[] buf = new byte[1024];
    ZipInputStream zipinputstream = null;
    ZipEntry zipentry;
    zipinputstream = new ZipInputStream(new FileInputStream(inputFile));

    zipentry = zipinputstream.getNextEntry();
    while (zipentry != null) {
        //for each entry to be extracted
        String entryName = outputDir + "/" + zipentry.getName();
        entryName = entryName.replace('/', File.separatorChar);
        entryName = entryName.replace('\\', File.separatorChar);
        int n;
        FileOutputStream fileoutputstream;
        File newFile = new File(entryName);

        if (zipentry.isDirectory()) {
            if (!newFile.mkdirs()) {
                break;
            }
            zipentry = zipinputstream.getNextEntry();
            continue;
        }
        fileoutputstream = new FileOutputStream(entryName);

        while ((n = zipinputstream.read(buf, 0, 1024)) > -1) {
            fileoutputstream.write(buf, 0, n);
        }

        fileoutputstream.close();
        zipinputstream.closeEntry();
        zipentry = zipinputstream.getNextEntry();

    } //while
    zipinputstream.close();
}

From source file:com.sangupta.jerry.util.ZipUtils.java

/**
 * Extract the given ZIP file into the given destination folder.
 * /*  w ww . j a  v a2  s .  c o  m*/
 * @param zipFile
 *            file to extract
 *            
 * @param baseFolder
 *            destination folder to extract in
 */
public static void extractZipToFolder(File zipFile, File baseFolder) {
    try {
        byte[] buf = new byte[1024];

        ZipInputStream zipinputstream = new ZipInputStream(new FileInputStream(zipFile));
        ZipEntry zipentry = zipinputstream.getNextEntry();

        while (zipentry != null) {
            // for each entry to be extracted
            String entryName = zipentry.getName();

            entryName = entryName.replace('/', File.separatorChar);
            entryName = entryName.replace('\\', File.separatorChar);

            int numBytes;
            FileOutputStream fileoutputstream;
            File newFile = new File(baseFolder, entryName);
            if (zipentry.isDirectory()) {
                if (!newFile.mkdirs()) {
                    break;
                }
                zipentry = zipinputstream.getNextEntry();
                continue;
            }

            fileoutputstream = new FileOutputStream(newFile);
            while ((numBytes = zipinputstream.read(buf, 0, 1024)) > -1) {
                fileoutputstream.write(buf, 0, numBytes);
            }

            fileoutputstream.close();
            zipinputstream.closeEntry();
            zipentry = zipinputstream.getNextEntry();
        }

        zipinputstream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.glaf.core.util.ZipUtils.java

public static Map<String, byte[]> getZipBytesMap(ZipInputStream zipInputStream) {
    Map<String, byte[]> zipMap = new java.util.HashMap<String, byte[]>();
    java.util.zip.ZipEntry zipEntry = null;
    ByteArrayOutputStream baos = null;
    BufferedOutputStream bos = null;
    byte tmpByte[] = null;
    try {/*from w ww  .  j  a  v a2 s .  c  o  m*/
        while ((zipEntry = zipInputStream.getNextEntry()) != null) {
            tmpByte = new byte[BUFFER];
            baos = new ByteArrayOutputStream();
            bos = new BufferedOutputStream(baos, BUFFER);
            int i = 0;
            while ((i = zipInputStream.read(tmpByte, 0, BUFFER)) != -1) {
                bos.write(tmpByte, 0, i);
            }
            bos.flush();
            byte[] bytes = baos.toByteArray();
            IOUtils.closeStream(baos);
            IOUtils.closeStream(baos);
            zipMap.put(zipEntry.getName(), bytes);
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        IOUtils.closeStream(baos);
        IOUtils.closeStream(baos);
    }
    return zipMap;
}

From source file:org.exoplatform.services.cms.impl.Utils.java

/**
 * get data from the version history file
 *
 * @param importHistorySourceStream/*from   w ww .j av a 2 s .c  o  m*/
 * @return
 * @throws Exception
 */
public static Map<String, String> getMapImportHistory(InputStream importHistorySourceStream) throws Exception {
    ZipInputStream zipInputStream = new ZipInputStream(importHistorySourceStream);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] data = new byte[1024];
    ZipEntry entry = zipInputStream.getNextEntry();
    Map<String, String> mapHistoryValue = new HashMap<String, String>();
    while (entry != null) {
        int available = -1;
        if (entry.getName().equals(MAPPING_FILE)) {
            while ((available = zipInputStream.read(data, 0, 1024)) > -1) {
                out.write(data, 0, available);
            }
            InputStream inputStream = new ByteArrayInputStream(out.toByteArray());
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
            String strLine;
            // Read File Line By Line
            while ((strLine = br.readLine()) != null) {
                // Put the history information into list
                if (strLine.indexOf("=") > -1) {
                    mapHistoryValue.put(strLine.split("=")[0], strLine.split("=")[1]);
                }
            }
            // Close the input stream
            inputStream.close();
            zipInputStream.closeEntry();
            break;
        }
        entry = zipInputStream.getNextEntry();
    }
    out.close();
    zipInputStream.close();
    return mapHistoryValue;
}

From source file:edu.uci.ics.asterix.event.service.AsterixEventServiceUtil.java

public static void unzip(String sourceFile, String destDir) throws IOException {
    BufferedOutputStream dest = null;
    FileInputStream fis = new FileInputStream(sourceFile);
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
    ZipEntry entry = null;/* w  w w .  j av  a 2 s  .c  o m*/

    int BUFFER_SIZE = 4096;
    while ((entry = zis.getNextEntry()) != null) {
        String dst = destDir + File.separator + entry.getName();
        if (entry.isDirectory()) {
            createDir(destDir, entry);
            continue;
        }
        int count;
        byte data[] = new byte[BUFFER_SIZE];

        // write the file to the disk
        FileOutputStream fos = new FileOutputStream(dst);
        dest = new BufferedOutputStream(fos, BUFFER_SIZE);
        while ((count = zis.read(data, 0, BUFFER_SIZE)) != -1) {
            dest.write(data, 0, count);
        }
        // close the output streams
        dest.flush();
        dest.close();
    }

    zis.close();
}

From source file:org.openo.nfvo.jujuvnfmadapter.common.UnCompressUtil.java

/**
 * zip//w ww  .  ja  v  a 2  s  .  co m
 * <br/>
 * 
 * @param sourceFile unzipPath
 * @param targetPath zippath
 * @param fileNames
 * @since NFVO 0.5
 */
public static boolean unCompressZip(String sourceFile, String targetPath, List<String> fileNames) {
    try {
        BufferedOutputStream dest = null;
        FileInputStream fis = new FileInputStream(sourceFile);
        ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
        ZipEntry entry = null;
        while ((entry = zis.getNextEntry()) != null) {
            String name = entry.getName();
            String[] names = name.split("/");
            String fileName = targetPath;
            for (int i = 0; i < names.length; i++) {
                String str = names[i];
                fileName = fileName + File.separator + str;
            }
            if (name.endsWith("/")) {
                FileUtils.mkDirs(fileName);
            } else {
                int count;
                byte data[] = new byte[2048];
                File file = getRealFileName(targetPath, name);
                fileNames.add(file.getName());
                dest = new BufferedOutputStream(new FileOutputStream(file));

                while ((count = zis.read(data, 0, 2048)) != -1) {
                    dest.write(data, 0, count);
                }
                log.debug("unzip to:" + file.getCanonicalPath());
                dest.flush();
                dest.close();
            }
        }
        zis.close();
        return true;
    } catch (Exception e) {
        log.error("UnCompressZip faield:", e);
    }
    return false;
}

From source file:JarMaker.java

/**
 * Unpack the jar file to a directory, till teaf files
 * @param file The source file name/*from   w ww  .  ja v  a  2  s.  co  m*/
 * @param file1 The target directory
 * @author wangxp
 * @version 2003/11/07
 */
public static void unpackAppJar(String file, String file1) throws IOException {

    // m_log.debug("unpackAppJar begin. file="+file+"|||file1="+file1);

    BufferedInputStream bufferedinputstream = new BufferedInputStream(new FileInputStream(file));
    ZipInputStream zipinputstream = new ZipInputStream(bufferedinputstream);
    byte abyte0[] = new byte[1024];
    for (ZipEntry zipentry = zipinputstream.getNextEntry(); zipentry != null; zipentry = zipinputstream
            .getNextEntry()) {
        File file2 = new File(file1, zipentry.getName());
        if (zipentry.isDirectory()) {
            if (!file2.exists() && !file2.mkdirs())
                throw new IOException("Could not make directory " + file2.getPath());
        } else {
            File file3 = file2.getParentFile();
            if (file3 != null && !file3.exists() && !file3.mkdirs())
                throw new IOException("Could not make directory " + file3.getPath());
            BufferedOutputStream bufferedoutputstream = new BufferedOutputStream(new FileOutputStream(file2));
            int i;
            try {
                while ((i = zipinputstream.read(abyte0, 0, abyte0.length)) != -1)
                    bufferedoutputstream.write(abyte0, 0, i);
            } catch (IOException ie) {
                ie.printStackTrace();
                // m_logger.error(ie);
                throw ie;
            }
            bufferedoutputstream.close();
        }
    }
    zipinputstream.close();
    bufferedinputstream.close();
    // m_log.debug("unpackAppJar end.");
}

From source file:com.wavemaker.StudioInstallService.java

public static File unzipFile(File zipfile) {
    int BUFFER = 2048;

    String zipname = zipfile.getName();
    int extindex = zipname.lastIndexOf(".");

    try {/*from w ww . j  a va 2  s.co m*/
        File zipFolder = new File(zipfile.getParentFile(), zipname.substring(0, extindex));
        if (zipFolder.exists())
            org.apache.commons.io.FileUtils.deleteDirectory(zipFolder);
        zipFolder.mkdir();

        File currentDir = zipFolder;
        //File currentDir = zipfile.getParentFile();

        BufferedOutputStream dest = null;
        FileInputStream fis = new FileInputStream(zipfile.toString());
        ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            System.out.println("Extracting: " + entry);
            if (entry.isDirectory()) {
                File f = new File(currentDir, entry.getName());
                if (f.exists())
                    f.delete(); // relevant if this is the top level folder
                f.mkdir();
            } else {
                int count;
                byte data[] = new byte[BUFFER];
                //needed for non-dir file ace/ace.js created by 7zip
                File destFile = new File(currentDir, entry.getName());
                // write the files to the disk
                FileOutputStream fos = new FileOutputStream(currentDir.toString() + "/" + 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();

        return currentDir;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return (File) null;

}

From source file:com.glaf.core.util.ZipUtils.java

public static void unzip(File zipFile) {
    FileInputStream fileInputStream = null;
    ZipInputStream zipInputStream = null;
    FileOutputStream fileoutputstream = null;
    BufferedOutputStream bufferedoutputstream = null;
    try {//from ww w.  j a v a  2 s .  co m
        fileInputStream = new FileInputStream(zipFile);
        zipInputStream = new ZipInputStream(fileInputStream);
        java.util.zip.ZipEntry zipEntry;
        while ((zipEntry = zipInputStream.getNextEntry()) != null) {
            byte abyte0[] = new byte[BUFFER];
            fileoutputstream = new FileOutputStream(zipEntry.getName());
            bufferedoutputstream = new BufferedOutputStream(fileoutputstream, BUFFER);
            int i;
            while ((i = zipInputStream.read(abyte0, 0, BUFFER)) != -1) {
                bufferedoutputstream.write(abyte0, 0, i);
            }
            bufferedoutputstream.flush();
            IOUtils.closeStream(fileoutputstream);
            IOUtils.closeStream(bufferedoutputstream);
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        IOUtils.closeStream(fileInputStream);
        IOUtils.closeStream(zipInputStream);
        IOUtils.closeStream(fileoutputstream);
        IOUtils.closeStream(bufferedoutputstream);
    }
}

From source file:com.edgenius.core.util.ZipFileUtil.java

public static void expandZipToFolder(InputStream is, String destFolder) throws ZipFileUtilException {
    // got our directory, so write out the input file and expand the zip file
    // this is really a hack - write it out temporarily then read it back in again! urg!!!!
    ZipInputStream zis = new ZipInputStream(is);
    int count;/*  w w  w  .  j  a  va2s  . c o  m*/
    byte data[] = new byte[BUFFER_SIZE];
    ZipEntry entry = null;
    BufferedOutputStream dest = null;
    String entryName = null;

    try {

        // work through each file, creating a node for each file
        while ((entry = zis.getNextEntry()) != null) {
            entryName = entry.getName();
            if (!entry.isDirectory()) {

                String destName = destFolder + File.separator + entryName;
                //It must sort out the directory information to current OS. 
                //e.g, if zip file is zipped under windows, but unzip in linux.  
                destName = FileUtil.makeCanonicalPath(destName);

                prepareDirectory(destName);

                FileOutputStream fos = new FileOutputStream(destName);
                dest = new BufferedOutputStream(fos, BUFFER_SIZE);
                while ((count = zis.read(data, 0, BUFFER_SIZE)) != -1) {
                    dest.write(data, 0, count);
                }
                dest.flush();
                IOUtils.closeQuietly(dest);
                dest = null;
            } else {
                String destName = destFolder + File.separator + entryName;
                destName = FileUtil.makeCanonicalPath(destName);
                new File(destName).mkdirs();
            }

        }

    } catch (IOException ioe) {

        log.error("Exception occured processing entries in zip file. Entry was " + entryName, ioe);
        throw new ZipFileUtilException(
                "Exception occured processing entries in zip file. Entry was " + entryName, ioe);

    } finally {
        IOUtils.closeQuietly(dest);
        IOUtils.closeQuietly(zis);
    }
}