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);
}
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);
}
}
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);
}
Aggregations