Example usage for java.util.zip ZipFile getInputStream

List of usage examples for java.util.zip ZipFile getInputStream

Introduction

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

Prototype

public InputStream getInputStream(ZipEntry entry) throws IOException 

Source Link

Document

Returns an input stream for reading the contents of the specified zip file entry.

Usage

From source file:com.dustindoloff.s3websitedeploy.Main.java

private static boolean upload(final AmazonS3 s3Client, final String bucket, final ZipFile zipFile) {
    boolean failed = false;
    final ObjectMetadata data = new ObjectMetadata();
    final Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        final ZipEntry entry = entries.nextElement();
        data.setContentLength(entry.getSize());
        try {// w  w  w . j  av a 2  s .  c  om
            s3Client.putObject(bucket, entry.getName(), zipFile.getInputStream(entry), data);
        } catch (final AmazonClientException | IOException e) {
            failed = true;
        }
    }
    return !failed;
}

From source file:com.isomorphic.maven.util.ArchiveUtils.java

/**
 * Unzips the <code>source</code> file to the <code>target</code> directory.
 * /*  w w  w .  j a  v  a 2s .co  m*/
 * @param source The file to be unzipped
 * @param target The directory to which the source file should be unzipped
 * @throws IOException
 */
public static void unzip(File source, File target) throws IOException {

    ZipFile zip = new ZipFile(source);
    Enumeration<? extends ZipEntry> entries = zip.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        File file = new File(target, entry.getName());
        FileUtils.copyInputStreamToFile(zip.getInputStream(entry), file);
    }
}

From source file:it.geosolutions.geofence.gui.server.utility.IoUtility.java

/**
 * Decompress./*w ww  . j a va  2s.c o m*/
 *
 * @param prefix
 *            the prefix
 * @param inputFile
 *            the input file
 * @param tempFile
 *            the temp file
 * @return the file
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static File decompress(final String prefix, final File inputFile, final File tempFile)
        throws IOException {
    final File tmpDestDir = createTodayPrefixedDirectory(prefix, new File(tempFile.getParent()));

    ZipFile zipFile = new ZipFile(inputFile);

    Enumeration<? extends ZipEntry> entries = zipFile.entries();

    while (entries.hasMoreElements()) {

        ZipEntry entry = (ZipEntry) entries.nextElement();
        InputStream stream = zipFile.getInputStream(entry);

        if (entry.isDirectory()) {
            // Assume directories are stored parents first then
            // children.
            (new File(tmpDestDir, entry.getName())).mkdir();

            continue;
        }

        File newFile = new File(tmpDestDir, entry.getName());

        FileOutputStream fos = new FileOutputStream(newFile);
        try {
            byte[] buf = new byte[1024];
            int len;

            while ((len = stream.read(buf)) >= 0) {
                saveCompressedStream(buf, fos, len);
            }

        } catch (IOException e) {
            zipFile.close();

            IOException ioe = new IOException("Not valid ZIP archive file type.");
            ioe.initCause(e);
            throw ioe;
        } finally {
            fos.flush();
            fos.close();

            stream.close();
        }
    }
    zipFile.close();

    if ((tmpDestDir.listFiles().length == 1) && (tmpDestDir.listFiles()[0].isDirectory())) {
        return getShpFile(tmpDestDir.listFiles()[0]);
    }

    // File[] files = tmpDestDir.listFiles(new FilenameFilter() {
    //
    // public boolean accept(File dir, String name) {
    // return FilenameUtils.getExtension(name).equalsIgnoreCase("shp");
    // }
    // });
    //
    // return files.length > 0 ? files[0] : null;

    return getShpFile(tmpDestDir);
}

From source file:eurecom.constrained.devices.ReadZipFile.java

public static void unzip(final ZipFile zipfile, final File directory) throws IOException {

    final Enumeration<? extends ZipEntry> entries = zipfile.entries();
    while (entries.hasMoreElements()) {
        final ZipEntry entry = entries.nextElement();
        final File file = file(directory, entry);
        if (entry.isDirectory()) {
            continue;
        }/*ww  w  .j a  v  a  2 s  . co m*/
        final InputStream input = zipfile.getInputStream(entry);
        try {
            // copy bytes from input to file
        } finally {
            input.close();
        }
    }
}

From source file:net.sourceforge.atunes.utils.ZipUtils.java

/**
 * Unzips a zip entry in a directory//from  w w w .j  a v a 2 s. c o  m
 * 
 * @param zipfile
 * @param entry
 * @param outputDir
 * @throws IOException
 */
