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:com.polyvi.xface.extension.zip.XZipExt.java

/**
 * ??zip?/*from w  w w.j av a  2 s .c  om*/
 *
 * @param dstFileUri
 *            
 * @param is
 *            ?zip?
 * @return ??
 * @throws IOException
 * @throws FileNotFoundException
 * @throws IllegalArgumentException
 */
private void unzipFileFromStream(Uri dstFileUri, InputStream is)
        throws IOException, FileNotFoundException, IllegalArgumentException {
    if (null == dstFileUri || null == is) {
        XLog.e(CLASS_NAME, "Method unzipFileFromStream: params is null");
        throw new IllegalArgumentException();
    }
    ZipInputStream zis = new ZipInputStream(is);
    ZipEntry entry = null;
    Uri unZipUri = null;
    while (null != (entry = zis.getNextEntry())) {
        File unZipFile = new File(dstFileUri.getPath() + File.separator + entry.getName());
        unZipUri = Uri.fromFile(unZipFile);
        if (entry.isDirectory()) {
            if (!unZipFile.exists()) {
                unZipFile.mkdirs();
            }
        } else {
            // ???
            prepareForZipDir(unZipUri);
            OutputStream fos = mResourceApi.openOutputStream(unZipUri);
            int readLen = 0;
            byte buffer[] = new byte[XConstant.BUFFER_LEN];
            while (-1 != (readLen = zis.read(buffer))) {
                fos.write(buffer, 0, readLen);
            }
            fos.close();
        }
    }
    zis.close();
    is.close();
}

From source file:org.geoserver.wps.gs.download.DownloadProcessTest.java

/**
 * This method is used for decoding an input file.
 * /*www  .  j  a v  a2 s .  c  o  m*/
 * @param input the input stream to decode
 * @param tempDirectory temporary directory on where the file is decoded.
 * @return the object the decoded file
 * @throws Exception the exception TODO review
 */
public static File decode(InputStream input, File tempDirectory) throws Exception {

    // unzip to the temporary directory
    ZipInputStream zis = null;
    try {
        zis = new ZipInputStream(input);
        ZipEntry entry = null;

        // Copy the whole file in the new position
        while ((entry = zis.getNextEntry()) != null) {
            File file = new File(tempDirectory, entry.getName());
            if (entry.isDirectory()) {
                file.mkdir();
            } else {
                int count;
                byte data[] = new byte[4096];
                // write the files to the disk
                FileOutputStream fos = null;
                try {
                    fos = new FileOutputStream(file);
                    while ((count = zis.read(data)) != -1) {
                        fos.write(data, 0, count);
                    }
                    fos.flush();
                } finally {
                    if (fos != null) {
                        org.apache.commons.io.IOUtils.closeQuietly(fos);
                    }
                }
            }
            zis.closeEntry();
        }
    } finally {
        if (zis != null) {
            org.apache.commons.io.IOUtils.closeQuietly(zis);
        }
    }

    return tempDirectory;
}

From source file:at.ac.tuwien.auto.iotsys.gateway.connectors.knx.KNXDeviceLoaderETSImpl.java

public void unZip(String zipFile, String outputFolder) {
    byte[] buffer = new byte[1024];

    try {/*from w  w  w.jav  a 2  s  .co  m*/
        File folder = new File(outputFolder);
        if (!folder.exists()) {
            folder.mkdir();
        }

        ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));

        ZipEntry ze = zis.getNextEntry();

        while (ze != null) {

            String fileName = ze.getName();
            File newFile = new File(outputFolder + File.separator + fileName);

            log.info("file unzip : " + newFile.getAbsoluteFile());

            new File(newFile.getParent()).mkdirs();

            FileOutputStream fos = new FileOutputStream(newFile);

            int len;
            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }

            fos.close();
            ze = zis.getNextEntry();
        }

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

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:nl.imvertor.common.file.ZipFile.java

/**
 * Unzip it/*from  www .  ja  v  a 2 s . com*/
 * @param zipFile input zip file
 * @param outputFolder zip file output folder
 * @param requestedFilePattern Pattern to match the file name.
 * 
 * @throws Exception 
 */
private void unZipIt(String zipFile, String outputFolder, Pattern requestedFilePattern) throws Exception {

    byte[] buffer = new byte[1024];

    //create output folder is not exists
    AnyFolder folder = new AnyFolder(outputFolder);
    folder.mkdir();

    //get the zip file content
    ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
    //get the zipped file list entry
    ZipEntry ze = zis.getNextEntry();

    while (ze != null) {
        if (!ze.isDirectory()) {
            String fileName = ze.getName();
            // if the pattern specified, use a matcher. Otherwise accept any file.
            Matcher m = (requestedFilePattern != null) ? requestedFilePattern.matcher(fileName) : null;
            if (requestedFilePattern == null || m.find()) {
                File newFile = new File(outputFolder + File.separator + fileName);
                //create all non exists folders
                //else you will hit FileNotFoundException for compressed folder
                new File(newFile.getParent()).mkdirs();
                FileOutputStream fos = new FileOutputStream(newFile);
                int len;
                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }
                fos.close();
            }
        }
        ze = zis.getNextEntry();
    }
    zis.closeEntry();
    zis.close();
}

