Search in sources :

Example 51 with ZipEntry

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());
}
Also used : Path(java.nio.file.Path) ZipInputStream(java.util.zip.ZipInputStream) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) ZipEntry(java.util.zip.ZipEntry) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Test(org.junit.Test)

Example 52 with ZipEntry

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());
}
Also used : JavaFileObject(javax.tools.JavaFileObject) ZipEntry(java.util.zip.ZipEntry) Test(org.junit.Test)

Example 53 with ZipEntry

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();
}
Also used : Path(java.nio.file.Path) ImmutableSet(com.google.common.collect.ImmutableSet) ZipFile(java.util.zip.ZipFile) BuildTarget(com.facebook.buck.model.BuildTarget) ZipEntry(java.util.zip.ZipEntry) ProcessResult(com.facebook.buck.testutil.integration.ProjectWorkspace.ProcessResult) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 54 with ZipEntry

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);
        }
    }
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ZipFile(java.util.zip.ZipFile) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ClassReader(org.objectweb.asm.ClassReader)

Example 55 with ZipEntry

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);
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) Test(org.junit.Test)

Aggregations

ZipEntry (java.util.zip.ZipEntry)1367 ZipFile (java.util.zip.ZipFile)479 File (java.io.File)469 IOException (java.io.IOException)361 ZipOutputStream (java.util.zip.ZipOutputStream)321 ZipInputStream (java.util.zip.ZipInputStream)300 InputStream (java.io.InputStream)282 FileOutputStream (java.io.FileOutputStream)278 FileInputStream (java.io.FileInputStream)270 Test (org.junit.Test)124 BufferedInputStream (java.io.BufferedInputStream)122 JarFile (java.util.jar.JarFile)122 BufferedOutputStream (java.io.BufferedOutputStream)99 ByteArrayOutputStream (java.io.ByteArrayOutputStream)97 ArrayList (java.util.ArrayList)84 ByteArrayInputStream (java.io.ByteArrayInputStream)78 OutputStream (java.io.OutputStream)67 JarOutputStream (java.util.jar.JarOutputStream)59 Path (java.nio.file.Path)56 Enumeration (java.util.Enumeration)56