Example usage for java.util.zip ZipInputStream ZipInputStream

List of usage examples for java.util.zip ZipInputStream ZipInputStream

Introduction

In this page you can find the example usage for java.util.zip ZipInputStream ZipInputStream.

Prototype

public ZipInputStream(InputStream in) 

Source Link

Document

Creates a new ZIP input stream.

Usage

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);
            }//from ww  w  .  j  av  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:cc.recommenders.utils.Zips.java

public static void unzip(File source, File dest) throws IOException {
    ZipInputStream zis = null;/*from w  w w  . ja v a 2 s . com*/
    try {
        InputSupplier<FileInputStream> fis = Files.newInputStreamSupplier(source);
        zis = new ZipInputStream(fis.getInput());
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            if (!entry.isDirectory()) {
                final File file = new File(dest, entry.getName());
                Files.createParentDirs(file);
                Files.write(ByteStreams.toByteArray(zis), file);
            }
        }
    } finally {
        Closeables.closeQuietly(zis);
    }
}

From source file:com.oprisnik.semdroid.app.parser.manifest.AXMLConverter.java

public static String getManifestString(byte[] apk) {
    String result = null;//from   w  w  w .  ja  v a  2s.c o m
    ZipInputStream zis = null;
    ByteArrayInputStream fis = null;
    try {

        fis = new ByteArrayInputStream(apk);

        zis = new ZipInputStream(fis);
        for (ZipEntry entry = zis.getNextEntry(); entry != null; entry = zis.getNextEntry()) {
            if (entry.getName().equals("AndroidManifest.xml")) {
                result = getManifestString(zis);
                break;
            }
        }
    } catch (FileNotFoundException e) {
        IOUtils.closeQuietly(fis);
    } catch (IOException e) {
    } finally {
        IOUtils.closeQuietly(zis);
    }
    return result;
}

From source file:Main.java

/**
 * Extract a zip resource into real files and directories
 * //  w  w  w .  j a v  a  2s  . c o m
 * @param in typically given as getResources().openRawResource(R.raw.something)
 * @param directory target directory
 * @param overwrite indicates whether to overwrite existing files
 * @return list of files that were unpacked (if overwrite is false, this list won't include files
 *         that existed before)
 * @throws IOException
 */
public static List<File> extractZipResource(InputStream in, File directory, boolean overwrite)
        throws IOException {
    final int BUFSIZE = 2048;
    byte buffer[] = new byte[BUFSIZE];
    ZipInputStream zin = new ZipInputStream(new BufferedInputStream(in, BUFSIZE));
    List<File> files = new ArrayList<File>();
    ZipEntry entry;
    directory.mkdirs();
    while ((entry = zin.getNextEntry()) != null) {
        File file = new File(directory, entry.getName());
        files.add(file);
        if (overwrite || !file.exists()) {
            if (entry.isDirectory()) {
                file.mkdirs();
            } else {
                file.getParentFile().mkdirs(); // Necessary because some zip files lack directory entries.
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file), BUFSIZE);
                int nRead;
                while ((nRead = zin.read(buffer, 0, BUFSIZE)) > 0) {
                    bos.write(buffer, 0, nRead);
                }
                bos.flush();
                bos.close();
            }
        }
    }
    zin.close();
    return files;
}

From source file:eionet.gdem.conversion.odf.OpenDocumentUtils.java

/**
 * Returns true, if inputstream is zip file
 * @param input InputStream//  www.j a  v  a  2 s  . c o  m
 * @return True if InputStream is a zip file.
 */
public static boolean isSpreadsheetFile(InputStream input) {

    ZipInputStream zipStream = null;
    ZipEntry zipEntry = null;
    try {
        zipStream = new ZipInputStream(input);
        while (zipStream.available() == 1 && (zipEntry = zipStream.getNextEntry()) != null) {
            if (zipEntry != null) {
                if ("content.xml".equals(zipEntry.getName())) {
                    // content file found, it is OpenDocument.
                    return true;
                }
            }
        }
    } catch (IOException ioe) {
        return false;
    } finally {
        IOUtils.closeQuietly(zipStream);
    }
    return false;

}