From source file:org.jevis.commons.drivermanagment.ClassImporter.java

public List<File> unZipIt(String outputFolder, File zipFile) {
    List<File> files = new ArrayList<>();

    byte[] buffer = new byte[1024];

    try {// w  ww  . j ava  2s.  co m

        //create output directory is not exists
        File folder = new File(outputFolder);
        if (!folder.exists()) {
            folder.mkdir();
        }

        //get the zip file content
        ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
        //get the zipped file list entry
        ZipEntry ze = zis.getNextEntry();

        while (ze != null) {

            String fileName = ze.getName();
            File newFile = new File(outputFolder + File.separator + fileName);
            newFile.deleteOnExit();
            files.add(newFile);

            //                System.out.println("file unzip : " + newFile.getAbsoluteFile());
            //create all non exists folders
            //else you will hit FileNotFoundException for compressed folder
            new File(newFile.getParent()).mkdirs();

            FileOutputStream fos = new FileOutputStream(newFile);

            int len;
            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }

            fos.close();
            ze = zis.getNextEntry();
        }

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

    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return files;
}

From source file:org.inaturalist.android.GuideXML.java

/**
 * Extracts a downloaded NGZ file into the offline guide directory
 * @param ngzFilename the NGZ file path/*from  w ww  .j  av  a2  s .c  o  m*/
 * @return true/false status
 */
