Search in sources :

Example 21 with ZipFile

use of java.util.zip.ZipFile in project buck by facebook.

the class ZipWriteTest method main.

public static void main(String[] args) throws IOException, InterruptedException {
    try (CustomZipOutputStream zipOut = ZipOutputStreams.newOutputStream(Paths.get("/dev/null"), ZipOutputStreams.HandleDuplicates.APPEND_TO_ZIP)) {
        try (ZipFile zipIn = new ZipFile(new File(args[0]))) {
            for (Enumeration<? extends ZipEntry> entries = zipIn.entries(); entries.hasMoreElements(); ) {
                ZipEntry entry = entries.nextElement();
                ZipEntry newEntry = new ZipEntry(entry);
                if (entry.getMethod() == ZipEntry.DEFLATED) {
                    newEntry.setCompressedSize(-1);
                }
                zipOut.putNextEntry(newEntry);
                InputStream inputStream = zipIn.getInputStream(entry);
                ByteStreams.copy(inputStream, zipOut);
                zipOut.closeEntry();
            }
        }
    }
    System.gc();
    Thread.sleep(TimeUnit.SECONDS.toMillis(1));
    System.gc();
    Thread.sleep(TimeUnit.SECONDS.toMillis(5));
}
Also used : ZipFile(java.util.zip.ZipFile) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 22 with ZipFile

use of java.util.zip.ZipFile in project buck by facebook.

the class GenAidlIntegrationTest method rootDirectoryDoesntChangeBuild.

@Test
public void rootDirectoryDoesntChangeBuild() throws IOException {
    AssumeAndroidPlatform.assumeSdkIsAvailable();
    ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "cached_build", tmp);
    workspace.setUp();
    Path outputOne = workspace.buildAndReturnOutput("//:AService");
    ProjectWorkspace workspaceTwo = TestDataHelper.createProjectWorkspaceForScenario(this, "cached_build", tmp2);
    workspaceTwo.setUp();
    Path outputTwo = workspaceTwo.buildAndReturnOutput("//:AService");
    assertEquals(workspace.getBuildLog().getRuleKey("//:AService"), workspaceTwo.getBuildLog().getRuleKey("//:AService"));
    try (ZipFile zipOne = new ZipFile(outputOne.toFile());
        ZipFile zipTwo = new ZipFile(outputTwo.toFile())) {
        Enumeration<? extends ZipEntry> entriesOne = zipOne.entries(), entriesTwo = zipTwo.entries();
        while (entriesOne.hasMoreElements()) {
            assertTrue(entriesTwo.hasMoreElements());
            ZipEntry entryOne = entriesOne.nextElement(), entryTwo = entriesTwo.nextElement();
            // Compare data first, otherwise crc difference will cause a failure and you don't get to
            // see the actual difference.
            assertEquals(zipEntryData(zipOne, entryOne), zipEntryData(zipTwo, entryTwo));
            assertEquals(zipEntryDebugString(entryOne), zipEntryDebugString(entryTwo));
        }
        assertFalse(entriesTwo.hasMoreElements());
    }
    assertEquals(new String(Files.readAllBytes(outputOne)), new String(Files.readAllBytes(outputTwo)));
}
Also used : Path(java.nio.file.Path) ProjectWorkspace(com.facebook.buck.testutil.integration.ProjectWorkspace) ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry) Test(org.junit.Test)

Example 23 with ZipFile

use of java.util.zip.ZipFile 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 24 with ZipFile

use of java.util.zip.ZipFile 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 25 with ZipFile

use of java.util.zip.ZipFile in project buck by facebook.

the class GenruleIntegrationTest method assertZipsAreEqual.

private void assertZipsAreEqual(Path zipPathOne, Path zipPathTwo) throws IOException {
    try (ZipFile zipOne = new ZipFile(zipPathOne.toFile());
        ZipFile zipTwo = new ZipFile(zipPathTwo.toFile())) {
        Enumeration<? extends ZipEntry> entriesOne = zipOne.entries(), entriesTwo = zipTwo.entries();
        while (entriesOne.hasMoreElements()) {
            assertTrue(entriesTwo.hasMoreElements());
            ZipEntry entryOne = entriesOne.nextElement(), entryTwo = entriesTwo.nextElement();
            assertEquals(zipEntryDebugString(entryOne), zipEntryDebugString(entryTwo));
            assertEquals(zipEntryData(zipOne, entryOne), zipEntryData(zipTwo, entryTwo));
        }
        assertFalse(entriesTwo.hasMoreElements());
    }
    assertEquals(new String(Files.readAllBytes(zipPathOne)), new String(Files.readAllBytes(zipPathTwo)));
}
Also used : ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry)

Aggregations

ZipFile (java.util.zip.ZipFile)580 ZipEntry (java.util.zip.ZipEntry)414 File (java.io.File)261 IOException (java.io.IOException)189 InputStream (java.io.InputStream)127 FileOutputStream (java.io.FileOutputStream)98 ZipOutputStream (java.util.zip.ZipOutputStream)89 Test (org.junit.Test)88 FileInputStream (java.io.FileInputStream)57 ArrayList (java.util.ArrayList)44 Enumeration (java.util.Enumeration)42 BufferedInputStream (java.io.BufferedInputStream)38 BufferedOutputStream (java.io.BufferedOutputStream)35 ZipException (java.util.zip.ZipException)32 ClassReader (org.objectweb.asm.ClassReader)29 OutputStream (java.io.OutputStream)27 JarFile (java.util.jar.JarFile)26 ZipInputStream (java.util.zip.ZipInputStream)26 FileNotFoundException (java.io.FileNotFoundException)23 Path (java.nio.file.Path)23