Search in sources :

Example 41 with JarOutputStream

use of java.util.jar.JarOutputStream in project buck by facebook.

the class AccumulateClassNamesStepTest method testExecuteAccumulateClassNamesStepOnJarFile.

@Test
public void testExecuteAccumulateClassNamesStepOnJarFile() throws IOException {
    // Create a JAR file.
    String name = "example.jar";
    File jarFile = tmp.newFile(name);
    try (JarOutputStream out = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(jarFile)))) {
        out.putNextEntry(new ZipEntry("com/example/Foo.class"));
        out.closeEntry();
        out.putNextEntry(new ZipEntry("com/example/Bar.class"));
        out.closeEntry();
        out.putNextEntry(new ZipEntry("com/example/not_a_class.png"));
        out.closeEntry();
        out.putNextEntry(new ZipEntry("com/example/subpackage/Baz.class"));
        out.closeEntry();
    }
    // Create the AccumulateClassNamesStep and execute it.
    ProjectFilesystem filesystem = new ProjectFilesystem(tmp.getRoot().toPath());
    AccumulateClassNamesStep accumulateClassNamesStep = new AccumulateClassNamesStep(filesystem, Optional.of(Paths.get(name)), Paths.get("output.txt"));
    ExecutionContext context = TestExecutionContext.newInstance();
    accumulateClassNamesStep.execute(context);
    String contents = Files.toString(new File(tmp.getRoot(), "output.txt"), Charsets.UTF_8);
    String separator = AccumulateClassNamesStep.CLASS_NAME_HASH_CODE_SEPARATOR;
    assertEquals("Verify that the contents are sorted alphabetically and ignore non-.class files.", Joiner.on('\n').join("com/example/Bar" + separator + SHA1_FOR_EMPTY_STRING, "com/example/Foo" + separator + SHA1_FOR_EMPTY_STRING, "com/example/subpackage/Baz" + separator + SHA1_FOR_EMPTY_STRING) + '\n', contents);
}
Also used : ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) JarOutputStream(java.util.jar.JarOutputStream) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) Test(org.junit.Test)

Example 42 with JarOutputStream

use of java.util.jar.JarOutputStream in project buck by facebook.

the class ClassesImpl method createJar.

@Override
public void createJar(Path jarPath) throws IOException {
    try (JarOutputStream jar = new JarOutputStream(Files.newOutputStream(jarPath))) {
        List<Path> files = Files.walk(root.getRoot().toPath()).filter(path -> path.toFile().isFile()).collect(Collectors.toList());
        for (Path file : files) {
            ZipEntry entry = new ZipEntry(MorePaths.pathWithUnixSeparators(root.getRoot().toPath().relativize(file)));
            jar.putNextEntry(entry);
            ByteStreams.copy(Files.newInputStream(file), jar);
            jar.closeEntry();
        }
    }
}
Also used : Files(java.nio.file.Files) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) MorePaths(com.facebook.buck.io.MorePaths) List(java.util.List) ClassReader(org.objectweb.asm.ClassReader) ByteStreams(com.google.common.io.ByteStreams) ClassVisitor(org.objectweb.asm.ClassVisitor) Path(java.nio.file.Path) JarOutputStream(java.util.jar.JarOutputStream) ZipEntry(java.util.zip.ZipEntry) TemporaryFolder(org.junit.rules.TemporaryFolder) InputStream(java.io.InputStream) Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) JarOutputStream(java.util.jar.JarOutputStream)

Example 43 with JarOutputStream

use of java.util.jar.JarOutputStream in project pinpoint by naver.

the class AgentDirGenerator method createJarFile.

