Search in sources :

Example 31 with JarEntry

use of java.util.jar.JarEntry in project bazel by bazelbuild.

the class ResourceJarBuilderTest method messages.

@Test
public void messages() throws Exception {
    File output = temporaryFolder.newFile("resources.jar");
    Path root = temporaryFolder.newFolder().toPath();
    Path r1 = root.resolve("one/a.xmb");
    Files.createDirectories(r1.getParent());
    Files.write(r1, "hello".getBytes(UTF_8));
    Path r2 = root.resolve("two/b.xmb");
    Files.createDirectories(r2.getParent());
    Files.write(r2, "goodbye".getBytes(UTF_8));
    // empty messages are omitted
    Path r3 = root.resolve("three/c.xmb");
    Files.createDirectories(r3.getParent());
    Files.write(r3, new byte[0]);
    ResourceJarBuilder.build(ResourceJarOptions.builder().setOutput(output.toString()).setMessages(ImmutableList.of(root + ":" + root.relativize(r1), root + ":" + root.relativize(r2), root + ":" + root.relativize(r3))).build());
    List<String> entries = new ArrayList<>();
    try (JarFile jf = new JarFile(output)) {
        Enumeration<JarEntry> jes = jf.entries();
        while (jes.hasMoreElements()) {
            entries.add(jes.nextElement().getName());
        }
    }
    assertThat(entries).containsExactly(//
    "META-INF/", "META-INF/MANIFEST.MF", "one/", "one/a.xmb", "two/", "two/b.xmb").inOrder();
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) JarFile(java.util.jar.JarFile) File(java.io.File) Test(org.junit.Test)

Example 32 with JarEntry

use of java.util.jar.JarEntry in project bazel by bazelbuild.

the class ResourceJarBuilderTest method resourceJars.

@Test
public void resourceJars() throws Exception {
    File output = temporaryFolder.newFile("resources.jar");
    File jar1 = temporaryFolder.newFile("jar1.jar");
    try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(jar1))) {
        jos.putNextEntry(new JarEntry("one/a.properties"));
        jos.putNextEntry(new JarEntry("one/b.properties"));
    }
    File jar2 = temporaryFolder.newFile("jar2.jar");
    try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(jar2))) {
        jos.putNextEntry(new JarEntry("two/c.properties"));
        jos.putNextEntry(new JarEntry("two/d.properties"));
    }
    ResourceJarBuilder.build(ResourceJarOptions.builder().setOutput(output.toString()).setResourceJars(ImmutableList.of(jar1.toString(), jar2.toString())).build());
    List<String> entries = new ArrayList<>();
    try (JarFile jf = new JarFile(output)) {
        Enumeration<JarEntry> jes = jf.entries();
        while (jes.hasMoreElements()) {
            entries.add(jes.nextElement().getName());
        }
    }
    assertThat(entries).containsExactly("META-INF/", "META-INF/MANIFEST.MF", "one/", "one/a.properties", "one/b.properties", "two/", "two/c.properties", "two/d.properties").inOrder();
}
Also used : FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) JarOutputStream(java.util.jar.JarOutputStream) JarEntry(java.util.jar.JarEntry) JarFile(java.util.jar.JarFile) JarFile(java.util.jar.JarFile) File(java.io.File) Test(org.junit.Test)

Example 33 with JarEntry

use of java.util.jar.JarEntry in project bazel by bazelbuild.

the class JarHelper method writeEntry.

/**
   * Writes an entry with specific contents to the jar. Directory entries must include the trailing
   * '/'.
   */
protected void writeEntry(JarOutputStream out, String name, byte[] content) throws IOException {
    if (names.add(name)) {
        // Create a new entry
        JarEntry entry = new JarEntry(name);
        entry.setTime(newEntryTimeMillis(name));
        int size = content.length;
        entry.setSize(size);
        if (size == 0) {
            entry.setMethod(JarEntry.STORED);
            entry.setCrc(0);
            out.putNextEntry(entry);
        } else {
            entry.setMethod(storageMethod);
            if (storageMethod == JarEntry.STORED) {
                CRC32 crc = new CRC32();
                crc.update(content);
                entry.setCrc(crc.getValue());
            }
            out.putNextEntry(entry);
            out.write(content);
        }
        out.closeEntry();
    }
}
Also used : CRC32(java.util.zip.CRC32) JarEntry(java.util.jar.JarEntry)

Example 34 with JarEntry

use of java.util.jar.JarEntry in project auto by google.

the class TemplateVars method parsedTemplateFromJar.

private static Template parsedTemplateFromJar(URL resourceUrl) throws URISyntaxException, IOException {
    // Jar URLs look like this: jar:file:/path/to/file.jar!/entry/within/jar
    // So take apart the URL to open the jar /path/to/file.jar and read the entry
    // entry/within/jar from it.
    String resourceUrlString = resourceUrl.toString().substring("jar:".length());
    int bang = resourceUrlString.lastIndexOf('!');
    String entryName = resourceUrlString.substring(bang + 1);
    if (entryName.startsWith("/")) {
        entryName = entryName.substring(1);
    }
    URI jarUri = new URI(resourceUrlString.substring(0, bang));
    JarFile jar = new JarFile(new File(jarUri));
    try {
        JarEntry entry = jar.getJarEntry(entryName);
        InputStream in = jar.getInputStream(entry);
        return templateFromInputStream(in);
    } finally {
        jar.close();
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) URI(java.net.URI) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 35 with JarEntry

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

the class DefaultFileHashCacheTest method whenJarMemberWithoutManifestIsQueriedThenThrow.

@Test(expected = UnsupportedOperationException.class)
public void whenJarMemberWithoutManifestIsQueriedThenThrow() throws IOException {
    ProjectFilesystem filesystem = new FakeProjectFilesystem();
    DefaultFileHashCache cache = new DefaultFileHashCache(filesystem, Optional.empty());
    Path abiJarPath = Paths.get("no-manifest.jar");
    Path memberPath = Paths.get("Empty.class");
    try (JarOutputStream jar = new JarOutputStream(filesystem.newFileOutputStream(abiJarPath))) {
        jar.putNextEntry(new JarEntry(memberPath.toString()));
        jar.write("Contents".getBytes(StandardCharsets.UTF_8));
        jar.closeEntry();
    }
    cache.get(ArchiveMemberPath.of(abiJarPath, memberPath));
}
Also used : ArchiveMemberPath(com.facebook.buck.io.ArchiveMemberPath) Path(java.nio.file.Path) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) JarOutputStream(java.util.jar.JarOutputStream) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) JarEntry(java.util.jar.JarEntry) Test(org.junit.Test)

Aggregations

JarEntry (java.util.jar.JarEntry)594 JarFile (java.util.jar.JarFile)290 File (java.io.File)217 IOException (java.io.IOException)187 InputStream (java.io.InputStream)134 JarOutputStream (java.util.jar.JarOutputStream)112 FileOutputStream (java.io.FileOutputStream)109 FileInputStream (java.io.FileInputStream)92 URL (java.net.URL)87 JarInputStream (java.util.jar.JarInputStream)87 ArrayList (java.util.ArrayList)67 Manifest (java.util.jar.Manifest)58 JarURLConnection (java.net.JarURLConnection)53 Test (org.junit.Test)39 HashSet (java.util.HashSet)31 ZipEntry (java.util.zip.ZipEntry)31 ZipFile (java.util.zip.ZipFile)30 OutputStream (java.io.OutputStream)29 BufferedInputStream (java.io.BufferedInputStream)26 Enumeration (java.util.Enumeration)26