Example usage for java.util.jar JarInputStream close

List of usage examples for java.util.jar JarInputStream close

Introduction

In this page you can find the example usage for java.util.jar JarInputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Closes this input stream and releases any system resources associated with the stream.

Usage

From source file:net.ftb.util.FileUtils.java

/**
 * deletes the META-INF//w  w w  .j a v  a2  s .com
 */
public static void killMetaInf() {
    File inputFile = new File(Settings.getSettings().getInstallPath() + "/" + ModPack.getSelectedPack().getDir()
            + "/minecraft/bin", "minecraft.jar");
    File outputTmpFile = new File(Settings.getSettings().getInstallPath() + "/"
            + ModPack.getSelectedPack().getDir() + "/minecraft/bin", "minecraft.jar.tmp");
    try {
        JarInputStream input = new JarInputStream(new FileInputStream(inputFile));
        JarOutputStream output = new JarOutputStream(new FileOutputStream(outputTmpFile));
        JarEntry entry;

        while ((entry = input.getNextJarEntry()) != null) {
            if (entry.getName().contains("META-INF")) {
                continue;
            }
            output.putNextEntry(entry);
            byte buffer[] = new byte[1024];
            int amo;
            while ((amo = input.read(buffer, 0, 1024)) != -1) {
                output.write(buffer, 0, amo);
            }
            output.closeEntry();
        }

        input.close();
        output.close();

        if (!inputFile.delete()) {
            Logger.logError("Failed to delete Minecraft.jar.");
            return;
        }
        outputTmpFile.renameTo(inputFile);
    } catch (FileNotFoundException e) {
        Logger.logError("Error while killing META-INF", e);
    } catch (IOException e) {
        Logger.logError("Error while killing META-INF", e);
    }
}

From source file:org.apache.sling.testing.clients.osgi.OsgiConsoleClient.java

/**
 * Get the version form a bundle file//from  w w  w .j  a v a2  s.c om
 * @param bundleFile
 * @return
 * @throws IOException
 */
public static String getBundleVersionFromFile(File bundleFile) throws IOException {
    String version = null;
    final JarInputStream jis = new JarInputStream(new FileInputStream(bundleFile));
    try {
        final Manifest m = jis.getManifest();
        if (m == null) {
            throw new IOException("Manifest is null in " + bundleFile.getAbsolutePath());
        }
        version = m.getMainAttributes().getValue(Constants.BUNDLE_VERSION);
    } finally {
        jis.close();
    }
    return version;
}

From source file:org.apache.sling.testing.clients.osgi.OsgiConsoleClient.java

/**
 * Get the symbolic name from a bundle file
 * @param bundleFile/*  w  ww  .j  ava  2s . co m*/
 * @return
 * @throws IOException
 */
public static String getBundleSymbolicName(File bundleFile) throws IOException {
    String name = null;
    final JarInputStream jis = new JarInputStream(new FileInputStream(bundleFile));
    try {
        final Manifest m = jis.getManifest();
        if (m == null) {
            throw new IOException("Manifest is null in " + bundleFile.getAbsolutePath());
        }
        name = m.getMainAttributes().getValue(Constants.BUNDLE_SYMBOLICNAME);
    } finally {
        jis.close();
    }
    return name;
}

From source file:ArchiveUtil.java

/**
 * Extracts the file {@code archive} to the target dir {@code targetDir} and deletes the 
 * files extracted upon jvm exit if the flag {@code deleteOnExit} is true.
 *///from  ww  w .  ja  v a  2s  .c  om
public static boolean extract(URL archive, File targetDir, boolean deleteOnExit) throws IOException {
    String archiveStr = archive.toString();
    String jarEntry = null;
    int idx = archiveStr.indexOf("!/");
    if (idx != -1) {
        if (!archiveStr.startsWith("jar:") && archiveStr.length() == idx + 2)
            return false;
        archive = new URL(archiveStr.substring(4, idx));
        jarEntry = archiveStr.substring(idx + 2);
    } else if (!isSupported(archiveStr))
        return false;

    JarInputStream jis = new JarInputStream(archive.openConnection().getInputStream());
    if (!targetDir.exists())
        targetDir.mkdirs();
    JarEntry entry = null;
    while ((entry = jis.getNextJarEntry()) != null) {
        String entryName = entry.getName();
        File entryFile = new File(targetDir, entryName);
        if (!entry.isDirectory()) {
            if (jarEntry == null || entryName.startsWith(jarEntry)) {
                if (!entryFile.exists() || entryFile.lastModified() != entry.getTime())
                    extractEntry(entryFile, jis, entry, deleteOnExit);
            }
        }
    }
    try {
        jis.close();
    } catch (Exception e) {
    }
    return true;
}

From source file:ezbake.frack.submitter.util.JarUtil.java