private static void unzipEntry(ZipFile zipfile, ZipEntry entry, File outputDir) throws IOException {
    if (entry.isDirectory()) {
        createDir(new File(outputDir, entry.getName()));
        return;
    }
    File outputFile = new File(outputDir, entry.getName());
    if (!outputFile.getParentFile().exists()) {
        createDir(outputFile.getParentFile());
    }
    BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry));
    BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
    try {
        IOUtils.copy(inputStream, outputStream);
    } finally {
        ClosingUtils.close(outputStream);
        ClosingUtils.close(inputStream);
    }
}

From source file:org.digidoc4j.utils.Helper.java

/**
 * Get the signature from a file./*w  w w . j av  a2s . c o m*/
 *
 * @param file  file containing the container
 * @param index index of the signature file
 * @return signature
 * @throws IOException when the signature is not found
 */
public static String extractSignature(String file, int index) throws IOException {
    ZipFile zipFile = new ZipFile(file);
    String signatureFileName = "META-INF/signatures" + index + ".xml";
    ZipEntry entry = zipFile.getEntry(signatureFileName);

    if (entry == null)
        throw new IOException(signatureFileName + " does not exists in archive: " + file);

    InputStream inputStream = zipFile.getInputStream(entry);
    String signatureContent = IOUtils.toString(inputStream, "UTF-8");

    zipFile.close();
    inputStream.close();

    return signatureContent;
}

From source file:org.flowerplatform.core.CoreUtils.java

private static void unzipEntry(ZipFile zipfile, ZipEntry entry, File outputDir) throws IOException {
    if (entry.isDirectory()) {
        new File(outputDir, entry.getName()).mkdirs();
        return;/*from  www.  j  av a2 s  . c  om*/
    }

    File outputFile = new File(outputDir, entry.getName());
    if (!outputFile.getParentFile().exists()) {
        outputFile.getParentFile().mkdirs();
    }

    BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry));
    BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));

    try {
        IOUtils.copy(inputStream, outputStream);
    } finally {
        outputStream.close();
        inputStream.close();
    }
}

From source file:com.google.gdt.eclipse.designer.webkit.WebKitSupportWin32.java

private static boolean deployNeeded(ZipFile zipFile) {
    if (WEBKIT_DIR.exists()) {
        // check if changed
        try {/*from   www . j a v a 2s .co  m*/
            File versionFile = new File(WEBKIT_DIR, WEBKIT_VERSION_NAME);
            if (!versionFile.exists()) {
                return true;
            }
            String currentVersion = readString(new FileInputStream(versionFile));
            // new version
            String newVersion = readString(zipFile.getInputStream(zipFile.getEntry(WEBKIT_VERSION_NAME)));
            return !currentVersion.equals(newVersion);
        } catch (Throwable e) {
            // ignore, means deploy needed
        }
    }
    return true;
}

From source file:Main.java

public static void readZipFile(String file) throws Exception {
    ZipFile zf = new ZipFile(file);
    InputStream in = new BufferedInputStream(new FileInputStream(file));
    ZipInputStream zin = new ZipInputStream(in);
    ZipEntry ze;/*from   w w w  .  j a va2  s  .  c om*/
    while ((ze = zin.getNextEntry()) != null) {
        if (ze.isDirectory()) {
        } else {
            System.err.println("file - " + ze.getName() + " : " + ze.getSize() + " bytes");
            long size = ze.getSize();
            if (size > 0) {
                BufferedReader br = new BufferedReader(new InputStreamReader(zf.getInputStream(ze)));
                String line;
                while ((line = br.readLine()) != null) {
                    System.out.println(line);
                }
                br.close();
            }
            System.out.println();
        }
    }
    zin.closeEntry();
}

From source file:Utils.java

/**
 * Unpack a zip file//from  w w w.java2  s. c  o m
 * 
 * @param theFile
 * @param targetDir
 * @return the file
 * @throws IOException
 */
public static File unpackArchive(File theFile, File targetDir) throws IOException {
    if (!theFile.exists()) {
        throw new IOException(theFile.getAbsolutePath() + " does not exist");
    }
    if (!buildDirectory(targetDir)) {
        throw new IOException("Could not create directory: " + targetDir);
    }
    ZipFile zipFile = new ZipFile(theFile);
    for (Enumeration entries = zipFile.entries(); entries.hasMoreElements();) {
        ZipEntry entry = (ZipEntry) entries.nextElement();
        File file = new File(targetDir, File.separator + entry.getName());
        if (!buildDirectory(file.getParentFile())) {
            throw new IOException("Could not create directory: " + file.getParentFile());
        }
        if (!entry.isDirectory()) {
            copyInputStream(zipFile.getInputStream(entry),
                    new BufferedOutputStream(new FileOutputStream(file)));
        } else {
            if (!buildDirectory(file)) {
                throw new IOException("Could not create directory: " + file);
            }
        }
    }
    zipFile.close();
    return theFile;
}