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.aurel.track.lucene.util.FileUtil.java

public static void unzipFile(File uploadZip, File unzipDestinationDirectory) throws IOException {
    if (!unzipDestinationDirectory.exists()) {
        unzipDestinationDirectory.mkdirs();
    }//from w  ww  . j  a va2 s .com
    final int BUFFER = 2048;
    // Open Zip file for reading
    ZipFile zipFile = new ZipFile(uploadZip, ZipFile.OPEN_READ);

    // Create an enumeration of the entries in the zip file
    Enumeration zipFileEntries = zipFile.entries();

    // Process each entry
    while (zipFileEntries.hasMoreElements()) {
        // grab a zip file entry
        ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
        String currentEntry = entry.getName();
        File destFile = new File(unzipDestinationDirectory, currentEntry);
        // grab file's parent directory structure
        File destinationParent = destFile.getParentFile();

        // create the parent directory structure if needed
        destinationParent.mkdirs();

        // extract file if not a directory
        if (!entry.isDirectory()) {
            BufferedInputStream is = new BufferedInputStream(zipFile.getInputStream(entry));
            int currentByte;
            // establish buffer for writing file
            byte data[] = new byte[BUFFER];

            // write the current file to disk
            FileOutputStream fos = new FileOutputStream(destFile);
            BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);

            // read and write until last byte is encountered
            while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, currentByte);
            }
            dest.flush();
            dest.close();
            is.close();
        }
    }
    zipFile.close();
}

From source file:org.gradle.cache.internal.WrapperDistributionCleanupAction.java

private GradleVersion readGradleVersionFromBuildReceipt(ZipFile zipFile) throws Exception {
    ZipEntry zipEntry = zipFile.getEntry(BUILD_RECEIPT_ZIP_ENTRY_PATH);
    if (zipEntry == null) {
        return null;
    }//from  w ww  .jav  a2s.co m
    InputStream in = zipFile.getInputStream(zipEntry);
    try {
        Properties properties = new Properties();
        properties.load(in);
        String versionString = properties.getProperty(GradleVersion.VERSION_NUMBER_PROPERTY);
        return GradleVersion.version(versionString);
    } finally {
        in.close();
    }
}

From source file:de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.RemoteApiController.java

private void uploadSourceDocument(ZipFile zip, ZipEntry entry, Project project, User user, String aFileType)
        throws IOException, UIMAException {
    String fileName = FilenameUtils.getName(entry.toString());

    InputStream zipStream = zip.getInputStream(entry);
    SourceDocument document = new SourceDocument();
    document.setName(fileName);//from  www. j a  va 2 s .co  m
    document.setProject(project);
    document.setFormat(aFileType);
    // Meta data entry to the database
    projectRepository.createSourceDocument(document, user);
    // Import source document to the project repository folder
    projectRepository.uploadSourceDocument(zipStream, document);
}

From source file:it.readbeyond.minstrel.librarian.FormatHandlerAbstractZIP.java

protected String getZipEntryText(ZipFile zipFile, ZipEntry ze) {
    String toReturn = "";
    try {/* ww w .j a va  2s . c o  m*/
        BufferedInputStream is = new BufferedInputStream(zipFile.getInputStream(ze));
        byte[] bytes = new byte[(int) (ze.getSize())];
        is.read(bytes, 0, bytes.length);
        is.close();
        toReturn = new String(bytes);
    } catch (Exception e) {
        // nothing
    }
    return toReturn;
}

From source file:com.liferay.mobile.sdk.core.tests.MobileSDKCoreTests.java

private void checkJar(File jar, boolean src) throws Exception {
    assertTrue(jar.exists());/*from   w w w  .  ja va2s. co  m*/

    final ZipFile zipFile = new ZipFile(jar);
    final ZipEntry manifest = zipFile.getEntry("META-INF/MANIFEST.MF");

    assertNotNull(manifest);

    final String manifestContents = CoreUtil.readStreamToString(zipFile.getInputStream(manifest));

    assertTrue(manifestContents.startsWith("Manifest-Version: 1.0"));

    boolean valid = false;

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

    while (entries.hasMoreElements()) {
        final String entryName = entries.nextElement().getName();

        if (entryName.startsWith(PACKAGE.split("\\.")[0]) && entryName.endsWith(src ? ".java" : ".class")) {
            valid = true;
            break;
        }
    }

    zipFile.close();

    assertTrue(valid);
}

From source file:ZipExploder.java

/**
 * copy a single entry from the archive//  www  . j a  v a 2  s .c o m
 * 
 * @param destDir
 * @param zf
 * @param ze
 * @throws IOException
 */
public void copyFileEntry(String destDir, ZipFile zf, ZipEntry ze) throws IOException {
    DataInputStream dis = new DataInputStream(new BufferedInputStream(zf.getInputStream(ze)));
    try {
        copyFileEntry(destDir, ze.isDirectory(), ze.getName(), dis);
    } finally {
        try {
            dis.close();
        } catch (IOException ioe) {
        }
    }
}

From source file:com.thoughtworks.go.util.ZipUtilTest.java

private void assertContent(File targetZipFile, String file, String expectedContent) throws IOException {
    ZipFile actualZip = new ZipFile(targetZipFile);
    ZipEntry entry = actualZip.getEntry(file);
    assertThat(entry).isNotNull();/*w ww.  j  a  v  a  2  s. c o  m*/
    assertThat(IOUtils.toString(actualZip.getInputStream(entry), UTF_8)).isEqualTo(expectedContent);
}

From source file:com.moss.nomad.core.runner.Runner.java

private MigrationContainer extractContainer(File f) throws Exception {

    ZipFile file = new ZipFile(f);
    ZipEntry entry = new ZipEntry("META-INF/container.xml");
    InputStream in = file.getInputStream(entry);
    Unmarshaller u = context.createUnmarshaller();
    MigrationContainer container = (MigrationContainer) u.unmarshal(in);
    in.close();/*  w  w  w .jav  a  2  s. c  o m*/
    file.close();

    return container;
}

From source file:org.eclairjs.nashorn.Utils.java

public static void unzipFile(String zipfile, String directory) throws IOException {
    Logger logger = Logger.getLogger(Utils.class);
    int DEFAULT_BUFFER_SIZE = 4096;

    logger.debug("Going to try and unzip: " + zipfile);

    ZipFile zfile = new ZipFile(zipfile);
    Enumeration<? extends ZipEntry> entries = zfile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        logger.debug("Extracting: " + entry.getName());

        File file = new File(directory, entry.getName());
        if (entry.isDirectory()) {
            file.mkdirs();/* ww  w.  j  a  v  a  2s  .  co m*/
        } else {
            file.getParentFile().mkdirs();
            InputStream in = zfile.getInputStream(entry);
            OutputStream out = new FileOutputStream(file);
            try {
                byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
                while (true) {
                    int readCount = in.read(buffer);
                    if (readCount < 0) {
                        break;
                    }
                    out.write(buffer, 0, readCount);
                }
            } finally {
                in.close();
            }
        }
    }
}

From source file:averroes.JarOrganizer.java

/**
 * Extract a class file to specified file.
 * //from   ww w. ja va  2 s .  c  om
 * @param sourceArchive
 * @param entry
 * @param destArchive
 * @throws IOException
 */
private void extractClassFile(ZipFile sourceArchive, ZipEntry entry, JarFile destArchive) throws IOException {
    // Write out the class file to the destination archive directly. No
    // temporary file used.
    destArchive.add(sourceArchive.getInputStream(entry), entry.getName());
}