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();
}
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();
}
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();
}
}
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();
}
}
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));
}
Aggregations