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

public static File UpdateZipFromPath(String sZipFile, String sPath) throws Exception {
    File tmpFile = File.createTempFile("z4zip-tmp-", ".zip");
    tmpFile.deleteOnExit();/*from  w w w. j av a  2  s  .  c o m*/
    ZipFile inZip = new ZipFile(sZipFile);
    ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(tmpFile.getPath()));

    Enumeration<? extends ZipEntry> entries = inZip.entries();
    while (entries.hasMoreElements()) {
        ZipEntry e = entries.nextElement();
        outZip.putNextEntry(e);
        if (!e.isDirectory()) {
            File f = new File(sPath + "/" + e.getName());
            if (f.exists()) {
                copy(new FileInputStream(f.getPath()), outZip);
            } else {
                copy(inZip.getInputStream(e), outZip);
            }
        }
        outZip.closeEntry();
    }
    inZip.close();
    outZip.close();
    return tmpFile;
}

From source file:org.geosdi.geoplatform.services.utility.PublishUtility.java

public static void extractEntryToFile(ZipEntry entry, ZipFile zipSrc, String tempUserDir)
        throws ResourceNotFoundFault {
    String entryName;//  ww  w .java2s.  co m
    FileOutputStream fileoutputstream = null;
    InputStream zipinputstream = null;
    try {
        zipinputstream = zipSrc.getInputStream(entry);
        int lastIndex = entry.getName().lastIndexOf('/');
        entryName = entry.getName().substring(lastIndex + 1).toLowerCase();
        String fileName = entryName.toLowerCase();
        String fileExtension = extractFileExtension(fileName);
        fileName = extractFileName(fileName);
        entryName = removeSpecialCharactersFromString(fileName) + "." + fileExtension;
        logger.info("INFO: Found file " + entryName);
        fileoutputstream = new FileOutputStream(tempUserDir + entryName);
        byte[] buf = new byte[1024];
        int n;
        while ((n = zipinputstream.read(buf, 0, 1024)) > -1) {
            fileoutputstream.write(buf, 0, n);
        }
    } catch (IOException ioe) {
        logger.error("ERROR on extractEntryToFile(): " + ioe.getMessage());
        throw new ResourceNotFoundFault(ioe.getMessage());
    } finally {
        try {
            fileoutputstream.close();
            zipinputstream.close();
        } catch (IOException e) {
        }
    }
}

From source file:de.langmi.spring.batch.examples.readers.file.zip.ZipMultiResourceItemReader.java

/**
 * Extract only files from the zip archive.
 *
 * @param currentZipFile/*from   www.j  a  v a2  s . c o m*/
 * @param extractedResources
 * @throws IOException 
 */
private static void extractFiles(final ZipFile currentZipFile, final List<Resource> extractedResources)
        throws IOException {
    Enumeration<? extends ZipEntry> zipEntryEnum = currentZipFile.entries();
    while (zipEntryEnum.hasMoreElements()) {
        ZipEntry zipEntry = zipEntryEnum.nextElement();
        LOG.info("extracting:" + zipEntry.getName());
        // traverse directories
        if (!zipEntry.isDirectory()) {
            // add inputStream
            extractedResources
                    .add(new InputStreamResource(currentZipFile.getInputStream(zipEntry), zipEntry.getName()));
            LOG.info("using extracted file:" + zipEntry.getName());
        }
    }
}

From source file:gov.va.oia.terminology.converters.sharedUtils.Unzip.java

public final static void unzip(File zipFile, File rootDir) throws IOException {
    ZipFile zip = new ZipFile(zipFile);
    @SuppressWarnings("unchecked")
    Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zip.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        java.io.File f = new java.io.File(rootDir, entry.getName());
        if (entry.isDirectory()) {
            f.mkdirs();/*w w w.j a v a 2 s .  co  m*/
            continue;
        } else {
            f.createNewFile();
        }
        InputStream is = null;
        OutputStream os = null;
        try {
            is = zip.getInputStream(entry);
            os = new FileOutputStream(f);
            IOUtils.copy(is, os);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (Exception e) {
                    // noop
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (Exception e) {
                    // noop
                }
            }
        }
    }
    zip.close();
}

From source file:Zip.java

/**
 * Reads a Zip file, iterating through each entry and dumping the contents
 * to the console./*from w ww .  j a v a 2s.  co m*/
 */
public static void readZipFile(String fileName) {
    ZipFile zipFile = null;
    try {
        // ZipFile offers an Enumeration of all the files in the Zip file
        zipFile = new ZipFile(fileName);
        for (Enumeration e = zipFile.entries(); e.hasMoreElements();) {
            ZipEntry zipEntry = (ZipEntry) e.nextElement();
            System.out.println(zipEntry.getName() + " contains:");
            // use BufferedReader to get one line at a time
            BufferedReader zipReader = new BufferedReader(
                    new InputStreamReader(zipFile.getInputStream(zipEntry)));
            while (zipReader.ready()) {
                System.out.println(zipReader.readLine());
            }
            zipReader.close();
        }
    } catch (IOException ioe) {
        System.out.println("An IOException occurred: " + ioe.getMessage());
    } finally {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (IOException ioe) {
            }
        }
    }
}

From source file:Main.java