private void createJarFile(File parentDir, String filepath) throws IOException {
    final String jarPath = parentDir.getPath() + File.separator + filepath;
    logger.debug("create jar:{}", jarPath);
    JarOutputStream jos = null;
    try {
        Manifest manifest = new Manifest();
        FileOutputStream out = new FileOutputStream(jarPath);
        jos = new JarOutputStream(out, manifest);
    } finally {
        IOUtils.closeQuietly(jos);
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) JarOutputStream(java.util.jar.JarOutputStream) Manifest(java.util.jar.Manifest)

Example 44 with JarOutputStream

use of java.util.jar.JarOutputStream in project otertool by wuntee.

the class JarSigner15 method signJarFile.

// the actual JAR signing method -- this is the method which
// will be called by those wrapping the JARSigner class
public void signJarFile(JarFile jarFile, OutputStream outputStream) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, IOException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, CertificateException, InstantiationException, ClassNotFoundException {
    // calculate the necessary files for the signed jAR
    // get the manifest out of the jar and verify that
    // all the entries in the manifest are correct
    Manifest manifest = getManifestFile(jarFile);
    Map entries = createEntries(manifest, jarFile);
    // create the message digest and start updating the
    // the attributes in the manifest to contain the SHA1
    // digests
    MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
    updateManifestDigest(manifest, jarFile, messageDigest, entries);
    // construct the signature file object and the
    // signature block objects
    SignatureFile signatureFile = createSignatureFile(manifest, messageDigest);
    SignatureFile.Block block = signatureFile.generateBlock(privateKey, certChain, true, jarFile);
    // start writing out the signed JAR file
    // write out the manifest to the output jar stream
    String manifestFileName = "META-INF/MANIFEST.MF";
    JarOutputStream jos = new JarOutputStream(outputStream);
    JarEntry manifestFile = new JarEntry(manifestFileName);
    jos.putNextEntry(manifestFile);
    byte[] manifestBytes = serialiseManifest(manifest);
    jos.write(manifestBytes, 0, manifestBytes.length);
    jos.closeEntry();
    // write out the signature file -- the signatureFile
    // object will name itself appropriately
    String signatureFileName = signatureFile.getMetaName();
    JarEntry signatureFileEntry = new JarEntry(signatureFileName);
    jos.putNextEntry(signatureFileEntry);
    signatureFile.write(jos);
    jos.closeEntry();
    // write out the signature block file -- again, the block
    // will name itself appropriately
    String signatureBlockName = block.getMetaName();
    JarEntry signatureBlockEntry = new JarEntry(signatureBlockName);
    jos.putNextEntry(signatureBlockEntry);
    block.write(jos);
    jos.closeEntry();
    // commit the rest of the original entries in the
    // META-INF directory. if any of their names conflict
    // with one that we created for the signed JAR file, then
    // we simply ignore it
    Enumeration metaEntries = jarFile.entries();
    while (metaEntries.hasMoreElements()) {
        JarEntry metaEntry = (JarEntry) metaEntries.nextElement();
        if (metaEntry.getName().startsWith("META-INF") && !(manifestFileName.equalsIgnoreCase(metaEntry.getName()) || signatureFileName.equalsIgnoreCase(metaEntry.getName()) || signatureBlockName.equalsIgnoreCase(metaEntry.getName())))
            writeJarEntry(metaEntry, jarFile, jos);
    }
    // now write out the rest of the files to the stream
    Enumeration allEntries = jarFile.entries();
    while (allEntries.hasMoreElements()) {
        JarEntry entry = (JarEntry) allEntries.nextElement();
        if (!entry.getName().startsWith("META-INF"))
            writeJarEntry(entry, jarFile, jos);
    }
    // finish the stream that we have been writing to
    jos.flush();
    jos.finish();
    // close the JAR file that we have been using
    jarFile.close();
}
Also used : Enumeration(java.util.Enumeration) JarOutputStream(java.util.jar.JarOutputStream) Manifest(java.util.jar.Manifest) MessageDigest(java.security.MessageDigest) JarEntry(java.util.jar.JarEntry) Map(java.util.Map)

Example 45 with JarOutputStream

use of java.util.jar.JarOutputStream in project OpenAM by OpenRock.

the class CreateSoapSTSDeployment method execute.

@SuppressWarnings("unchecked")
@Override
public String execute(Locale locale, Map mapParams) throws WorkflowException {
    try {
        validatePresenceOfMandatoryParams(mapParams);
        final JarInputStream soapSTSServerWar = getJarInputStream();
        final Path outputJarPath = getOutputJarFilePath(getStringParam(mapParams, REALM_PARAM));
        final JarOutputStream modifiedSoapSTSServerWar = getJarOutputStream(outputJarPath, soapSTSServerWar.getManifest());
        processFileContents(soapSTSServerWar, modifiedSoapSTSServerWar, mapParams);
        return getCompletionMessage(locale, outputJarPath);
    } catch (WorkflowException e) {
        Debug.getInstance("workflow").error("Exception caught in CreateSoapSTSDeployment#execute: " + e.getMessage());
        throw e;
    }
}
Also used : Path(java.nio.file.Path) JarInputStream(java.util.jar.JarInputStream) JarOutputStream(java.util.jar.JarOutputStream)

Aggregations

JarOutputStream (java.util.jar.JarOutputStream)273 FileOutputStream (java.io.FileOutputStream)171 File (java.io.File)145 JarEntry (java.util.jar.JarEntry)109 Manifest (java.util.jar.Manifest)80 IOException (java.io.IOException)60 ZipEntry (java.util.zip.ZipEntry)60 JarFile (java.util.jar.JarFile)53 Test (org.junit.Test)43 FileInputStream (java.io.FileInputStream)42 InputStream (java.io.InputStream)41 ByteArrayOutputStream (java.io.ByteArrayOutputStream)38 ByteArrayInputStream (java.io.ByteArrayInputStream)30 JarInputStream (java.util.jar.JarInputStream)28 BufferedOutputStream (java.io.BufferedOutputStream)25 ArrayList (java.util.ArrayList)23 OutputStream (java.io.OutputStream)22 Path (java.nio.file.Path)21 Map (java.util.Map)19 HashMap (java.util.HashMap)16