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.marklogic.contentpump.ZipDelimitedJSONReader.java

@Override
protected void initFileStream(InputSplit inSplit) throws IOException, InterruptedException {
    setFile(((FileSplit) inSplit).getPath());
    fileIn = fs.open(file);/*ww  w. j a  v a  2s . co  m*/
    zipIn = new ZipInputStream(fileIn);

}

From source file:com.skcraft.launcher.creator.util.ModInfoReader.java

/**
 * Detect the mods listed in the given .jar
 *
 * @param file The file//from w  w w . j  av  a  2s . c o  m
 * @return A list of detected mods
 */
public List<? extends ModInfo> detectMods(File file) {
    Closer closer = Closer.create();

    try {
        FileInputStream fis = closer.register(new FileInputStream(file));
        BufferedInputStream bis = closer.register(new BufferedInputStream(fis));
        ZipInputStream zis = closer.register(new ZipInputStream(bis));

        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            if (entry.getName().equalsIgnoreCase(FORGE_INFO_FILENAME)) {
                List<ForgeModInfo> mods;
                String content = CharStreams.toString(new InputStreamReader(zis, Charsets.UTF_8));

                try {
                    mods = mapper.readValue(content, ForgeModManifest.class).getMods();
                } catch (JsonMappingException | JsonParseException e) {
                    mods = mapper.readValue(content, new TypeReference<List<ForgeModInfo>>() {
                    });
                }

                if (mods != null) {
                    // Ignore "examplemod"
                    return Collections.unmodifiableList(
                            mods.stream().filter(info -> !info.getModId().equals("examplemod"))
                                    .collect(Collectors.toList()));
                } else {
                    return Collections.emptyList();
                }

            } else if (entry.getName().equalsIgnoreCase(LITELOADER_INFO_FILENAME)) {
                String content = CharStreams.toString(new InputStreamReader(zis, Charsets.UTF_8));
                return new ImmutableList.Builder<ModInfo>()
                        .add(mapper.readValue(content, LiteLoaderModInfo.class)).build();
            }
        }

        return Collections.emptyList();
    } catch (JsonMappingException e) {
        log.log(Level.WARNING, "Unknown format " + FORGE_INFO_FILENAME + " file in " + file.getAbsolutePath(),
                e);
        return Collections.emptyList();
    } catch (JsonParseException e) {
        log.log(Level.WARNING, "Corrupt " + FORGE_INFO_FILENAME + " file in " + file.getAbsolutePath(), e);
        return Collections.emptyList();
    } catch (IOException e) {
        log.log(Level.WARNING, "Failed to read " + file.getAbsolutePath(), e);
        return Collections.emptyList();
    } finally {
        try {
            closer.close();
        } catch (IOException ignored) {
        }
    }
}

From source file:com.googlecode.jsfFlex.shared.tasks.sdk.UnzipTask.java

protected void performTask() {

    BufferedOutputStream bufferOutputStream = null;
    ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(_file));
    ZipEntry entry;//  w w w.  j  ava 2 s .co m

    try {

        while ((entry = zipInputStream.getNextEntry()) != null) {

            ensureDirectoryExists(entry.getName(), entry.isDirectory());

            bufferOutputStream = new BufferedOutputStream(new FileOutputStream(_dest + entry.getName()),
                    BUFFER_SIZE);
            int currRead = 0;
            byte[] dataRead = new byte[BUFFER_SIZE];

            while ((currRead = zipInputStream.read(dataRead, 0, BUFFER_SIZE)) != -1) {
                bufferOutputStream.write(dataRead, 0, currRead);
            }
            bufferOutputStream.flush();
            bufferOutputStream.close();
        }

        _log.debug("UnzipTask performTask has been completed with " + toString());
    } catch (IOException ioExcept) {
        StringBuilder errorMessage = new StringBuilder();
        errorMessage.append("Error in Unzip's performTask with following fields \n");
        errorMessage.append(toString());
        throw new ComponentBuildException(errorMessage.toString(), ioExcept);
    } finally {
        try {
            zipInputStream.close();
            if (bufferOutputStream != null) {
                bufferOutputStream.close();
            }
        } catch (IOException innerIOExcept) {
            _log.info("Error while closing the streams within UnzipTask's finally block");
        }
    }

}