public boolean extractOfflineGuide(String ngzFilename) {
    // First, create the offline guide directory, if it doesn't exist
    File offlineGuidesDir = new File(mContext.getExternalCacheDir() + OFFLINE_GUIDE_PATH + mGuideId);
    offlineGuidesDir.mkdirs();

    // Next, extract the NGZ file into that directory
    String basePath = offlineGuidesDir.getPath();
    InputStream is;
    ZipInputStream zis;
    try {
        String filename;
        is = new FileInputStream(ngzFilename);
        zis = new ZipInputStream(new BufferedInputStream(is));
        ZipEntry ze;
        byte[] buffer = new byte[1024];
        int count;

        // Extract all files in the zip file - one by one
        while ((ze = zis.getNextEntry()) != null) {
            // Get current filename
            filename = ze.getName();

            // Need to create directories if doesn't exist, or it will generate an Exception...
            if (ze.isDirectory()) {
                File fmd = new File(basePath + "/" + filename);
                fmd.mkdirs();
                continue;
            }

            FileOutputStream fout = new FileOutputStream(basePath + "/" + filename);

            // Extract current file
            while ((count = zis.read(buffer)) != -1) {
                fout.write(buffer, 0, count);
            }

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

        zis.close();
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }

    return true;
}

From source file:isl.FIMS.utils.Utils.java

/**
 * Unzip it (This implementation works only when zip contains files-folders
 * with ASCII filenames Greek characters break the code!
 *
 * @param zipFile input zip file//from w ww. ja  v a  2s  .c  o m
 * @param output zip file output folder
 */
public static void unzip(String zipFile, String outputFolder) {

    String rootFolderName = "";
    String rootFlashFilename = "";
    byte[] buffer = new byte[1024];

    try {

        //get the zip file content
        ZipInputStream zis = new ZipInputStream(new FileInputStream(outputFolder + File.separator + zipFile));
        //get the zipped file list entry

        ZipEntry ze = zis.getNextEntry();

        boolean rootDirFound = false;
        boolean flashFileFound = false;
        while (ze != null) {

            String fileName = ze.getName();
            File newFile = new File(outputFolder + File.separator + fileName);
            //create all non exists folders
            //else you will hit FileNotFoundException for compressed folder
            if (!ze.getName().contains("__MACOSX")) {
                if (ze.isDirectory()) {
                    if (rootDirFound == false) {
                        rootFolderName = newFile.getName();
                        rootDirFound = true;
                    }
                    new File(newFile.getParent()).mkdirs();
                } else {
                    FileOutputStream fos = null;

                    new File(newFile.getParent()).mkdirs();

                    if (flashFileFound == false && newFile.getName().endsWith(".swf")
                            && !newFile.getName().startsWith(".")) {
                        rootFlashFilename = newFile.getName();
                        flashFileFound = true;
                    }

                    fos = new FileOutputStream(newFile);

                    int len;
                    while ((len = zis.read(buffer)) > 0) {
                        fos.write(buffer, 0, len);
                    }

                    fos.close();
                }
            }
            ze = zis.getNextEntry();
        }

        zis.closeEntry();
        zis.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:org.jboss.dashboard.workspace.GraphicElementManagerImpl.java

public synchronized void addFileToResource(final String name, final File file, final GraphicElement element,
        final String descriptorFileName) throws Exception {
    byte[] zipFile = element.getZipFile();
    byte[] buffer = new byte[65536];

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ZipOutputStream zos = new ZipOutputStream(bos);

    ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(zipFile));
    ZipEntry entry = null;//from ww w .  ja v  a 2s  .c o  m
    while ((entry = zis.getNextEntry()) != null) {
        String entryName = entry.getName();
        if (entryName.equals(descriptorFileName)) {
            Properties props = new Properties();
            props.load(zis);
            props.setProperty("resource." + name, name);
            zos.putNextEntry(new ZipEntry(entryName));
            props.store(zos, "");
            zos.closeEntry();
        } else if (entryName.equals(name)) {
            throw new Exception("Duplicated entry name: " + name);
        } else {
            zos.putNextEntry(new ZipEntry(entryName));
            int len = 0;
            while ((len = zis.read(buffer)) != -1) {
                zos.write(buffer, 0, len);
            }
            zos.closeEntry();
        }
    }

    InputStream in = new BufferedInputStream(new FileInputStream(file));
    zos.putNextEntry(new ZipEntry(name));
    int len = 0;
    while ((len = in.read(buffer)) != -1) {
        zos.write(buffer, 0, len);
    }
    zos.closeEntry();
    zos.close();
    element.setZipFile(bos.toByteArray());
    createOrUpdate(element);
}

From source file:configuration.Util.java

/**
 * This un-ZIP a the input_filename to the output_filename
 * @param input_filename// w  w w .j  av a  2  s.  com
 * @param output_filename
 * @return True if success
 */
public static boolean unzip(String input_filename, String output_filename) {
    try {
        ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(input_filename));
        OutputStream out = new FileOutputStream(output_filename);
        byte[] buffer = new byte[1024];
        int len;
        while ((len = zipInputStream.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }
        zipInputStream.close();
        out.close();
        return true;
    } catch (Exception e) {
        System.out.println("Unzip Failed!");
        System.out.println(e);
        e.printStackTrace();
        return false;
    }
}

From source file:org.yccheok.jstock.gui.IndicatorProjectManager.java

public boolean install(File zipFile) {
    // First, get the project name.
    final String projectName = IndicatorProjectManager.getProjectName(zipFile);
    if (projectName == null) {
        return false;
    }/* w ww . ja v  a2  s  .co  m*/

    final String jHotDrawFilename = projectName + JHOTDRAW_RESERVE_WORD + ".xml";
    final String operatorIndicatorFilename = projectName + ".xml";

    boolean status = true;
    /* projectName, operatorIndicatorFilename, jHotDrawFilename */
    ZipInputStream in = null;
    try {
        in = new ZipInputStream(new FileInputStream(zipFile));
        // Get the first entry
        ZipEntry zipEntry = null;

        while ((zipEntry = in.getNextEntry()) != null) {
            final String name = zipEntry.getName();
            String outFilename = null;

            if (name.equalsIgnoreCase(operatorIndicatorFilename)) {
                outFilename = this.getOperatorIndicatorFilename(projectName);
            } else if (name.equalsIgnoreCase(jHotDrawFilename)) {
                outFilename = this.getJHotDrawFilename(projectName);
            } else {
                assert (false);
            }

            OutputStream out = null;

            try {
                out = new FileOutputStream(outFilename);

                // Transfer bytes from the ZIP file to the output file
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
            } finally {
                org.yccheok.jstock.file.Utils.close(out);
                org.yccheok.jstock.file.Utils.closeEntry(in);
            }
        }
    } catch (IOException ex) {
        log.error(null, ex);
        status = false;
    } finally {
        org.yccheok.jstock.file.Utils.close(in);
    }

    if (status == false) {
        new File(this.getOperatorIndicatorFilename(projectName)).delete();
        new File(this.getJHotDrawFilename(projectName)).delete();
        return false;
    }

    final OperatorIndicator operatorIndicator = (OperatorIndicator) Utils.fromXML(OperatorIndicator.class,
            this.getOperatorIndicatorFilename(projectName));
    if (operatorIndicator == null || operatorIndicator.getType() != this.preferredOperatorIndicatorType) {
        new File(this.getOperatorIndicatorFilename(projectName)).delete();
        new File(this.getJHotDrawFilename(projectName)).delete();
        return false;
    }

    if (this.contains(projectName) == false) {
        this.projects.add(projectName);
    }
    return true;
}