Search in sources :

Example 76 with ZipOutputStream

use of java.util.zip.ZipOutputStream in project bazel by bazelbuild.

the class JarFilter method filterJars.

/** Filters a list of jars, keeping anything matching the passed predicate. */
private static void filterJars(List<Path> jars, Path output, Predicate<String> shouldKeep) throws IOException {
    final int bufferSize = 8 * 1024;
    byte[] buffer = new byte[bufferSize];
    try (ZipOutputStream outputStream = new ZipOutputStream(new FileOutputStream(output.toFile()))) {
        for (Path jar : jars) {
            try (ZipFile sourceZipFile = new ZipFile(jar.toFile())) {
                Enumeration<? extends ZipEntry> entries = sourceZipFile.entries();
                while (entries.hasMoreElements()) {
                    ZipEntry entry = entries.nextElement();
                    if (!shouldKeep.apply(entry.getName())) {
                        continue;
                    }
                    ZipEntry newEntry = new ZipEntry(entry.getName());
                    outputStream.putNextEntry(newEntry);
                    try (InputStream inputStream = sourceZipFile.getInputStream(entry)) {
                        int len;
                        while ((len = inputStream.read(buffer)) != -1) {
                            outputStream.write(buffer, 0, len);
                        }
                    }
                }
            }
        }
    }
}
Also used : Path(java.nio.file.Path) ZipFile(java.util.zip.ZipFile) ZipOutputStream(java.util.zip.ZipOutputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry)

Example 77 with ZipOutputStream

use of java.util.zip.ZipOutputStream in project bazel by bazelbuild.

the class DexBuilder method processRequest.

private static void processRequest(ExecutorService executor, Cache<DexingKey, byte[]> dexCache, List<String> args) throws OptionsParsingException, IOException, InterruptedException, ExecutionException {
    OptionsParser optionsParser = OptionsParser.newOptionsParser(Options.class, DexingOptions.class);
    optionsParser.setAllowResidue(false);
    optionsParser.parse(args);
    Options options = optionsParser.getOptions(Options.class);
    try (ZipFile in = new ZipFile(options.inputJar.toFile());
        ZipOutputStream out = createZipOutputStream(options.outputZip)) {
        produceDexArchive(in, out, executor, /*convertOnReaderThread*/
        false, optionsParser.getOptions(DexingOptions.class), dexCache);
    }
    // Use input's timestamp for output file so the output file is stable.
    Files.setLastModifiedTime(options.outputZip, Files.getLastModifiedTime(options.inputJar));
}
Also used : DexingOptions(com.google.devtools.build.android.dexer.Dexing.DexingOptions) ZipFile(java.util.zip.ZipFile) DexingOptions(com.google.devtools.build.android.dexer.Dexing.DexingOptions) ZipOutputStream(java.util.zip.ZipOutputStream) OptionsParser(com.google.devtools.common.options.OptionsParser)

Example 78 with ZipOutputStream

use of java.util.zip.ZipOutputStream in project bazel by bazelbuild.

the class IdlClassTest method idlClass.

@Test
public void idlClass() throws IOException {
    File classJar = tempFolder.newFile("lib.jar");
    File manifestProto = tempFolder.newFile("lib.manifest");
    File tempDir = tempFolder.newFolder("temp_files");
    File outputClassJar = tempFolder.newFile("lib-idl.jar");
    File outputSourceJar = tempFolder.newFile("lib-idl-src.jar");
    List<String> classes = Arrays.asList("Baz.class", "Baz$0.class", "Baz$1.class", "c/g/Foo.class", "c/g/Foo$0.class", "c/g/Foo$Inner.class", "c/g/Foo$Inner$InnerMost.class", "c/g/Bar.class", "c/g/Bar2.class", "c/g/Bar$Inner.class", "c/g/Bar2$Inner.class");
    try (OutputStream os = new FileOutputStream(classJar);
        ZipOutputStream zos = new ZipOutputStream(os)) {
        for (String path : classes) {
            zos.putNextEntry(new ZipEntry(path));
        }
    }
    tempFolder.newFolder("c");
    tempFolder.newFolder("c/g");
    tempFolder.newFolder("wrong");
    tempFolder.newFolder("wrong/source");
    tempFolder.newFolder("wrong/source/dir");
    for (String file : Arrays.asList("c/g/Foo.java", "c/g/Bar.java", "wrong/source/dir/Baz.java")) {
        tempFolder.newFile(file);
    }
    try (OutputStream os = new FileOutputStream(manifestProto)) {
        MANIFEST.writeTo(os);
    }
    IdlClass.main(new String[] { "--manifest_proto", manifestProto.toString(), "--class_jar", classJar.toString(), "--output_class_jar", outputClassJar.toString(), "--output_source_jar", outputSourceJar.toString(), "--temp_dir", tempDir.toString(), "--idl_source_base_dir", tempFolder.getRoot().getPath(), "c/g/Bar.java", "wrong/source/dir/Baz.java" });
    List<String> classJarEntries = getJarEntries(outputClassJar);
    assertThat(classJarEntries).containsExactly("c/g/Bar.class", "c/g/Bar$Inner.class", "c/g/Bar2.class", "c/g/Bar2$Inner.class", "Baz.class", "Baz$0.class", "Baz$1.class");
    List<String> sourceJarEntries = getJarEntries(outputSourceJar);
    assertThat(sourceJarEntries).containsExactly("c/g/Bar.java", "Baz.java");
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) OutputStream(java.io.OutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) File(java.io.File) ZipFile(java.util.zip.ZipFile) Test(org.junit.Test)

