Search in sources :

Example 1 with TABLE_OF_CONTENTS_FILE

use of com.android.tools.build.bundletool.model.utils.FileNames.TABLE_OF_CONTENTS_FILE in project bundletool by google.

the class ApkSetWriter method zip.

/**
 * Creates ApkSet writer which stores all splits as ZIP archive.
 */
static ApkSetWriter zip(Path tempDirectory, Path outputFile) {
    return new ApkSetWriter() {

        @Override
        public Path getSplitsDirectory() {
            return tempDirectory;
        }

        @Override
        public void writeApkSet(BuildApksResult toc) throws IOException {
            Stream<ApkDescription> apks = toc.getVariantList().stream().flatMap(variant -> variant.getApkSetList().stream()).flatMap(apkSet -> apkSet.getApkDescriptionList().stream());
            Stream<ApkDescription> assets = toc.getAssetSliceSetList().stream().flatMap(assetSliceSet -> assetSliceSet.getApkDescriptionList().stream());
            ImmutableSet<String> apkRelativePaths = Stream.concat(apks, assets).map(ApkDescription::getPath).sorted().collect(toImmutableSet());
            zipApkSet(apkRelativePaths, toc.toByteArray());
        }

        @Override
        public void writeApkSet(BuildSdkApksResult toc) throws IOException {
            Stream<ApkDescription> apks = toc.getVariantList().stream().flatMap(variant -> variant.getApkSetList().stream()).flatMap(apkSet -> apkSet.getApkDescriptionList().stream());
            ImmutableSet<String> apkRelativePaths = apks.map(ApkDescription::getPath).sorted().collect(toImmutableSet());
            zipApkSet(apkRelativePaths, toc.toByteArray());
        }

        private void zipApkSet(ImmutableSet<String> apkRelativePaths, byte[] tocBytes) throws IOException {
            try (ZipArchive zipArchive = new ZipArchive(outputFile)) {
                zipArchive.add(new BytesSource(tocBytes, TABLE_OF_CONTENTS_FILE, Deflater.NO_COMPRESSION));
                for (String relativePath : apkRelativePaths) {
                    zipArchive.add(new LargeFileSource(getSplitsDirectory().resolve(relativePath), /* tmpStorage= */
                    null, relativePath, Deflater.NO_COMPRESSION));
                }
            }
        }
    };
}
Also used : TABLE_OF_CONTENTS_FILE(com.android.tools.build.bundletool.model.utils.FileNames.TABLE_OF_CONTENTS_FILE) ImmutableSet(com.google.common.collect.ImmutableSet) Files(java.nio.file.Files) BuildApksResult(com.android.bundle.Commands.BuildApksResult) BytesSource(com.android.zipflinger.BytesSource) LargeFileSource(com.android.zipflinger.LargeFileSource) IOException(java.io.IOException) Deflater(java.util.zip.Deflater) Stream(java.util.stream.Stream) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) ApkDescription(com.android.bundle.Commands.ApkDescription) ZipArchive(com.android.zipflinger.ZipArchive) BuildSdkApksResult(com.android.bundle.Commands.BuildSdkApksResult) Path(java.nio.file.Path) BuildSdkApksResult(com.android.bundle.Commands.BuildSdkApksResult) BytesSource(com.android.zipflinger.BytesSource) ApkDescription(com.android.bundle.Commands.ApkDescription) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) BuildApksResult(com.android.bundle.Commands.BuildApksResult) ZipArchive(com.android.zipflinger.ZipArchive) LargeFileSource(com.android.zipflinger.LargeFileSource)

Aggregations

ApkDescription (com.android.bundle.Commands.ApkDescription)1 BuildApksResult (com.android.bundle.Commands.BuildApksResult)1 BuildSdkApksResult (com.android.bundle.Commands.BuildSdkApksResult)1 TABLE_OF_CONTENTS_FILE (com.android.tools.build.bundletool.model.utils.FileNames.TABLE_OF_CONTENTS_FILE)1 BytesSource (com.android.zipflinger.BytesSource)1 LargeFileSource (com.android.zipflinger.LargeFileSource)1 ZipArchive (com.android.zipflinger.ZipArchive)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)1 IOException (java.io.IOException)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Stream (java.util.stream.Stream)1 Deflater (java.util.zip.Deflater)1