From source file:com.thoughtworks.go.server.initializers.CommandRepositoryInitializerIntegrationTest.java

@Test
public void shouldReplaceWithPackagedCommandRepositoryWhenOlderRepoExists() throws Exception {
    File versionFile = TestFileUtil.writeStringToTempFileInFolder("default", "version.txt", "10.1=10");
    File randomFile = TestFileUtil.createTestFile(versionFile.getParentFile(), "random");
    File defaultCommandRepoDir = versionFile.getParentFile();

    initializer.usePackagedCommandRepository(
            new ZipInputStream(new FileInputStream(getZippedCommandRepo("12.4=12"))), defaultCommandRepoDir);

    assertThat(defaultCommandRepoDir.exists(), is(true));
    assertThat(FileUtils.readFileToString(new File(defaultCommandRepoDir, "version.txt"), UTF_8),
            is("12.4=12"));
    assertThat(new File(defaultCommandRepoDir, "snippet.xml").exists(), is(true));
    assertThat(new File(defaultCommandRepoDir, randomFile.getName()).exists(), is(false));
}

From source file:com.wavemaker.tools.project.upgrade.UpgradeTemplateFile.java

@Override
public void doUpgrade(Project project, UpgradeInfo upgradeInfo) {

    if (this.relativePath == null) {
        throw new WMRuntimeException("No file provided");
    }//w  w  w  . j a  va2 s  . c o m

    try {
        File localFile = project.getRootFolder().getFile(this.relativePath);

        InputStream resourceStream = this.getClass().getClassLoader()
                .getResourceAsStream(ProjectManager._TEMPLATE_APP_RESOURCE_NAME);
        ZipInputStream resourceZipStream = new ZipInputStream(resourceStream);

        ZipEntry zipEntry = null;

        while ((zipEntry = resourceZipStream.getNextEntry()) != null) {
            if (this.relativePath.equals(zipEntry.getName())) {
                Writer writer = localFile.getContent().asWriter();
                IOUtils.copy(resourceZipStream, writer);
                writer.close();
            }
        }

        resourceZipStream.close();
        resourceStream.close();
    } catch (IOException e) {
        throw new WMRuntimeException(e);
    }

    if (this.message != null) {
        upgradeInfo.addMessage(this.message);
    }
}

From source file:com.nuvolect.deepdive.probe.ApkZipUtil.java

public static boolean unzip(OmniFile zipOmni, OmniFile targetDir, ProgressStream progressStream) {

    String volumeId = zipOmni.getVolumeId();
    String rootFolderPath = targetDir.getPath();
    boolean DEBUG = true;

    ZipInputStream zis = new ZipInputStream(zipOmni.getFileInputStream());
    ZipEntry entry = null;//from   w  w  w  .  j  a v a2 s.c om
    try {
        while ((entry = zis.getNextEntry()) != null) {

            if (entry.isDirectory()) {

                OmniFile dir = new OmniFile(volumeId, entry.getName());
                if (dir.mkdir()) {

                    if (DEBUG)
                        LogUtil.log(LogUtil.LogType.OMNI_ZIP, "dir created: " + dir.getPath());
                }
            } else {

                String path = rootFolderPath + "/" + entry.getName();
                OmniFile file = new OmniFile(volumeId, path);

                // Create any necessary directories
                file.getParentFile().mkdirs();

                OutputStream out = file.getOutputStream();

                OmniFiles.copyFileLeaveInOpen(zis, out);
                if (DEBUG)
                    LogUtil.log(LogUtil.LogType.OMNI_ZIP, "file created: " + file.getPath());

                progressStream.putStream("Unpacked: " + entry.getName());
            }
        }
        zis.close();

    } catch (IOException e) {
        LogUtil.logException(LogUtil.LogType.OMNI_ZIP, e);
        return false;
    }

    return true;
}

