Search in sources :

Example 6 with Entry

use of com.android.zipflinger.Entry in project bundletool by google.

the class ZipReader method getUncompressedPayload.

/**
 * Returns an {@link InputStream} of the uncompressed payload of a zip entry.
 */
@MustBeClosed
public InputStream getUncompressedPayload(String entryName) {
    Entry entry = getEntry(entryName).orElseThrow(() -> new EntryNotFoundException(zipMap.getFile(), entryName));
    InputStream entryPayload = getEntryPayload(entry);
    if (!entry.isCompressed()) {
        return entryPayload;
    }
    // nowrap = gzip compatible
    Inflater inflater = new Inflater(/* nowrap= */
    true);
    return new InflaterInputStream(entryPayload, inflater, BUFFER_SIZE_BYTES);
}
Also used : Entry(com.android.zipflinger.Entry) InflaterInputStream(java.util.zip.InflaterInputStream) PayloadInputStream(com.android.zipflinger.PayloadInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) EntryNotFoundException(com.android.tools.build.bundletool.io.ZipReader.EntryNotFoundException) Inflater(java.util.zip.Inflater) MustBeClosed(com.google.errorprone.annotations.MustBeClosed)

Example 7 with Entry

use of com.android.zipflinger.Entry in project bundletool by google.

the class ZipReader method transferTo.

/**
 * Copies the bytes of the payload of a zip entry to the given {@link ZipWriter}.
 *
 * <p>The copy happens using {@link FileChannel#transferTo} which takes advantage of filesystem
 * cache, making it a very I/O efficient way to copy data across files.
 */
public void transferTo(ZipWriter zipWriter, String entryName) {
    Entry entry = getEntry(entryName).orElseThrow(() -> new EntryNotFoundException(zipMap.getFile(), entryName));
    Location payloadLocation = entry.getPayloadLocation();
    try {
        zipWriter.transferFrom(fileChannel, payloadLocation.first, payloadLocation.size());
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : Entry(com.android.zipflinger.Entry) EntryNotFoundException(com.android.tools.build.bundletool.io.ZipReader.EntryNotFoundException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) Location(com.android.zipflinger.Location)

Example 8 with Entry

use of com.android.zipflinger.Entry in project bundletool by google.

the class ZipFlingerApkSerializer method writeToZipFile.

private void writeToZipFile(ModuleSplit split, Path outputPath, TempDirectory tempDir) throws IOException {
    checkFileDoesNotExist(outputPath);
    createParentDirectories(outputPath);
    split = apkSigner.signEmbeddedApks(split);
    // Write a Proto-APK with only files that aapt2 requires as part of the convert command.
    Path partialProtoApk = tempDir.getPath().resolve("proto.apk");
    writeProtoApk(split, partialProtoApk, tempDir);
    // Invoke aapt2 to convert files from proto to binary format.
    Path binaryApkPath = tempDir.getPath().resolve("binary.apk");
    if (enableSparseEncoding && split.getResourceTable().isPresent()) {
        Path interimApk = tempDir.getPath().resolve("interim.apk");
        aapt2.convertApkProtoToBinary(partialProtoApk, interimApk);
        aapt2.optimizeToSparseResourceTables(interimApk, binaryApkPath);
    } else {
        aapt2.convertApkProtoToBinary(partialProtoApk, binaryApkPath);
    }
    checkState(Files.exists(binaryApkPath), "No APK created by aapt2 convert command.");
    try (ZipArchive apkWriter = new ZipArchive(outputPath);
        ZipReader aapt2ApkReader = ZipReader.createFromFile(binaryApkPath)) {
        ImmutableMap<ZipPath, ModuleEntry> moduleEntriesByName = split.getEntries().stream().collect(toImmutableMap(entry -> ApkSerializerHelper.toApkEntryPath(entry.getPath()), entry -> entry, // e.g. base/assets/foo and base/root/assets/foo.
        (a, b) -> b));
        // Sorting entries by name for determinism.
        ImmutableSortedSet<ZipPath> sortedEntryNames = Stream.concat(aapt2ApkReader.getEntries().keySet().stream().map(ZipPath::create), moduleEntriesByName.keySet().stream()).collect(toImmutableSortedSet(naturalOrder()));
        ApkEntrySerializer apkEntrySerializer = new ApkEntrySerializer(apkWriter, aapt2ApkReader, split, tempDir);
        for (ZipPath pathInApk : sortedEntryNames) {
            Optional<Entry> aapt2Entry = aapt2ApkReader.getEntry(pathInApk.toString());
            if (aapt2Entry.isPresent()) {
                apkEntrySerializer.addAapt2Entry(pathInApk, aapt2Entry.get());
            } else {
                ModuleEntry moduleEntry = checkNotNull(moduleEntriesByName.get(pathInApk));
                apkEntrySerializer.addRegularEntry(pathInApk, moduleEntry);
            }
        }
    }
    apkSigner.signApk(outputPath, split);
}
Also used : Path(java.nio.file.Path) ZipPath(com.android.tools.build.bundletool.model.ZipPath) NO_ALIGNMENT(com.android.zipflinger.Source.NO_ALIGNMENT) Comparator.naturalOrder(java.util.Comparator.naturalOrder) Map(java.util.Map) ZipArchive(com.android.zipflinger.ZipArchive) Version(com.android.tools.build.bundletool.model.version.Version) NO_COMPRESSION_EXTENSIONS(com.android.tools.build.bundletool.io.ApkSerializerHelper.NO_COMPRESSION_EXTENSIONS) Path(java.nio.file.Path) ImmutableSortedSet.toImmutableSortedSet(com.google.common.collect.ImmutableSortedSet.toImmutableSortedSet) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Aapt2Command(com.android.tools.build.bundletool.androidtools.Aapt2Command) Preconditions.checkState(com.google.common.base.Preconditions.checkState) SpecialModuleEntry(com.android.tools.build.bundletool.model.BundleModule.SpecialModuleEntry) UncheckedIOException(java.io.UncheckedIOException) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) Stream(java.util.stream.Stream) ModuleSplit(com.android.tools.build.bundletool.model.ModuleSplit) BundleConfig(com.android.bundle.Config.BundleConfig) ModuleEntry(com.android.tools.build.bundletool.model.ModuleEntry) UseBundleCompression(com.android.tools.build.bundletool.commands.BuildApksManagerComponent.UseBundleCompression) FilePreconditions.checkFileDoesNotExist(com.android.tools.build.bundletool.model.utils.files.FilePreconditions.checkFileDoesNotExist) Optional(java.util.Optional) SparseEncoding(com.android.bundle.Config.ResourceOptimizations.SparseEncoding) Pattern(java.util.regex.Pattern) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) SAME_AS_SOURCE(com.android.tools.build.bundletool.model.CompressionLevel.SAME_AS_SOURCE) PathMatcher(com.android.tools.build.bundletool.model.utils.PathMatcher) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ZipPath(com.android.tools.build.bundletool.model.ZipPath) BytesSource(com.android.zipflinger.BytesSource) BEST_COMPRESSION(com.android.tools.build.bundletool.model.CompressionLevel.BEST_COMPRESSION) ApkListener(com.android.tools.build.bundletool.model.ApkListener) Inject(javax.inject.Inject) ImmutableList(com.google.common.collect.ImmutableList) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) ApkDescription(com.android.bundle.Commands.ApkDescription) CompressionLevel(com.android.tools.build.bundletool.model.CompressionLevel) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) MANIFEST_FILENAME(com.android.tools.build.bundletool.model.BundleModule.MANIFEST_FILENAME) Files(java.nio.file.Files) DEFAULT_COMPRESSION(com.android.tools.build.bundletool.model.CompressionLevel.DEFAULT_COMPRESSION) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) ApkSerializerHelper.requiresAapt2Conversion(com.android.tools.build.bundletool.io.ApkSerializerHelper.requiresAapt2Conversion) IOException(java.io.IOException) FileUtils.createParentDirectories(com.android.tools.build.bundletool.model.utils.files.FileUtils.createParentDirectories) VerboseLogs(com.android.tools.build.bundletool.commands.BuildApksModule.VerboseLogs) Entry(com.android.zipflinger.Entry) Maps(com.google.common.collect.Maps) NO_COMPRESSION(com.android.tools.build.bundletool.model.CompressionLevel.NO_COMPRESSION) NO_DEFAULT_UNCOMPRESS_EXTENSIONS(com.android.tools.build.bundletool.model.version.VersionGuardedFeature.NO_DEFAULT_UNCOMPRESS_EXTENSIONS) FileUtils(com.android.tools.build.bundletool.model.utils.files.FileUtils) SpecialModuleEntry(com.android.tools.build.bundletool.model.BundleModule.SpecialModuleEntry) ModuleEntry(com.android.tools.build.bundletool.model.ModuleEntry) Entry(com.android.zipflinger.Entry) SpecialModuleEntry(com.android.tools.build.bundletool.model.BundleModule.SpecialModuleEntry) ModuleEntry(com.android.tools.build.bundletool.model.ModuleEntry) ZipArchive(com.android.zipflinger.ZipArchive) ZipPath(com.android.tools.build.bundletool.model.ZipPath)