Example 79 with ZipOutputStream

use of java.util.zip.ZipOutputStream in project bazel by bazelbuild.

the class JarFilterTest method fullIntegrationTest.

@Test
public void fullIntegrationTest() throws Exception {
    File fooJava = folder.newFile("Foo.java");
    Files.write("package com.google.foo; class Foo { class Inner {} }".getBytes(UTF_8), fooJava);
    File barJava = folder.newFile("Bar.java");
    Files.write("package com.google.foo.bar; class Bar {}".getBytes(UTF_8), barJava);
    File srcJar = folder.newFile("gen.srcjar");
    try (ZipOutputStream zo = new ZipOutputStream(new FileOutputStream(srcJar))) {
        zo.putNextEntry(new ZipEntry("com/google/foo/gen/Gen.java"));
        zo.write("package gen; class Gen {}".getBytes(UTF_8));
        zo.closeEntry();
        zo.putNextEntry(new ZipEntry("com/google/foo/gen/Gen2.java"));
        zo.write("package gen; class Gen2 {}".getBytes(UTF_8));
        zo.closeEntry();
    }
    File src3Jar = folder.newFile("gen3.srcjar");
    try (ZipOutputStream zo = new ZipOutputStream(new FileOutputStream(src3Jar))) {
        zo.putNextEntry(new ZipEntry("com/google/foo/gen/Gen3.java"));
        zo.write("package gen; class Gen3 {}".getBytes(UTF_8));
        zo.closeEntry();
    }
    File filterJar = folder.newFile("foo.jar");
    try (ZipOutputStream zo = new ZipOutputStream(new FileOutputStream(filterJar))) {
        zo.putNextEntry(new ZipEntry("com/google/foo/Foo.class"));
        zo.closeEntry();
        zo.putNextEntry(new ZipEntry("com/google/foo/Foo$Inner.class"));
        zo.closeEntry();
        zo.putNextEntry(new ZipEntry("com/google/foo/bar/Bar.class"));
        zo.closeEntry();
        zo.putNextEntry(new ZipEntry("gen/Gen.class"));
        zo.closeEntry();
        zo.putNextEntry(new ZipEntry("gen/Gen2.class"));
        zo.closeEntry();
        zo.putNextEntry(new ZipEntry("gen/Gen3.class"));
        zo.closeEntry();
        zo.putNextEntry(new ZipEntry("com/google/foo/Foo2.class"));
        zo.closeEntry();
    }
    File filterSrcJar = folder.newFile("foo-src.jar");
    try (ZipOutputStream zo = new ZipOutputStream(new FileOutputStream(filterSrcJar))) {
        zo.putNextEntry(new ZipEntry("com/google/foo/Foo.java"));
        zo.closeEntry();
        zo.putNextEntry(new ZipEntry("com/google/foo/bar/Bar.java"));
        zo.closeEntry();
        zo.putNextEntry(new ZipEntry("gen/Gen.java"));
        zo.closeEntry();
        zo.putNextEntry(new ZipEntry("gen/Gen2.java"));
        zo.closeEntry();
        zo.putNextEntry(new ZipEntry("gen/Gen3.java"));
        zo.closeEntry();
        zo.putNextEntry(new ZipEntry("com/google/foo/Foo2.java"));
        zo.closeEntry();
        zo.putNextEntry(new ZipEntry("com/google/foo/bar/Bar2.java"));
        zo.closeEntry();
    }
    File filteredJar = folder.newFile("foo-filtered-gen.jar");
    File filteredSourceJar = folder.newFile("foo-filtered-gen-src.jar");
    String[] args = new String[] { "--keep_java_files", fooJava.getPath() + File.pathSeparator + barJava.getPath(), "--keep_source_jars", Joiner.on(File.pathSeparator).join(srcJar.getPath(), src3Jar.getPath()), "--filter_jars", filterJar.getPath(), "--filter_source_jars", filterSrcJar.getPath(), "--filtered_jar", filteredJar.getPath(), "--filtered_source_jar", filteredSourceJar.getPath() };
    JarFilterOptions options = JarFilter.parseArgs(args);
    JarFilter.main(options);
    List<String> filteredJarNames = Lists.newArrayList();
    try (ZipFile zipFile = new ZipFile(filteredJar)) {
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry zipEntry = entries.nextElement();
            filteredJarNames.add(zipEntry.getName());
        }
    }
    List<String> filteredSourceJarNames = Lists.newArrayList();
    try (ZipFile zipFile = new ZipFile(filteredSourceJar)) {
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry zipEntry = entries.nextElement();
            filteredSourceJarNames.add(zipEntry.getName());
        }
    }
    assertThat(filteredJarNames).containsExactly("com/google/foo/Foo.class", "com/google/foo/Foo$Inner.class", "com/google/foo/bar/Bar.class", "gen/Gen.class", "gen/Gen2.class", "gen/Gen3.class");
    assertThat(filteredSourceJarNames).containsExactly("com/google/foo/Foo.java", "com/google/foo/bar/Bar.java", "gen/Gen.java", "gen/Gen2.java", "gen/Gen3.java");
}
Also used : JarFilterOptions(com.google.devtools.build.android.ideinfo.JarFilter.JarFilterOptions) ZipFile(java.util.zip.ZipFile) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) File(java.io.File) ZipFile(java.util.zip.ZipFile) Test(org.junit.Test)

