Search in sources :

Example 1 with ZipReader

use of com.google.devtools.build.zip.ZipReader in project bazel by bazelbuild.

the class ZipCombiner method addZip.

/**
   * Adds the contents of a ZIP file to the combined ZIP file using the specified
   * {@link ZipEntryFilter} to determine the appropriate action for each file. 
   *
   * @param zipFile the ZIP file to add to the combined ZIP file
   * @throws IOException if there is an error reading the ZIP file or writing entries to the
   *     combined ZIP file
   */
public void addZip(File zipFile) throws IOException {
    try (ZipReader zip = new ZipReader(zipFile)) {
        for (ZipFileEntry entry : zip.entries()) {
            String filename = entry.getName();
            EntryAction action = getAction(filename);
            switch(action.getType()) {
                case SKIP:
                    break;
                case COPY:
                case RENAME:
                    writeEntry(zip, entry, action);
                    break;
                case MERGE:
                    entries.put(filename, null);
                    InputStream in = zip.getRawInputStream(entry);
                    if (entry.getMethod() == Compression.DEFLATED) {
                        in = new InflaterInputStream(in, getInflater());
                    }
                    action.getStrategy().merge(in, action.getMergeBuffer());
                    break;
            }
        }
    }
}
Also used : InflaterInputStream(java.util.zip.InflaterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) DeflaterInputStream(java.util.zip.DeflaterInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) ZipReader(com.google.devtools.build.zip.ZipReader) ZipFileEntry(com.google.devtools.build.zip.ZipFileEntry)

Example 2 with ZipReader

use of com.google.devtools.build.zip.ZipReader in project bazel by bazelbuild.

the class ZipCombinerTest method testZipCombinerAgainstJavaUtil.

@Test
public void testZipCombinerAgainstJavaUtil() throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try (JarOutputStream jarOut = new JarOutputStream(out)) {
        ZipEntry entry;
        entry = new ZipEntry("META-INF/");
        entry.setTime(ZipCombiner.DOS_EPOCH.getTime());
        entry.setMethod(JarOutputStream.STORED);
        entry.setSize(0);
        entry.setCompressedSize(0);
        entry.setCrc(0);
        jarOut.putNextEntry(entry);
        entry = new ZipEntry("META-INF/MANIFEST.MF");
        entry.setTime(ZipCombiner.DOS_EPOCH.getTime());
        entry.setMethod(JarOutputStream.DEFLATED);
        jarOut.putNextEntry(entry);
        jarOut.write(new byte[] { 1, 2, 3, 4 });
    }
    File javaFile = writeInputStreamToFile(new ByteArrayInputStream(out.toByteArray()));
    out.reset();
    try (ZipCombiner zipcombiner = new ZipCombiner(out)) {
        zipcombiner.addDirectory("META-INF/", ZipCombiner.DOS_EPOCH, new ExtraData[] { new ExtraData((short) 0xCAFE, new byte[0]) });
        zipcombiner.addFile("META-INF/MANIFEST.MF", ZipCombiner.DOS_EPOCH, new ByteArrayInputStream(new byte[] { 1, 2, 3, 4 }));
    }
    File zipCombinerFile = writeInputStreamToFile(new ByteArrayInputStream(out.toByteArray()));
    byte[] zipCombinerRaw = out.toByteArray();
    new ZipTester(zipCombinerRaw).validate();
    assertZipFilesEquivalent(new ZipReader(zipCombinerFile), new ZipReader(javaFile));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ZipEntry(java.util.zip.ZipEntry) JarOutputStream(java.util.jar.JarOutputStream) ZipReader(com.google.devtools.build.zip.ZipReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ExtraData(com.google.devtools.build.zip.ExtraData) File(java.io.File) Test(org.junit.Test)

Example 3 with ZipReader

use of com.google.devtools.build.zip.ZipReader in project bazel by bazelbuild.

the class ZipDecompressor method decompress.

/**
   * This unzips the zip file to a sibling directory of {@link DecompressorDescriptor#archivePath}.
   * The zip file is expected to have the WORKSPACE file at the top level, e.g.:
   *
   * <pre>
   * $ unzip -lf some-repo.zip
   * Archive:  ../repo.zip
   *  Length      Date    Time    Name
   * ---------  ---------- -----   ----
   *        0  2014-11-20 15:50   WORKSPACE
   *        0  2014-11-20 16:10   foo/
   *      236  2014-11-20 15:52   foo/BUILD
   *      ...
   * </pre>
   */
@Override
@Nullable
public Path decompress(DecompressorDescriptor descriptor) throws RepositoryFunctionException {
    Path destinationDirectory = descriptor.archivePath().getParentDirectory();
    Optional<String> prefix = descriptor.prefix();
    boolean foundPrefix = false;
    try (ZipReader reader = new ZipReader(descriptor.archivePath().getPathFile())) {
        Collection<ZipFileEntry> entries = reader.entries();
        for (ZipFileEntry entry : entries) {
            StripPrefixedPath entryPath = StripPrefixedPath.maybeDeprefix(entry.getName(), prefix);
            foundPrefix = foundPrefix || entryPath.foundPrefix();
            if (entryPath.skip()) {
                continue;
            }
            extractZipEntry(reader, entry, destinationDirectory, entryPath.getPathFragment());
        }
    } catch (IOException e) {
        throw new RepositoryFunctionException(new IOException(String.format("Error extracting %s to %s: %s", descriptor.archivePath(), destinationDirectory, e.getMessage())), Transience.TRANSIENT);
    }
    if (prefix.isPresent() && !foundPrefix) {
        throw new RepositoryFunctionException(new IOException("Prefix " + prefix.get() + " was given, but not found in the zip"), Transience.PERSISTENT);
    }
    return destinationDirectory;
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) ZipReader(com.google.devtools.build.zip.ZipReader) ZipFileEntry(com.google.devtools.build.zip.ZipFileEntry) IOException(java.io.IOException) RepositoryFunctionException(com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException) Nullable(javax.annotation.Nullable)

Aggregations

ZipReader (com.google.devtools.build.zip.ZipReader)3 ZipFileEntry (com.google.devtools.build.zip.ZipFileEntry)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 RepositoryFunctionException (com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException)1 Path (com.google.devtools.build.lib.vfs.Path)1 ExtraData (com.google.devtools.build.zip.ExtraData)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 JarOutputStream (java.util.jar.JarOutputStream)1 DeflaterInputStream (java.util.zip.DeflaterInputStream)1 InflaterInputStream (java.util.zip.InflaterInputStream)1 ZipEntry (java.util.zip.ZipEntry)1 Nullable (javax.annotation.Nullable)1 Test (org.junit.Test)1