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:Main.java

public static void unZip(InputStream zipFileIS, String outputFolder) throws IOException {

    byte[] buffer = new byte[1024];

    //create output directory is not exists
    File folder = new File(outputFolder);
    if (!folder.exists()) {
        folder.mkdir();/* ww w .j  av  a  2s.  c o  m*/
    }

    //get the zip file content
    ZipInputStream zis = new ZipInputStream(zipFileIS);
    //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);

        if (ze.isDirectory()) {
            newFile.mkdirs();
        } else {

            FileOutputStream fos = new FileOutputStream(newFile);

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

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

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

From source file:Main.java

private static void loadICUData(Context context, File destDir) throws IOException {
    OutputStream out = null;//  ww w. j  a v  a 2 s.c  o m
    ZipInputStream in = null;
    File icuDir = new File(destDir, "icu");
    File icuDataFile = new File(icuDir, "icudt53l.dat");
    try {
        if (!icuDir.exists())
            icuDir.mkdirs();
        if (!icuDataFile.exists()) {
            in = new ZipInputStream(context.getAssets().open("icudt53l.zip"));
            in.getNextEntry();
            out = new FileOutputStream(icuDataFile);
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        }
    } catch (IOException e) {
        if (icuDataFile.exists())
            icuDataFile.delete();
        throw e;
    } finally {
        if (in != null)
            in.close();
        if (out != null) {
            out.flush();
            out.close();
        }
    }
}

From source file:nl.surfsara.warcexamples.hadoop.ldps.LDPRMapper.java

public static void unZip(InputStream zippedIS, File outputFolder) throws IOException {
    byte[] buffer = new byte[1024];

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

    while (ze != null) {
        String fileName = ze.getName();
        File newFile = new File(outputFolder + File.separator + fileName);
        new File(newFile.getParent()).mkdirs();
        FileOutputStream fos = new FileOutputStream(newFile);
        int len;//from   w w  w.j  av  a  2s.c  om
        while ((len = zis.read(buffer)) > 0) {
            fos.write(buffer, 0, len);
        }
        fos.close();
        ze = zis.getNextEntry();
    }

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

From source file:Utils.java

/**
 * unpack a segment from a zip/*from  w ww.j a v  a 2  s .com*/
 * 
 * @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;

    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());

        f.getParentFile().mkdirs();

        fout = new FileOutputStream(f);
        int len;
        while ((len = zin.read(buffer)) > 0) {
            fout.write(buffer, 0, len);
        }
        zin.closeEntry();
        fout.close();
        f.setLastModified(ts);
    }
    fout.close();
}

From source file:org.openecomp.sdc.ZipUtil.java

private static Map<String, byte[]> readZip(byte[] zipAsBytes) {

    Map<String, byte[]> fileNameToByteArray = new HashMap<String, byte[]>();

    byte[] buffer = new byte[1024];
    ZipInputStream zis = null;
    try {//  w  ww  .  j a  v  a2 s. co m

        zis = new ZipInputStream(new ByteArrayInputStream(zipAsBytes));
        // get the zipped file list entry
        ZipEntry ze = zis.getNextEntry();

        while (ze != null) {

            String fileName = ze.getName();

            if (false == ze.isDirectory()) {

                ByteArrayOutputStream os = new ByteArrayOutputStream();
                try {
                    int len;
                    while ((len = zis.read(buffer)) > 0) {
                        os.write(buffer, 0, len);
                    }

                    // aClass.outputStreamMethod(os);
                    String aString = new String(os.toByteArray(), "UTF-8");

                    fileNameToByteArray.put(fileName, os.toByteArray());

                } finally {
                    if (os != null) {
                        os.close();
                    }
                }
            }
            ze = zis.getNextEntry();

        }

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

    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    } finally {
        if (zis != null) {
            try {
                zis.closeEntry();
                zis.close();
            } catch (IOException e) {
                // TODO: add log
            }

        }
    }

    return fileNameToByteArray;

}

From source file:org.zaproxy.libs.DownloadTools.java

