use of com.android.tools.build.bundletool.model.CompressionLevel in project bundletool by google.
the class AppBundleRecompressor method recompressAppBundle.
public void recompressAppBundle(File inputFile, File outputFile) {
try (ZipReader zipReader = ZipReader.createFromFile(inputFile.toPath());
ZipArchive newBundle = new ZipArchive(outputFile);
TempDirectory tempDirectory = new TempDirectory(getClass().getSimpleName())) {
ZipEntrySourceFactory sourceFactory = new ZipEntrySourceFactory(zipReader, tempDirectory);
List<ListenableFuture<ZipEntrySource>> sources = new ArrayList<>();
BundleConfig bundleConfig = extractBundleConfig(zipReader);
ImmutableSet<String> uncompressedAssetsModules = extractModulesWithUncompressedAssets(zipReader, bundleConfig);
CompressionManager compressionManager = new CompressionManager(bundleConfig, uncompressedAssetsModules);
for (Entry entry : zipReader.getEntries().values()) {
CompressionLevel compressionLevel = compressionManager.getCompressionLevel(entry);
// parallelization there either.
if (compressionLevel.equals(SAME_AS_SOURCE) || compressionLevel.equals(NO_COMPRESSION) || entry.getUncompressedSize() < LARGE_ENTRY_SIZE_THRESHOLD_BYTES) {
sources.add(immediateFuture(sourceFactory.create(entry, compressionLevel)));
} else {
sources.add(executor.submit(() -> sourceFactory.create(entry, compressionLevel)));
}
}
// as they're ready.
for (ListenableFuture<ZipEntrySource> sourceFuture : Futures.inCompletionOrder(sources)) {
ZipEntrySource source = Futures.getUnchecked(sourceFuture);
if (source.getCompressionLevel().isCompressed() && source.getCompressedSize() >= source.getUncompressedSize()) {
// No benefit in compressing, leave the file uncompressed.
newBundle.add(sourceFactory.create(source.getEntry(), NO_COMPRESSION));
} else {
newBundle.add(source);
}
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
Aggregations