Search in sources :

Example 1 with ZipFileEntry

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

the class ZipCombiner method addFile.

/**
   * Adds a file with the specified name and date to the combined ZIP file.
   *
   * @param filename the name of the file to create
   * @param date the modified time to assign to the file
   * @param in the {@link InputStream} containing the file data
   * @throws IOException if there is an error writing the file entry
   * @throws IllegalArgumentException if the combined ZIP file already contains a file of the same
   *     name.
   */
public void addFile(String filename, Date date, InputStream in) throws IOException {
    ZipFileEntry entry = new ZipFileEntry(filename);
    entry.setTime(date != null ? date.getTime() : new Date().getTime());
    addFile(entry, in);
}
Also used : ZipFileEntry(com.google.devtools.build.zip.ZipFileEntry) Date(java.util.Date)

Example 2 with ZipFileEntry

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

the class ZipCombiner method writeEntry.

/**
   * Writes an entry from the specified source {@link ZipReader} and {@link ZipFileEntry} using the
   * specified {@link EntryAction}.
   * 
   *  <p>Writes the output entry from the input entry performing inflation or deflation as needed
   *  and applies any values from the {@link EntryAction} as needed.
   */
private void writeEntry(ZipReader zip, ZipFileEntry entry, EntryAction action) throws IOException {
    checkArgument(action.getType() != ActionType.SKIP, "Cannot write a zip entry whose action is of type SKIP.");
    ZipFileEntry outEntry = new ZipFileEntry(entry);
    if (action.getType() == ActionType.RENAME) {
        checkNotNull(action.getNewName(), "ZipEntryFilter actions of type RENAME must not have a null filename.");
        outEntry.setName(action.getNewName());
    }
    if (action.getDate() != null) {
        outEntry.setTime(action.getDate().getTime());
    }
    InputStream data;
    if (mode == OutputMode.FORCE_DEFLATE && entry.getMethod() != Compression.DEFLATED) {
        // The output mode is deflate, but the entry compression is not. Create a deflater stream
        // from the raw file data and deflate to a temporary byte array to determine the deflated
        // size. Then use this byte array as the input stream for writing the entry.
        ByteArrayOutputStream tmp = new ByteArrayOutputStream();
        copyStream(new DeflaterInputStream(zip.getRawInputStream(entry), getDeflater()), tmp);
        data = new ByteArrayInputStream(tmp.toByteArray());
        outEntry.setMethod(Compression.DEFLATED);
        outEntry.setCompressedSize(tmp.size());
    } else if (mode == OutputMode.FORCE_STORED && entry.getMethod() != Compression.STORED) {
        // The output mode is stored, but the entry compression is not; create an inflater stream
        // from the raw file data. 
        data = new InflaterInputStream(zip.getRawInputStream(entry), getInflater());
        outEntry.setMethod(Compression.STORED);
        outEntry.setCompressedSize(entry.getSize());
    } else {
        // Entry compression agrees with output mode; use the raw file data as is.
        data = zip.getRawInputStream(entry);
    }
    writeEntry(outEntry, data);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) DeflaterInputStream(java.util.zip.DeflaterInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) DeflaterInputStream(java.util.zip.DeflaterInputStream) ZipFileEntry(com.google.devtools.build.zip.ZipFileEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 3 with ZipFileEntry

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

the class ZipCombiner method addDirectory.

/**
   * Adds a directory entry to the combined ZIP file using the specified filename, date, and extra
   * data.
   *
   * @param filename the name of the directory to create
   * @param date the modified time to assign to the directory
   * @param extra the extra field data to add to the directory entry
   * @throws IOException if there is an error writing the directory entry
   */
public void addDirectory(String filename, Date date, ExtraData[] extra) throws IOException {
    checkArgument(filename.endsWith("/"), "Directory names must end with a /");
    checkState(!entries.containsKey(filename), "Zip already contains a directory named %s", filename);
    ZipFileEntry entry = new ZipFileEntry(filename);
    entry.setMethod(Compression.STORED);
    entry.setCrc(0);
    entry.setSize(0);
    entry.setCompressedSize(0);
    entry.setTime(date != null ? date.getTime() : new Date().getTime());
    entry.setExtra(new ExtraDataList(extra));
    out.putNextEntry(entry);
    out.closeEntry();
    entries.put(filename, entry);
}
Also used : ZipFileEntry(com.google.devtools.build.zip.ZipFileEntry) ExtraDataList(com.google.devtools.build.zip.ExtraDataList) Date(java.util.Date)

Example 4 with ZipFileEntry

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

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

the class ZipCombiner method addFile.

/**
   * Adds a file with attributes specified by the {@link ZipFileEntry} to the combined ZIP file.
   *
   * @param entry the {@link ZipFileEntry} containing the entry meta-data
   * @param in the {@link InputStream} containing the file data
   * @throws IOException if there is an error writing the file entry
   * @throws IllegalArgumentException if the combined ZIP file already contains a file of the same
   *     name.
   */
public void addFile(ZipFileEntry entry, InputStream in) throws IOException {
    checkNotNull(entry, "Zip entry must not be null.");
    checkNotNull(in, "Input stream must not be null.");
    checkArgument(!entries.containsKey(entry.getName()), "Zip already contains a file named '%s'.", entry.getName());
    ByteArrayOutputStream uncompressed = new ByteArrayOutputStream();
    copyStream(in, uncompressed);
    writeEntryFromBuffer(new ZipFileEntry(entry), uncompressed.toByteArray());
}
Also used : ZipFileEntry(com.google.devtools.build.zip.ZipFileEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

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