public static void downloadDriver(String urlStr, String destDir, String destFile) {
    File dest = new File(destDir + destFile);
    if (dest.exists()) {
        System.out.println("Already exists: " + dest.getAbsolutePath());
        return;/* w  w  w. ja  v a2s .  co  m*/
    }
    File parent = dest.getParentFile();
    if (!parent.exists() && !parent.mkdirs()) {
        System.out.println("Failed to create directory : " + dest.getParentFile().getAbsolutePath());
    }
    byte[] buffer = new byte[1024];
    if (urlStr.endsWith(".zip")) {
        try {
            URL url = new URL(urlStr);
            ZipInputStream zipIn = new ZipInputStream(url.openStream());
            ZipEntry entry;

            boolean isFound = false;
            while ((entry = zipIn.getNextEntry()) != null) {
                if (destFile.equals(entry.getName())) {
                    isFound = true;
                    FileOutputStream out = new FileOutputStream(dest);
                    int read = 0;
                    while ((read = zipIn.read(buffer)) != -1) {
                        out.write(buffer, 0, read);
                    }
                    out.close();
                    System.out.println("Updated: " + dest.getAbsolutePath());

                } else {
                    System.out.println("Found " + entry.getName());
                }
                zipIn.closeEntry();
                entry = zipIn.getNextEntry();
            }

            zipIn.close();

            if (!isFound) {
                System.out.println("Failed to find " + destFile);
                System.exit(1);
            }

        } catch (IOException ex) {
            ex.printStackTrace();
            System.exit(1);
        }
    } else if (urlStr.endsWith(".tar.gz")) {
        try {
            URL url = new URL(urlStr);
            GZIPInputStream gzis = new GZIPInputStream(url.openStream());

            File tarFile = new File(dest.getAbsolutePath() + ".tar");
            FileOutputStream out = new FileOutputStream(tarFile);

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

            gzis.close();
            out.close();

            TarArchiveInputStream tar = new TarArchiveInputStream(new FileInputStream(tarFile));
            ArchiveEntry entry;
            boolean isFound = false;
            while ((entry = tar.getNextEntry()) != null) {
                if (destFile.equals(entry.getName())) {
                    out = new FileOutputStream(dest);
                    isFound = true;

                    int read = 0;
                    while ((read = tar.read(buffer)) != -1) {
                        out.write(buffer, 0, read);
                    }
                    out.close();
                    System.out.println("Updated: " + dest.getAbsolutePath());
                }
            }
            tar.close();
            tarFile.delete();
            if (!isFound) {
                System.out.println("Failed to find " + destFile);
                System.exit(1);
            }

        } catch (IOException ex) {
            ex.printStackTrace();
            System.exit(1);
        }
    }
}

From source file:Main.java

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

    try {/* w ww . j a  v 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);

            System.out.println("file unzip : " + newFile.getAbsoluteFile());
            if (ze.isDirectory()) {
                newFile.mkdirs();
            } else {
                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();
        System.out.println("Done");
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:com.microsoft.intellij.AzurePlugin.java

private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
    BufferedOutputStream bos = new BufferedOutputStream(new java.io.FileOutputStream(filePath));
    byte[] bytesIn = new byte[1024 * 10];
    int read = 0;
    while ((read = zipIn.read(bytesIn)) != -1) {
        bos.write(bytesIn, 0, read);/*from w  w  w  .  j ava2 s  .c o m*/
    }
    bos.close();
}

From source file:localization.split.java

public static Vector<String> readzipfile(String filepath) {
    Vector<String> v = new Vector<String>();
    byte[] buffer = new byte[1024];
    String outputFolder = filepath.substring(0, filepath.lastIndexOf("."));
    System.out.println(outputFolder);
    try {//from w w w .j a  v a2s. c  o  m
        File folder = new File(outputFolder);
        if (!folder.exists()) {
            folder.mkdir();
        }
        ZipInputStream zis = new ZipInputStream(new FileInputStream(filepath));
        ZipEntry ze = zis.getNextEntry();
        while (ze != null) {
            String fileName = ze.getName();
            File newFile = new File(outputFolder + "\\" + fileName);
            v.addElement(newFile.getAbsolutePath());
            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 (Exception e) {

    }
    return v;
}

From source file:org.openecomp.sdc.common.util.ZipUtil.java

public static Map<String, byte[]> readZip(ZipInputStream zis) {

    Map<String, byte[]> fileNameToByteArray = new HashMap<String, byte[]>();

    byte[] buffer = new byte[1024];
    try {/*  ww w . java  2s  .co  m*/
        // get the zipped file list entry
        ZipEntry ze = zis.getNextEntry();

        while (ze != null) {

            String fileName = ze.getName();

            if (false == ze.isDirectory()) {

                ByteArrayOutputStream os = new ByteArrayOutputStream();
                try {
                    int len;
                    while ((len = zis.read(buffer)) > 0) {
                        os.write(buffer, 0, len);
                    }

                    fileNameToByteArray.put(fileName, os.toByteArray());

                } finally {
                    if (os != null) {
                        os.close();
                    }
                }
            }
            ze = zis.getNextEntry();

        }

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

    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    } finally {
        if (zis != null) {
            try {
                zis.closeEntry();
                zis.close();
            } catch (IOException e) {
                // TODO: add log
            }

        }
    }

    return fileNameToByteArray;

}