private static void extractFileToPath(ZipFile nar, String targetPath, ZipEntry e, boolean ignorename,
        boolean strip) throws FileNotFoundException, IOException {
    File f = null;/*  w  w w. j  a  v  a2s .  c  om*/
    if (ignorename)
        f = new File(targetPath);
    else {

        f = new File(targetPath + "/" + (strip ? stripExtraLevel(e.getName()) : e.getName()));
    }
    File fP = f.getParentFile();
    if (fP != null && fP.exists() == false) {
        boolean s = fP.mkdirs();
        Log.d(TAG, "fp make" + s);
    }
    FileOutputStream os = new FileOutputStream(f);
    InputStream is = nar.getInputStream(e);
    copyFile(is, os);
}

From source file:net.chris54721.infinitycubed.utils.Utils.java

public static void unzip(File zipFile, File outputFolder) {
    InputStream in = null;//from   w  ww  .jav a2s  . co  m
    OutputStream out = null;
    try {
        ZipFile zip = new ZipFile(zipFile);
        Enumeration<? extends ZipEntry> entries = zip.entries();
        if (!outputFolder.isDirectory())
            outputFolder.mkdirs();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            File entryTarget = new File(outputFolder, entry.getName());
            entryTarget.getParentFile().mkdirs();
            if (entry.isDirectory())
                entryTarget.mkdirs();
            else {
                in = zip.getInputStream(entry);
                out = new FileOutputStream(entryTarget);
                IOUtils.copy(in, out);
            }
        }
    } catch (Exception e) {
        LogHelper.error("Failed extracting " + zipFile.getName(), e);
    } finally {
        if (in != null)
            IOUtils.closeQuietly(in);
        if (out != null)
            IOUtils.closeQuietly(out);
    }
}

From source file:org.codice.ddf.admin.application.service.impl.ApplicationFileInstaller.java

/**
 * Does the grunt work of parsing through the features.xml file provided using
 * {@link ZipFile#getInputStream(ZipEntry)}. Currently we only parse the main feature which is
 * denoted by install=auto.//from w  w w.  j ava2s  .c  o  m
 *
 * @param zipFile
 *            zip file get the {@link InputStream} from.
 * @param featureZipEntry
 *            the specific file pointing to the features.xml file.
 * @return The {@link ZipFileApplicationDetails} of the given features.xml file.
 * @throws ApplicationServiceException
 *             Any error that occurs within this method will be wrapped in this.
 * @throws ParserConfigurationException
 * @throws IOException
 * @throws SAXException
 */
private static ZipFileApplicationDetails getAppDetailsFromFeature(ZipFile zipFile, ZipEntry featureZipEntry)
        throws ParserConfigurationException, SAXException, IOException {

    // double check if this zip entry is a features.xml named file.
    if (!isFeatureFile(featureZipEntry)) {
        return null;
    }

    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(zipFile.getInputStream(featureZipEntry));

    NodeList featureNodes = doc.getElementsByTagName(FEATURE_TAG_NAME);

    for (int i = 0; i < featureNodes.getLength(); i++) {
        Node curNode = featureNodes.item(i);
        if (curNode.getAttributes() != null
                && curNode.getAttributes().getNamedItem(INSTALL_ATTRIBUTE_NAME) != null
                && Feature.DEFAULT_INSTALL_MODE.equals(
                        curNode.getAttributes().getNamedItem(INSTALL_ATTRIBUTE_NAME).getTextContent())) {
            String name = curNode.getAttributes().getNamedItem(NAME_ATTRIBUTE_NAME).getTextContent();
            String version = curNode.getAttributes().getNamedItem(VERSION_ATTRIBUTE_NAME).getTextContent();

            return new ZipFileApplicationDetails(name, version);
        }
    }
    return null;
}

From source file:org.jboss.tools.openshift.reddeer.utils.FileHelper.java

private static void unzipEntry(ZipFile zipfile, ZipEntry entry, File outputDirectory) {

    if (entry.isDirectory()) {
        createDirectory(new File(outputDirectory, entry.getName()));
        return;//from   w  ww.j av  a2 s. com
    }

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

    BufferedInputStream inputStream = null;
    BufferedOutputStream outputStream = null;
    try {
        inputStream = new BufferedInputStream(zipfile.getInputStream(entry));
        outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
        copy(inputStream, outputStream, 1024);
    } catch (IOException ex) {
    } finally {
        try {
            outputStream.close();
        } catch (Exception ex) {
        }
        try {
            inputStream.close();
        } catch (Exception ex) {
        }
    }
    outputFile.setExecutable(true);
    outputFile.setReadable(true);
    outputFile.setWritable(true);
}

From source file:com.ibm.util.merge.CompareArchives.java

/**
 * @param zip1/*from w ww .ja v a2  s.c om*/
 * @param files1
 * @param zip2
 * @param files2
 * @throws IOException
 */
private static final void assertMembersEqual(ZipFile zip1, HashMap<String, ZipEntry> files1, ZipFile zip2,
        HashMap<String, ZipEntry> files2) throws IOException {
    if (files1.size() != files2.size()) {
        fail("Different Sizes, expected " + Integer.toString(files1.size()) + " found "
                + Integer.toString(files2.size()));
    }

    for (String key : files1.keySet()) {
        if (!files2.containsKey(key)) {
            fail("Expected file not in target " + key);
        }
        String file1 = IOUtils.toString(zip1.getInputStream(files1.get(key)));
        String file2 = IOUtils.toString(zip2.getInputStream(files2.get(key)));
        assertEquals(file1, file2);
    }
}