Aggregations

Entry (com.android.zipflinger.Entry)8 SpecialModuleEntry (com.android.tools.build.bundletool.model.BundleModule.SpecialModuleEntry)5 ModuleEntry (com.android.tools.build.bundletool.model.ModuleEntry)5 ZipArchive (com.android.zipflinger.ZipArchive)5 IOException (java.io.IOException)5 UncheckedIOException (java.io.UncheckedIOException)5 BundleConfig (com.android.bundle.Config.BundleConfig)4 ZipPath (com.android.tools.build.bundletool.model.ZipPath)4 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)4 ApkDescription (com.android.bundle.Commands.ApkDescription)3 VerboseLogs (com.android.tools.build.bundletool.commands.BuildApksModule.VerboseLogs)3 ApkSerializerHelper.requiresAapt2Conversion (com.android.tools.build.bundletool.io.ApkSerializerHelper.requiresAapt2Conversion)3 ApkListener (com.android.tools.build.bundletool.model.ApkListener)3 ModuleSplit (com.android.tools.build.bundletool.model.ModuleSplit)3 PathMatcher (com.android.tools.build.bundletool.model.utils.PathMatcher)3 FileUtils (com.android.tools.build.bundletool.model.utils.files.FileUtils)3 Version (com.android.tools.build.bundletool.model.version.Version)3 NO_DEFAULT_UNCOMPRESS_EXTENSIONS (com.android.tools.build.bundletool.model.version.VersionGuardedFeature.NO_DEFAULT_UNCOMPRESS_EXTENSIONS)3 ApkSerializerHelper.toApkEntryPath (com.android.tools.build.bundletool.io.ApkSerializerHelper.toApkEntryPath)2 EntryNotFoundException (com.android.tools.build.bundletool.io.ZipReader.EntryNotFoundException)2