From source file:com.twemyeez.picklr.InstallManager.java

public static void unzip() {

    // Firstly get the working directory
    String workingDirectory = Minecraft.getMinecraft().mcDataDir.getAbsolutePath();

    // If it ends with a . then remove it
    if (workingDirectory.endsWith(".")) {
        workingDirectory = workingDirectory.substring(0, workingDirectory.length() - 1);
    }/*  w w  w .j  av a2s. c  om*/

    // If it doesn't end with a / then add it
    if (!workingDirectory.endsWith("/") && !workingDirectory.endsWith("\\")) {
        workingDirectory = workingDirectory + "/";
    }

    // Use a test file to see if libraries installed
    File file = new File(workingDirectory + "mods/mp3spi1.9.5.jar");

    // If the libraries are installed, return
    if (file.exists()) {
        System.out.println("Checking " + file.getAbsolutePath());
        System.out.println("Target file exists, so not downloading API");
        return;
    }

    // Now try to download the libraries
    try {

        String location = "http://www.javazoom.net/mp3spi/sources/mp3spi1.9.5.zip";

        // Define the URL
        URL url = new URL(location);

        // Get the ZipInputStream
        ZipInputStream zipInput = new ZipInputStream(new BufferedInputStream((url).openStream()));

        // Use a temporary ZipEntry as a buffer
        ZipEntry zipFile;

        // While there are more file entries
        while ((zipFile = zipInput.getNextEntry()) != null) {
            // Check if it is one of the file names that we want to copy
            Boolean required = false;
            if (zipFile.getName().indexOf("mp3spi1.9.5.jar") != -1) {
                required = true;
            }
            if (zipFile.getName().indexOf("jl1.0.1.jar") != -1) {
                required = true;
            }
            if (zipFile.getName().indexOf("tritonus_share.jar") != -1) {
                required = true;
            }
            if (zipFile.getName().indexOf("LICENSE.txt") != -1) {
                required = true;
            }

            // If it is, then we shall now copy it
            if (!zipFile.getName().replace("MpegAudioSPI1.9.5/", "").equals("") && required) {

                // Get the file location
                String tempFile = new File(zipFile.getName()).getName();

                tempFile = tempFile.replace("LICENSE.txt", "MpegAudioLicence.txt");

                // Initialise the target file
                File targetFile = (new File(
                        workingDirectory + "mods/" + tempFile.replace("MpegAudioSPI1.9.5/", "")));

                // Print a debug/alert message
                System.out.println("Picklr is extracting to " + workingDirectory + "mods/"
                        + tempFile.replace("MpegAudioSPI1.9.5/", ""));

                // Make parent directories if required
                targetFile.getParentFile().mkdirs();

                // If the file does not exist, create it
                if (!targetFile.exists()) {
                    targetFile.createNewFile();
                }

                // Create a buffered output stream to the destination
                BufferedOutputStream destinationOutput = new BufferedOutputStream(
                        new FileOutputStream(targetFile, false), 2048);

                // Store the data read
                int bytesRead;

                // Data buffer
                byte dataBuffer[] = new byte[2048];

                // While there is still data to write
                while ((bytesRead = zipInput.read(dataBuffer, 0, 2048)) != -1) {
                    // Write it to the output stream
                    destinationOutput.write(dataBuffer, 0, bytesRead);
                }

                // Flush the output
                destinationOutput.flush();

                // Close the output stream
                destinationOutput.close();
            }

        }
        // Close the zip input
        zipInput.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:Main.java

/** Returns true if these zip files act like equivalent sets.
 * The order of the zip entries is not important: if they contain
 * exactly the same contents, this returns true.
 * @param zip1 one zip file /* w  w  w. ja  v  a2 s . com*/
 * @param zip2 another zip file
 * @return true if the two zip archives are equivalent sets
 * @throws IOException
 */
public static boolean zipEquals(File zip1, File zip2) throws IOException {
    if (zip1.equals(zip2))
        return true;

    InputStream in = null;
    ZipInputStream zipIn = null;
    try {
        in = new FileInputStream(zip1);
        zipIn = new ZipInputStream(in);
        ZipEntry e = zipIn.getNextEntry();
        Vector<String> entries = new Vector<String>();
        while (e != null) {
            entries.add(e.getName());

            InputStream other = getZipEntry(zip2, e.getName());
            if (other == null) {
                return false;
            }

            if (equals(zipIn, other) == false) {
                return false;
            }
            e = zipIn.getNextEntry();
        }
        //now we've established everything in zip1 is in zip2

        //but what if zip2 has entries zip1 doesn't?
        zipIn.close();
        in.close();

        in = new FileInputStream(zip2);
        zipIn = new ZipInputStream(in);
        e = zipIn.getNextEntry();
        while (e != null) {
            if (entries.contains(e.getName()) == false) {
                return false;
            }
            e = zipIn.getNextEntry();
        }

        //the entries are exactly the same
        return true;
    } finally {
        try {
            zipIn.close();
        } catch (Throwable t) {
        }
        try {
            in.close();
        } catch (Throwable t) {
        }
    }
}

From source file:de.suse.swamp.util.FileUtils.java

/**
 * Decompress the provided Stream to "targetPath"
 *///  w  ww  .j ava 2 s .  co m
public static void uncompress(InputStream inputFile, String targetPath) throws Exception {
    ZipInputStream zin = new ZipInputStream(new BufferedInputStream(inputFile));
    BufferedOutputStream dest = null;
    ZipEntry entry;
    while ((entry = zin.getNextEntry()) != null) {
        int count;
        byte data[] = new byte[2048];
        // write the files to the disk
        if (entry.isDirectory()) {
            org.apache.commons.io.FileUtils.forceMkdir(new File(targetPath + "/" + entry.getName()));
        } else {
            FileOutputStream fos = new FileOutputStream(targetPath + "/" + entry.getName());
            dest = new BufferedOutputStream(fos, 2048);
            while ((count = zin.read(data, 0, 2048)) != -1) {
                dest.write(data, 0, count);
            }
            dest.flush();
            dest.close();
        }
    }
}

From source file:JarResources.java

public JarResources(String jarFileName) throws Exception {
    this.jarFileName = jarFileName;
    ZipFile zf = new ZipFile(jarFileName);
    Enumeration e = zf.entries();
    while (e.hasMoreElements()) {
        ZipEntry ze = (ZipEntry) e.nextElement();

        htSizes.put(ze.getName(), new Integer((int) ze.getSize()));
    }/* ww w.  j  a  va 2 s. co m*/
    zf.close();

    // extract resources and put them into the hashtable.
    FileInputStream fis = new FileInputStream(jarFileName);
    BufferedInputStream bis = new BufferedInputStream(fis);
    ZipInputStream zis = new ZipInputStream(bis);
    ZipEntry ze = null;
    while ((ze = zis.getNextEntry()) != null) {
        if (ze.isDirectory()) {
            continue;
        }

        int size = (int) ze.getSize();
        // -1 means unknown size.
        if (size == -1) {
            size = ((Integer) htSizes.get(ze.getName())).intValue();
        }

        byte[] b = new byte[(int) size];
        int rb = 0;
        int chunk = 0;
        while (((int) size - rb) > 0) {
            chunk = zis.read(b, rb, (int) size - rb);
            if (chunk == -1) {
                break;
            }
            rb += chunk;
        }

        htJarContents.put(ze.getName(), b);
    }
}

From source file:cd.education.data.collector.android.utilities.ZipUtils.java

public static File extractFirstZipEntry(File zipFile, boolean deleteAfterUnzip) throws IOException {
    ZipInputStream zipInputStream = null;
    File targetFile = null;//from w ww . j a v  a2  s.  c  om
    try {
        zipInputStream = new ZipInputStream(new FileInputStream(zipFile));
        ZipEntry zipEntry = zipInputStream.getNextEntry();
        if (zipEntry != null) {
            targetFile = doExtractInTheSameFolder(zipFile, zipInputStream, zipEntry);
        }
    } finally {
        IOUtils.closeQuietly(zipInputStream);
    }

    if (deleteAfterUnzip && targetFile != null && targetFile.exists()) {
        FileUtils.deleteAndReport(zipFile);
    }

    return targetFile;
}