public static File addFilesToJar(File sourceJar, List<File> newFiles) throws IOException {
    JarOutputStream target = null;
    JarInputStream source;
    File outputJar = new File(sourceJar.getParentFile(), getRepackagedJarName(sourceJar.getAbsolutePath()));
    log.debug("Output file created at {}", outputJar.getAbsolutePath());
    outputJar.createNewFile();/*w  ww  . j  a va  2s. c om*/
    try {
        source = new JarInputStream(new FileInputStream(sourceJar));
        target = new JarOutputStream(new FileOutputStream(outputJar));
        ZipEntry entry = source.getNextEntry();
        while (entry != null) {
            String name = entry.getName();
            // Add ZIP entry to output stream.
            target.putNextEntry(new ZipEntry(name));
            // Transfer bytes from the ZIP file to the output file
            int len;
            byte[] buffer = new byte[BUFFER_SIZE];
            while ((len = source.read(buffer)) > 0) {
                target.write(buffer, 0, len);
            }
            entry = source.getNextEntry();
        }
        source.close();
        for (File fileToAdd : newFiles) {
            add(fileToAdd, fileToAdd.getParentFile().getAbsolutePath(), target);
        }
    } finally {
        if (target != null) {
            log.debug("Closing output stream");
            target.close();
        }
    }
    return outputJar;
}

From source file:org.vafer.jdependency.utils.DependencyUtils.java

public static Set<String> getDependenciesOfJar(final InputStream pInputStream) throws IOException {
    final JarInputStream inputStream = new JarInputStream(pInputStream);
    final NullOutputStream nullStream = new NullOutputStream();
    final Set<String> dependencies = new HashSet<String>();
    try {//from  w ww .  jav a 2s.c o m
        while (true) {
            final JarEntry entry = inputStream.getNextJarEntry();

            if (entry == null) {
                break;
            }

            if (entry.isDirectory()) {
                // ignore directory entries
                IOUtils.copy(inputStream, nullStream);
                continue;
            }

            final String name = entry.getName();

            if (name.endsWith(".class")) {
                final DependenciesClassAdapter v = new DependenciesClassAdapter();
                new ClassReader(inputStream).accept(v, 0);
                dependencies.addAll(v.getDependencies());
            } else {
                IOUtils.copy(inputStream, nullStream);
            }
        }
    } finally {
        inputStream.close();
    }

    return dependencies;
}

From source file:org.pentaho.webpackage.deployer.WebPackageURLConnectionTest.java

@Test
public void testClosingStream() throws IOException {
    WebPackageURLConnection connection = new WebPackageURLConnection(
            new File("src/test/resources/my-simple-module-1.4.0.zip").toURI().toURL());
    connection.connect();//from   w  ww.jav  a  2s.com

    InputStream inputStream = connection.getInputStream();
    JarInputStream jar = new JarInputStream(inputStream);
    jar.getManifest();
    jar.close();

    try {
        connection.transform_thread.get();
    } catch (Exception exception) {
        fail("Thread failed to execute transform() method: " + exception.getMessage());
    }
}

From source file:org.diffkit.common.DKUnjar.java

/**
 * N.B. this method is much less efficient than the similar signature without
 * substitutions, so use that if you don't really need substitutions <br>
 * /* w w  w  .  j a va  2 s.c  o  m*/
 * closes inputStream_ at the end
 */
public static void unjar(JarInputStream inputStream_, File outputDir_, Map<String, String> substitutions_)
        throws IOException {
    boolean isDebugEnabled = LOG.isDebugEnabled();
    if (isDebugEnabled)
        LOG.debug("substitutions_->{}", substitutions_);

    if (MapUtils.isEmpty(substitutions_))
        unjar(inputStream_, outputDir_);

    DKValidate.notNull(inputStream_, outputDir_);
    if (!outputDir_.isDirectory())
        throw new RuntimeException(String.format("directory does not exist->%s", outputDir_));

    JarEntry entry = null;
    while ((entry = inputStream_.getNextJarEntry()) != null) {
        String contents = IOUtils.toString(inputStream_);
        if (isDebugEnabled)
            LOG.debug("contents->{}", contents);
        contents = DKStringUtil.replaceEach(contents, substitutions_);
        if (isDebugEnabled)
            LOG.debug("substituted contents->{}", contents);
        File outFile = new File(outputDir_, entry.getName());
        FileUtils.writeStringToFile(outFile, contents);
    }
    inputStream_.close();
}

From source file:org.pentaho.webpackage.deployer.archive.impl.WebPackageURLConnectionTest.java

@Test
public void testClosingStream() throws IOException {
    WebPackageURLConnection connection = new WebPackageURLConnection(
            getResourceUrl("/my-simple-module-1.4.0.zip"));
    connection.connect();/*from ww w . j  a  v a2 s  .  c o  m*/

    InputStream inputStream = connection.getInputStream();
    JarInputStream jar = new JarInputStream(inputStream);
    jar.getManifest();
    jar.close();

    try {
        connection.transform_thread.get();
    } catch (Exception exception) {
        fail("Thread failed to execute transform() method: " + exception.getMessage());
    }
}

From source file:org.apache.brooklyn.rt.felix.EmbeddedFelixFrameworkTest.java

@Test
public void testReadKnownManifest() throws Exception {
    TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), BROOKLYN_TEST_OSGI_ENTITIES_PATH);
    InputStream in = this.getClass().getResourceAsStream(BROOKLYN_TEST_OSGI_ENTITIES_PATH);
    JarInputStream jarIn = new JarInputStream(in);
    ManifestHelper helper = ManifestHelper.forManifest(jarIn.getManifest());
    jarIn.close();
    Assert.assertEquals(helper.getVersion().toString(), "0.1.0");
    Assert.assertTrue(helper.getExportedPackages().contains("org.apache.brooklyn.test.osgi.entities"));
}