Search in sources :

Example 6 with ZipFileEntry

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

the class ZipInputEntry method add.

/**
   * Adds this entry to a zip using the given {@code ZipCombiner}. Entry can be either a directory
   * or a file.
   */
public void add(ZipCombiner combiner) throws IOException {
    ZipFileEntry entry = new ZipFileEntry(zipPath);
    if (Files.isDirectory(source)) {
        String name = entry.getName();
        if (!name.endsWith("/")) {
            name = name + "/";
        }
        combiner.addDirectory(name, DOS_EPOCH);
        return;
    }
    try (InputStream inputStream = Files.newInputStream(source)) {
        entry.setTime(DOS_EPOCH.getTime());
        entry.setVersion(MADE_BY_VERSION);
        entry.setExternalAttributes(externalFileAttribute);
        combiner.addFile(entry, inputStream);
    }
}
Also used : InputStream(java.io.InputStream) ZipFileEntry(com.google.devtools.build.zip.ZipFileEntry)

Example 7 with ZipFileEntry

use of com.google.devtools.build.zip.ZipFileEntry 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)

Example 8 with ZipFileEntry

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

the class ZipCombiner method finish.

/**
   * Writes any remaining output data to the output stream and also creates the merged entries by
   * calling the {@link CustomMergeStrategy} implementations given back from the
   * {@link ZipEntryFilter}.
   *
   * @throws IOException if the output stream or the filter throws an IOException
   * @throws IllegalStateException if this method was already called earlier
   */
public void finish() throws IOException {
    for (Entry<String, EntryAction> entry : actions.entrySet()) {
        String filename = entry.getKey();
        EntryAction action = entry.getValue();
        if (action.getType() == ActionType.MERGE) {
            ByteArrayOutputStream uncompressed = action.getMergeBuffer();
            action.getStrategy().finish(uncompressed);
            ZipFileEntry e = new ZipFileEntry(filename);
            e.setTime(action.getDate() != null ? action.getDate().getTime() : new Date().getTime());
            writeEntryFromBuffer(e, uncompressed.toByteArray());
        }
    }
    out.finish();
}
Also used : ZipFileEntry(com.google.devtools.build.zip.ZipFileEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Date(java.util.Date)

Example 9 with ZipFileEntry

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

the class BundleMerging method addEntriesFromOtherZip.

/**
   * Copies all entries from the source zip into a destination zip using the given combiner. The
   * contents of the source zip can appear to be in a sub-directory of the destination zip by
   * passing a non-empty string for the entry names prefix with a trailing '/'.
   */
private void addEntriesFromOtherZip(ZipCombiner combiner, Path sourceZip, String entryNamesPrefix) throws IOException {
    Map<String, Integer> externalFileAttributes = ZipFiles.unixExternalFileAttributes(sourceZip);
    try (ZipInputStream zipIn = new ZipInputStream(Files.newInputStream(sourceZip))) {
        while (true) {
            ZipEntry zipInEntry = zipIn.getNextEntry();
            if (zipInEntry == null) {
                break;
            }
            // TODO(bazel-dev): Add support for soft links because we will need them for MacOS support
            // in frameworks at the very least. https://github.com/bazelbuild/bazel/issues/289
            String name = entryNamesPrefix + zipInEntry.getName();
            if (zipInEntry.isDirectory()) {
                // same leaf files.
                if (!combiner.containsFile(name)) {
                    combiner.addDirectory(name, DOS_EPOCH);
                }
                continue;
            }
            Integer externalFileAttr = externalFileAttributes.get(zipInEntry.getName());
            if (externalFileAttr == null) {
                externalFileAttr = ZipInputEntry.DEFAULT_EXTERNAL_FILE_ATTRIBUTE;
            }
            ZipFileEntry zipOutEntry = new ZipFileEntry(name);
            zipOutEntry.setTime(DOS_EPOCH.getTime());
            zipOutEntry.setVersion(ZipInputEntry.MADE_BY_VERSION);
            zipOutEntry.setExternalAttributes(externalFileAttr);
            combiner.addFile(zipOutEntry, zipIn);
        }
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) ZipFileEntry(com.google.devtools.build.zip.ZipFileEntry)

Aggregations

ZipFileEntry (com.google.devtools.build.zip.ZipFileEntry)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 InputStream (java.io.InputStream)3 Date (java.util.Date)3 ZipReader (com.google.devtools.build.zip.ZipReader)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 DeflaterInputStream (java.util.zip.DeflaterInputStream)2 InflaterInputStream (java.util.zip.InflaterInputStream)2 RepositoryFunctionException (com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException)1 Path (com.google.devtools.build.lib.vfs.Path)1 ExtraDataList (com.google.devtools.build.zip.ExtraDataList)1 IOException (java.io.IOException)1 ZipEntry (java.util.zip.ZipEntry)1 ZipInputStream (java.util.zip.ZipInputStream)1 Nullable (javax.annotation.Nullable)1