Example 80 with ZipOutputStream

use of java.util.zip.ZipOutputStream in project bazel by bazelbuild.

the class JarFilterTest method legacyIntegrationTest.

@Test
public void legacyIntegrationTest() throws Exception {
    PackageManifest packageManifest = PackageManifest.newBuilder().addSources(JavaSourcePackage.newBuilder().setArtifactLocation(ArtifactLocation.newBuilder().setIsSource(true).setRelativePath("com/google/foo/Foo.java")).setPackageString("com.google.foo")).addSources(JavaSourcePackage.newBuilder().setArtifactLocation(ArtifactLocation.newBuilder().setIsSource(true).setRelativePath("com/google/bar/Bar.java")).setPackageString("com.google.bar")).addSources(JavaSourcePackage.newBuilder().setArtifactLocation(ArtifactLocation.newBuilder().setIsSource(true).setRelativePath("some/path/Test.java")).setPackageString("com.google.test")).build();
    assertThat(JarFilter.parsePackageManifest(packageManifest)).containsExactly("com/google/foo/Foo", "com/google/bar/Bar", "com/google/test/Test");
    File manifest = folder.newFile("foo.manifest");
    try (FileOutputStream outputStream = new FileOutputStream(manifest)) {
        packageManifest.writeTo(outputStream);
    }
    File filterJar = folder.newFile("foo.jar");
    try (ZipOutputStream zo = new ZipOutputStream(new FileOutputStream(filterJar))) {
        zo.putNextEntry(new ZipEntry("com/google/foo/Foo.class"));
        zo.closeEntry();
        zo.putNextEntry(new ZipEntry("com/google/foo/Foo$Inner.class"));
        zo.closeEntry();
        zo.putNextEntry(new ZipEntry("com/google/bar/Bar.class"));
        zo.closeEntry();
        zo.putNextEntry(new ZipEntry("com/google/test/Test.class"));
        zo.closeEntry();
        zo.putNextEntry(new ZipEntry("com/google/foo/Foo2.class"));
        zo.closeEntry();
    }
    File outputJar = folder.newFile("foo-filtered-gen.jar");
    String[] args = new String[] { "--jars", filterJar.getPath(), "--output", outputJar.getPath(), "--manifest", manifest.getPath() };
    JarFilter.JarFilterOptions options = JarFilter.parseArgs(args);
    JarFilter.main(options);
    List<String> filteredJarNames = Lists.newArrayList();
    try (ZipFile zipFile = new ZipFile(outputJar)) {
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry zipEntry = entries.nextElement();
            filteredJarNames.add(zipEntry.getName());
        }
    }
    assertThat(filteredJarNames).containsExactly("com/google/foo/Foo.class", "com/google/foo/Foo$Inner.class", "com/google/bar/Bar.class", "com/google/test/Test.class");
}
Also used : JarFilterOptions(com.google.devtools.build.android.ideinfo.JarFilter.JarFilterOptions) ZipFile(java.util.zip.ZipFile) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) PackageManifest(com.google.devtools.build.lib.ideinfo.androidstudio.PackageManifestOuterClass.PackageManifest) File(java.io.File) ZipFile(java.util.zip.ZipFile) Test(org.junit.Test)

Aggregations

ZipOutputStream (java.util.zip.ZipOutputStream)1168 ZipEntry (java.util.zip.ZipEntry)745 FileOutputStream (java.io.FileOutputStream)561 File (java.io.File)486 IOException (java.io.IOException)393 FileInputStream (java.io.FileInputStream)193 ByteArrayOutputStream (java.io.ByteArrayOutputStream)186 BufferedOutputStream (java.io.BufferedOutputStream)177 ZipFile (java.util.zip.ZipFile)163 InputStream (java.io.InputStream)144 Test (org.junit.Test)128 OutputStream (java.io.OutputStream)109 ByteArrayInputStream (java.io.ByteArrayInputStream)94 ZipInputStream (java.util.zip.ZipInputStream)88 Path (java.nio.file.Path)82 BufferedInputStream (java.io.BufferedInputStream)61 ArrayList (java.util.ArrayList)55 FileNotFoundException (java.io.FileNotFoundException)51 Date (java.util.Date)44 HashMap (java.util.HashMap)44