use of java.util.zip.ZipEntry in project buck by facebook.
the class JarDirectoryStepTest method jarsShouldContainDirectoryEntries.
@Test
public void jarsShouldContainDirectoryEntries() throws IOException {
Path zipup = folder.newFolder("dir-zip");
Path subdir = zipup.resolve("dir/subdir");
Files.createDirectories(subdir);
Files.write(subdir.resolve("a.txt"), "cake".getBytes());
JarDirectoryStep step = new JarDirectoryStep(new ProjectFilesystem(zipup), Paths.get("output.jar"), ImmutableSortedSet.of(zipup), /* main class */
null, /* manifest file */
null);
ExecutionContext context = TestExecutionContext.newInstance();
int returnCode = step.execute(context).getExitCode();
assertEquals(0, returnCode);
Path zip = zipup.resolve("output.jar");
assertTrue(Files.exists(zip));
// Iterate over each of the entries, expecting to see the directory names as entries.
Set<String> expected = Sets.newHashSet("dir/", "dir/subdir/");
try (ZipInputStream is = new ZipInputStream(Files.newInputStream(zip))) {
for (ZipEntry entry = is.getNextEntry(); entry != null; entry = is.getNextEntry()) {
expected.remove(entry.getName());
}
}
assertTrue("Didn't see entries for: " + expected, expected.isEmpty());
}
use of java.util.zip.ZipEntry in project buck by facebook.
the class JavaInMemoryFileManagerTest method testMultipleFilesInSamePackage.
@Test
public void testMultipleFilesInSamePackage() throws Exception {
JavaFileObject fileObject1 = inMemoryFileManager.getJavaFileForOutput(StandardLocation.CLASS_OUTPUT, "jvm.java.JavaFileParser", JavaFileObject.Kind.CLASS, null);
JavaFileObject fileObject2 = inMemoryFileManager.getJavaFileForOutput(StandardLocation.CLASS_OUTPUT, "jvm.java.JavaInMemoryFileManager", JavaFileObject.Kind.CLASS, null);
fileObject1.openOutputStream().close();
fileObject2.openOutputStream().close();
List<ZipEntry> zipEntries = outputStream.getZipEntries();
assertEquals(4, zipEntries.size());
assertEquals("jvm/", zipEntries.get(0).getName());
assertEquals("jvm/java/", zipEntries.get(1).getName());
assertEquals("jvm/java/JavaFileParser.class", zipEntries.get(2).getName());
assertEquals("jvm/java/JavaInMemoryFileManager.class", zipEntries.get(3).getName());
}
use of java.util.zip.ZipEntry in project buck by facebook.
the class DefaultJavaLibraryIntegrationTest method testBuildJavaLibraryExportsDirectoryEntries.
@Test
public void testBuildJavaLibraryExportsDirectoryEntries() throws IOException {
workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "export_directory_entries", tmp);
workspace.setUp();
// Run `buck build`.
BuildTarget target = BuildTargetFactory.newInstance("//:empty_directory_entries");
ProcessResult buildResult = workspace.runBuckBuild(target.getFullyQualifiedName());
buildResult.assertSuccess();
Path outputFile = workspace.getPath(BuildTargets.getGenPath(filesystem, target, "lib__%s__output/" + target.getShortName() + ".jar"));
assertTrue(Files.exists(outputFile));
ImmutableSet.Builder<String> jarContents = ImmutableSet.builder();
try (ZipFile zipFile = new ZipFile(outputFile.toFile())) {
for (ZipEntry zipEntry : Collections.list(zipFile.entries())) {
jarContents.add(zipEntry.getName());
}
}
// TODO(mread): Change the output to the intended output.
assertEquals(jarContents.build(), ImmutableSet.of("META-INF/", "META-INF/MANIFEST.MF", "swag.txt", "yolo.txt"));
workspace.verify();
}
use of java.util.zip.ZipEntry in project buck by facebook.
the class AbiClass method extract.
public static AbiClass extract(Path pathToJar, String className) throws IOException {
try (ZipFile zip = new ZipFile(pathToJar.toString())) {
ZipEntry entry = zip.getEntry(className);
if (entry == null) {
return null;
}
try (InputStream entryStream = zip.getInputStream(entry)) {
ClassReader reader = new ClassReader(entryStream);
ClassNode classNode = new ClassNode();
reader.accept(classNode, 0);
return new AbiClass(classNode);
}
}
}
use of java.util.zip.ZipEntry in project buck by facebook.
the class ClasspathTraversalTest method testZip.
@Test
public void testZip() throws IOException {
String[] files = { "test/foo.txt", "bar.txt", "test/baz.txt" };
File file = tempDir.newFile("test.zip");
try (ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(file)))) {
for (String filename : files) {
ZipEntry entry = new ZipEntry(filename);
zipOut.putNextEntry(entry);
zipOut.write(filename.getBytes(Charsets.UTF_8));
}
}
verifyFileLike(3, file);
}
Aggregations