Example usage for java.util.jar JarInputStream getNextEntry

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

Introduction

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

Prototype

public ZipEntry getNextEntry() throws IOException 

Source Link

Document

Reads the next ZIP file entry and positions the stream at the beginning of the entry data.

Usage

From source file:com.asual.summer.onejar.OneJarMojo.java

public void execute() throws MojoExecutionException {

    JarOutputStream out = null;//  w  w  w.  j  a  va 2 s. c o  m
    JarInputStream in = null;

    try {

        File jarFile = new File(buildDirectory, jarName);
        File warFile = new File(buildDirectory, warName);

        Manifest manifest = new Manifest(new ByteArrayInputStream("Manifest-Version: 1.0\n".getBytes("UTF-8")));
        manifest.getMainAttributes().putValue("Main-Class", JAR_MAIN_CLASS);
        manifest.getMainAttributes().putValue("One-Jar-Main-Class", ONE_JAR_MAIN_CLASS);

        out = new JarOutputStream(new FileOutputStream(jarFile, false), manifest);
        in = new JarInputStream(getClass().getClassLoader().getResourceAsStream(ONE_JAR_DIST));

        putEntry(out, new FileInputStream(warFile), new ZipEntry(JAR_CLASSPATH + warFile.getName()));

        for (Artifact artifact : pluginArtifacts) {
            if (artifact.getArtifactId().equalsIgnoreCase("summer-onejar")) {
                artifact.updateVersion(artifact.getVersion(), localRepository);
                putEntry(out, new FileInputStream(artifact.getFile()),
                        new ZipEntry(JAR_CLASSPATH + artifact.getFile().getName()));
                MavenProject project = mavenProjectBuilder.buildFromRepository(artifact,
                        remoteArtifactRepositories, localRepository);
                List<Dependency> dependencies = project.getDependencies();
                for (Dependency dependency : dependencies) {
                    if (!"provided".equals(dependency.getScope())) {
                        Artifact dependencyArtifact = artifactFactory.createArtifact(dependency.getGroupId(),
                                dependency.getArtifactId(), dependency.getVersion(), dependency.getScope(),
                                dependency.getType());
                        dependencyArtifact.updateVersion(dependencyArtifact.getVersion(), localRepository);
                        putEntry(out, new FileInputStream(dependencyArtifact.getFile()),
                                new ZipEntry(JAR_CLASSPATH + dependencyArtifact.getFile().getName()));
                    }
                }
            }
        }

        while (true) {
            ZipEntry entry = in.getNextEntry();
            if (entry != null) {
                putEntry(out, in, entry);
            } else {
                break;
            }
        }

        projectHelper.attachArtifact(project, "jar", jarFile);

    } catch (Exception e) {
        getLog().error(e.getMessage(), e);
        throw new MojoExecutionException(e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(out);
        IOUtils.closeQuietly(in);
    }
}

From source file:net.sourceforge.jweb.maven.mojo.OneExecutablejarMojo.java

public void execute() throws MojoExecutionException {

    // Show some info about the plugin.
    displayPluginInfo();/*w w w. ja  va 2s .c  o m*/

    JarOutputStream out = null;
    JarInputStream template = null;

    File onejarFile;
    try {
        // Create the target file
        onejarFile = new File(outputDirectory, filename);

        // Open a stream to write to the target file
        out = new JarOutputStream(new FileOutputStream(onejarFile, false), getManifest());

        // Main jar
        if (getLog().isDebugEnabled()) {
            getLog().debug("Adding main jar main/[" + mainJarFilename + "]");
        }
        addToZip(new File(outputDirectory, mainJarFilename), "main/", out);

        // All dependencies, including transient dependencies, but excluding system scope dependencies
        List<File> dependencyJars = extractDependencyFiles(artifacts);
        if (getLog().isDebugEnabled()) {
            getLog().debug("Adding [" + dependencyJars.size() + "] dependency libraries...");
        }
        for (File jar : dependencyJars) {
            addToZip(jar, "lib/", out);
        }

        // System scope dependencies
        List<File> systemDependencyJars = extractSystemDependencyFiles(dependencies);
        if (getLog().isDebugEnabled()) {
            getLog().debug("Adding [" + systemDependencyJars.size() + "] system dependency libraries...");
        }
        for (File jar : systemDependencyJars) {
            addToZip(jar, "lib/", out);
        }

        // Native libraries
        if (binlibs != null) {
            for (FileSet eachFileSet : binlibs) {
                List<File> includedFiles = toFileList(eachFileSet);
                if (getLog().isDebugEnabled()) {
                    getLog().debug("Adding [" + includedFiles.size() + "] native libraries...");
                }
                for (File eachIncludedFile : includedFiles) {
                    addToZip(eachIncludedFile, "binlib/", out);
                }
            }
        }

        // One-jar stuff
        getLog().debug("Adding one-jar components...");
        template = openOnejarTemplateArchive();
        ZipEntry entry;
        while ((entry = template.getNextEntry()) != null) {
            // Skip the manifest file, no need to clutter...
            if (!"boot-manifest.mf".equals(entry.getName())) {
                addToZip(out, entry, template);
            }
        }

    } catch (IOException e) {
        getLog().error(e);
        throw new MojoExecutionException("One-jar Mojo failed.", e);
    } finally {
        IOUtils.closeQuietly(out);
        IOUtils.closeQuietly(template);
    }

    // Attach the created one-jar to the build.
    if (attachToBuild) {
        projectHelper.attachArtifact(project, "jar", classifier, onejarFile);
    }
}

From source file:org.apache.axis2.jaxws.description.builder.JAXWSRIWSDLGenerator.java

/**
 * Check if this inputstream is a jar/zip
 *
 * @param is//  w  w w.  ja v a  2  s.  com
 * @return true if inputstream is a jar
 */
public static boolean isJar(InputStream is) {
    try {
        JarInputStream jis = new JarInputStream(is);
        if (jis.getNextEntry() != null) {
            return true;
        }
    } catch (IOException ioe) {
    }
    return false;
}

From source file:org.apache.axis2.jaxws.framework.JAXWSDeployer.java

/**
 * Check if this inputstream is a jar/zip
 *
 * @param f - file/*from w w w .  j  a va  2 s. c  o  m*/
 * @return true if inputstream is a jar
 */
public static boolean isJar(File f) {
    try {
        JarInputStream jis = new JarInputStream(new FileInputStream(f));
        if (jis.getNextEntry() != null) {
            return true;
        }
    } catch (IOException ioe) {
    }
    return false;
}

From source file:org.chtijbug.drools.platform.runtime.utils.Xsd2JarTransformerTestCase.java

@Test
public void should_get_all_expected_entries_from_generated_jar_file() throws IOException {
    Xsd2JarTransformer toTest = new Xsd2JarTransformer();

    URL xsdFile = this.getClass().getResource("/model.xsd");

    InputStream modelJarStream = toTest.transformXsd2Jar("org.pymma.drools", new File(xsdFile.getFile()));

    File modelJarFile = File.createTempFile("model", ".jar");
    IOUtils.copy(modelJarStream, FileUtils.openOutputStream(modelJarFile));

    JarInputStream inputStream = new JarInputStream(FileUtils.openInputStream(modelJarFile));
    assertThat(inputStream.getManifest()).isNotNull();

    List<ZipEntry> allJarEntries = new ArrayList<ZipEntry>();

    ZipEntry entry;//  w ww.  j a  va2  s  .c  o m
    while ((entry = inputStream.getNextEntry()) != null)
        allJarEntries.add(entry);

    assertThat(allJarEntries).hasSize(5);
}

From source file:org.eclipse.packagedrone.repo.aspect.common.p2.internal.P2Unzipper.java

private void processZippedMetaData(final Context context, final ZipInputStream zis, final String filename,
        final String xpath) throws Exception {
    final JarInputStream jin = new JarInputStream(zis, false);
    ZipEntry entry;/*from   w  ww  . j a v  a 2 s . com*/
    while ((entry = jin.getNextEntry()) != null) {
        if (entry.getName().equals(filename)) {
            processMetaData(context, jin, filename, xpath);
        }
    }
}

From source file:org.ngrinder.common.util.CompressionUtil.java

/**
 * Unpack the given jar file.//  w w  w  .j a v  a 2s  .  com
 * 
 * @param jarFile
 *            file to be uncompressed
 * @param destDir
 *            destination directory
 * @throws IOException
 *             occurs when IO has a problem.
 */
public static void unjar(File jarFile, String destDir) throws IOException {
    File dest = new File(destDir);
    if (!dest.exists()) {
        dest.mkdirs();
    }
    if (!dest.isDirectory()) {
        LOGGER.error("Destination must be a directory.");
        throw new IOException("Destination must be a directory.");
    }
    JarInputStream jin = new JarInputStream(new FileInputStream(jarFile));
    byte[] buffer = new byte[1024];

    ZipEntry entry = jin.getNextEntry();
    while (entry != null) {
        String fileName = entry.getName();
        if (fileName.charAt(fileName.length() - 1) == '/') {
            fileName = fileName.substring(0, fileName.length() - 1);
        }
        if (fileName.charAt(0) == '/') {
            fileName = fileName.substring(1);
        }
        if (File.separatorChar != '/') {
            fileName = fileName.replace('/', File.separatorChar);
        }
        File file = new File(dest, fileName);
        if (entry.isDirectory()) {
            // make sure the directory exists
            file.mkdirs();
            jin.closeEntry();
        } else {
            // make sure the directory exists
            File parent = file.getParentFile();
            if (parent != null && !parent.exists()) {
                parent.mkdirs();
            }

            // dump the file
            OutputStream out = new FileOutputStream(file);
            int len = 0;
            while ((len = jin.read(buffer, 0, buffer.length)) != -1) {
                out.write(buffer, 0, len);
            }
            out.flush();
            IOUtils.closeQuietly(out);
            jin.closeEntry();
            file.setLastModified(entry.getTime());
        }
        entry = jin.getNextEntry();
    }
    Manifest mf = jin.getManifest();
    if (mf != null) {
        File file = new File(dest, "META-INF/MANIFEST.MF");
        File parent = file.getParentFile();
        if (!parent.exists()) {
            parent.mkdirs();
        }
        OutputStream out = new FileOutputStream(file);
        mf.write(out);
        out.flush();
        IOUtils.closeQuietly(out);
    }
    IOUtils.closeQuietly(jin);
}

From source file:ru.codeinside.adm.ui.UploadDeployer.java

private boolean hasApiServer(String supportedInterface, JarInputStream jarStream)
        throws IOException, SAXException, ParserConfigurationException {
    for (String xmlPath : jarStream.getManifest().getMainAttributes().getValue("Service-Component")
            .split(",")) {
        ZipEntry nextZipEntry = jarStream.getNextEntry();
        while (nextZipEntry != null && !xmlPath.equals(nextZipEntry.getName())) {
            nextZipEntry = jarStream.getNextEntry();
        }//from w  ww  . j a  va  2 s.  c  o m
        Document xmlDoc = readXml(jarStream);

        Node node = getChildForNlByNames(xmlDoc.getChildNodes(), "component", "service", "provide");
        if (node != null) {
            String interfaceValue = node.getAttributes().getNamedItem("interface").getNodeValue();

            if (StringUtils.isNotEmpty(interfaceValue) && supportedInterface.equals(interfaceValue.trim())) {
                return true;
            }
        }
    }
    return false;
}

From source file:ru.runa.af.web.system.TaskHandlerClassesInformation.java

private static void searchInJar(String jarName, JarInputStream jarInputStream) throws IOException {
    boolean matches = false;
    for (String patternFileName : BotStationResources.getTaskHandlerJarNames()) {
        if (FilenameUtils.wildcardMatch(jarName, patternFileName)) {
            matches = true;/*from  www . j av a2 s .com*/
            break;
        }
    }
    if (!matches) {
        log.debug("Ignored " + jarName);
        return;
    }
    log.info("Searching in " + jarName);
    ZipEntry entry;
    while ((entry = jarInputStream.getNextEntry()) != null) {
        if (entry.getName().endsWith(".class")) {
            try {
                String className = entry.getName();
                int lastIndexOfDotSymbol = className.lastIndexOf('.');
                className = className.substring(0, lastIndexOfDotSymbol).replace('/', '.');
                // If we can't load class - just move to next class.
                Class<?> someClass = ClassLoaderUtil.loadClass(className);
                if (TaskHandler.class.isAssignableFrom(someClass)
                        && !Modifier.isAbstract(someClass.getModifiers())) {
                    taskHandlerImplementationClasses.add(someClass.getCanonicalName());
                }
            } catch (Throwable e) {
                log.warn("Error on loading task handler for " + e.getMessage());
            }
        }
    }
}