From source file:com.thoughtworks.go.domain.DirHandler.java

public void handle(InputStream stream) throws IOException {
    ZipInputStream zipInputStream = new ZipInputStream(stream);
    LOG.info(/*w  w w  . j  a  va2  s.  c  o  m*/
            "[Agent Fetch Artifact] Downloading from '{}' to '{}'. Will read from Socket stream to compute MD5 and write to file",
            srcFile, destOnAgent.getAbsolutePath());

    long before = System.currentTimeMillis();
    new ZipUtil((entry, stream1) -> {
        LOG.info("[Agent Fetch Artifact] Downloading a directory from '{}' to '{}'. Handling the entry: '{}'",
                srcFile, destOnAgent.getAbsolutePath(), entry.getName());
        new ChecksumValidator(artifactMd5Checksums).validate(getSrcFilePath(entry), md5Hex(stream1),
                checksumValidationPublisher);
    }).unzip(zipInputStream, destOnAgent);
    LOG.info("[Agent Fetch Artifact] Downloading a directory from '{}' to '{}'. Took: {}ms", srcFile,
            destOnAgent.getAbsolutePath(), System.currentTimeMillis() - before);
}

From source file:eu.openanalytics.rsb.AbstractITCase.java

public static void validateZipResult(final InputStream responseStream) throws IOException {
    final ZipInputStream result = new ZipInputStream(responseStream);
    ZipEntry ze = null;//from www . ja v  a  2s  .c  om

    while ((ze = result.getNextEntry()) != null) {
        if (ze.getName().endsWith(".pdf")) {
            return;
        }
    }

    fail("No PDF file found in Zip result");
}

From source file:org.cloudfoundry.tools.io.zip.ZipArchive.java

/**
 * Unzip the specified input stream into a folder.
 * /*ww  w  . j  ava  2s .com*/
 * @param inputStream the input stream to unzip (this must contain zip contents)
 * @param destination the destination folder
 * @see #unpack(File, Folder)
 */
public static void unpack(InputStream inputStream, Folder destination) {
    Assert.notNull(inputStream, "InputStream must not be null");
    Assert.notNull(destination, "Destination must not be null");
    destination.createIfMissing();
    ZipInputStream zip = new ZipInputStream(new BufferedInputStream(inputStream));
    try {
        InputStream noCloseZip = new NoCloseInputStream(zip);
        ZipEntry entry = zip.getNextEntry();
        while (entry != null) {
            if (entry.isDirectory()) {
                destination.getFolder(entry.getName()).createIfMissing();
            } else {
                destination.getFile(entry.getName()).getContent().write(noCloseZip);
            }
            entry = zip.getNextEntry();
        }
    } catch (IOException e) {
        throw new ResourceException(e);
    } finally {
        try {
            zip.close();
        } catch (IOException e) {
        }
    }
}

From source file:ZipFileIO.java

/**
 * Return the first directory of this archive. This is needed to determine
 * the plugin directory.//ww w.  j  a  v a  2s  . c  o  m
 * 
 * @param zipFile
 * @return <class>File</class> containing the first entry of this archive
 */
public static File getFirstFile(File zipFile) throws IOException {
    ZipInputStream in = null;
    try {
        // Open the ZIP file
        in = new ZipInputStream(new FileInputStream(zipFile));

        // Get the first entry
        ZipEntry entry = null;

        while ((entry = in.getNextEntry()) != null) {
            String outFilename = entry.getName();

            if (entry.isDirectory()) {
                return new File(outFilename);
            }
        }
    } finally {
        if (in != null) {
            // Close the stream
            in.close();
        }
    }
    return null;
}