com.android.tools.build.bundletool.io.ZipBuilder类的使用及代码示例

x33g5p2x  于2022-02-05 转载在 其他  
字(9.9k)|赞(0)|评价(0)|浏览(130)

本文整理了Java中com.android.tools.build.bundletool.io.ZipBuilder类的一些代码示例,展示了ZipBuilder类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ZipBuilder类的具体详情如下:
包路径:com.android.tools.build.bundletool.io.ZipBuilder
类名称:ZipBuilder

ZipBuilder介绍

[英]Builder for creating zip files.

The builder behaves lazily and does not write any output until #writeTo(Path) is invoked.
[中]用于创建zip文件的生成器。
构建器行为迟缓,在调用#writeTo(路径)之前不会写入任何输出。

代码示例

代码示例来源:origin: google/bundletool

public static Path createApksArchiveFile(BuildApksResult result, Path location) throws Exception {
 ZipBuilder archiveBuilder = new ZipBuilder();
 result.getVariantList().stream()
   .flatMap(variant -> variant.getApkSetList().stream())
   .flatMap(apkSet -> apkSet.getApkDescriptionList().stream())
   .forEach(
     apkDesc ->
       archiveBuilder.addFileWithContent(ZipPath.create(apkDesc.getPath()), DUMMY_BYTES));
 archiveBuilder.addFileWithProtoContent(ZipPath.create("toc.pb"), result);
 return archiveBuilder.writeTo(location);
}

代码示例来源:origin: google/bundletool

/**
 * Lazily creates empty directory at the specified path.
 *
 * <p>Note that creating an empty directory is not required to add files into that directory.
 *
 * <p>Will throw an exception if the path is already taken.
 */
public ZipBuilder addDirectory(ZipPath dir) {
 return addEntryInternal(
   dir, Entry.builder().setIsDirectory(true).setOptions(ImmutableSet.of()).build());
}

代码示例来源:origin: google/bundletool

/**
 * Lazily creates an entry at the specified path and with the given content.
 *
 * <p>Will throw an exception if the path is already taken.
 */
public ZipBuilder addFileWithContent(ZipPath toPath, byte[] content, EntryOption... options) {
 return addFile(toPath, () -> new ByteArrayInputStream(content), options);
}

代码示例来源:origin: google/bundletool

private Path createSimpleBaseModule() throws IOException {
 return new ZipBuilder()
   .addFileWithProtoContent(
     ZipPath.create("manifest/AndroidManifest.xml"), androidManifest(PKG_NAME))
   .writeTo(tmpDir.resolve("base.zip"));
}

代码示例来源:origin: google/bundletool

private ZipFile createZipFileWithFiles(String... fileNames) throws IOException {
  ZipBuilder zipBuilder = new ZipBuilder();
  for (String fileName : fileNames) {
   zipBuilder.addFileWithContent(ZipPath.create(fileName), new byte[1]);
  }
  Path zipPath = zipBuilder.writeTo(tmp.getRoot().toPath().resolve("output.jar"));
  return new ZipFile(zipPath.toFile());
 }
}

代码示例来源:origin: google/bundletool

private static ZipBuilder createBasicZipBuilder(BundleConfig config) {
 ZipBuilder zipBuilder = new ZipBuilder();
 zipBuilder.addFileWithContent(ZipPath.create("BundleConfig.pb"), config.toByteArray());
 return zipBuilder;
}

代码示例来源:origin: google/bundletool

@Test
 public void validateModuleZipFile_invokesRightSubValidatorMethods() throws Exception {
  Path modulePath =
    new ZipBuilder()
      .addDirectory(ZipPath.create("module"))
      .addFileWithContent(ZipPath.create("module/file.txt"), DUMMY_CONTENT)
      .writeTo(tempFolder.resolve("module.zip"));

  try (ZipFile moduleZip = new ZipFile(modulePath.toFile())) {
   new ValidatorRunner(ImmutableList.of(validator)).validateModuleZipFile(moduleZip);

   verify(validator).validateModuleZipFile(eq(moduleZip));
   verifyNoMoreInteractions(validator);
  }
 }
}

代码示例来源:origin: google/bundletool

@Test
public void directoryZipEntriesInModuleFiles_notIncludedInBundle() throws Exception {
 Path tmpBaseModulePath = Files.move(buildSimpleModule("base"), tmpDir.resolve("base.zip.tmp"));
 Path baseModulePath;
 // Copy the valid bundle, only add a directory zip entry.
 try (ZipFile tmpBaseModuleZip = new ZipFile(tmpBaseModulePath.toFile())) {
  baseModulePath =
    new ZipBuilder()
      .copyAllContentsFromZip(ZipPath.create(""), tmpBaseModuleZip)
      .addDirectory(ZipPath.create("directory-entry"))
      .writeTo(tmpDir.resolve("base.zip"));
 }
 BuildBundleCommand.builder()
   .setOutputPath(bundlePath)
   .setModulesPaths(ImmutableList.of(baseModulePath))
   .build()
   .execute();
 try (ZipFile bundleZip = new ZipFile(bundlePath.toFile())) {
  assertThat(Collections.list(bundleZip.entries()).stream().filter(ZipEntry::isDirectory))
    .isEmpty();
 }
}

代码示例来源:origin: google/bundletool

ZipBuilder zipBuilder = new ZipBuilder();
zipBuilder.addFileWithProtoContent(
  ZipPath.create(BUNDLE_CONFIG_FILE_NAME), bundle.getBundleConfig(), compression);
 for (Entry<ZipPath, InputStreamSupplier> metadataEntry :
   bundle.getBundleMetadata().getFileDataMap().entrySet()) {
  zipBuilder.addFile(
    METADATA_DIRECTORY.resolve(metadataEntry.getKey()),
    metadataEntry.getValue(),
  ZipPath entryPath = moduleDir.resolve(entry.getPath());
  if (entry.isDirectory()) {
   zipBuilder.addDirectory(entryPath);
  } else {
   zipBuilder.addFile(entryPath, entry.getContentSupplier(), compression);
 zipBuilder.addFileWithProtoContent(
   moduleDir.resolve(SpecialModuleEntry.ANDROID_MANIFEST.getPath()),
   module.getAndroidManifest().getManifestRoot().getProto(),
   .ifPresent(
     assetsConfig ->
       zipBuilder.addFileWithProtoContent(
         moduleDir.resolve(SpecialModuleEntry.ASSETS_TABLE.getPath()),
         assetsConfig,
   .ifPresent(
     nativeConfig ->

代码示例来源:origin: google/bundletool

@Test
public void metaInfDirectoryNotAModule() throws Exception {
 createBasicZipBuilderWithManifest()
   .addFileWithContent(ZipPath.create("META-INF/GOOG.RSA"), DUMMY_CONTENT)
   .addFileWithContent(ZipPath.create("META-INF/MANIFEST.SF"), DUMMY_CONTENT)
   .writeTo(bundleFile);
 AppBundle appBundle = AppBundle.buildFromZip(new ZipFile(bundleFile.toFile()));
 assertThat(appBundle.getFeatureModules().keySet())
   .containsExactly(BundleModuleName.create("base"));
}

代码示例来源:origin: google/bundletool

ZipBuilder zipBuilder = new ZipBuilder();
for (ModuleEntry entry : split.getEntries()) {
 ZipPath pathInApk = toApkEntryPath(entry.getPath());
  zipBuilder.addFileFromDisk(pathInApk, signedWearApk.toFile(), entryOptions);
 } else {
  zipBuilder.addFile(pathInApk, entry.getContentSupplier(), entryOptions);
  .ifPresent(
    resourceTable ->
      zipBuilder.addFileWithProtoContent(
        SpecialModuleEntry.RESOURCE_TABLE.getPath(), resourceTable));
zipBuilder.addFileWithProtoContent(
  ZipPath.create(MANIFEST_FILENAME), split.getAndroidManifest().getManifestRoot().getProto());
 zipBuilder.writeTo(outputPath);
} catch (IOException e) {
 throw new UncheckedIOException(

代码示例来源:origin: google/bundletool

@Test
public void testMultipleModules() throws Exception {
 createBasicZipBuilder(BUNDLE_CONFIG)
   .addFileWithProtoContent(ZipPath.create("base/manifest/AndroidManifest.xml"), MANIFEST)
   .addFileWithContent(ZipPath.create("base/dex/classes.dex"), DUMMY_CONTENT)
   .addFileWithContent(ZipPath.create("base/assets/file.txt"), DUMMY_CONTENT)
   .addFileWithProtoContent(ZipPath.create("detail/manifest/AndroidManifest.xml"), MANIFEST)
   .addFileWithContent(ZipPath.create("detail/assets/file.txt"), DUMMY_CONTENT)
   .writeTo(bundleFile);
 AppBundle appBundle = AppBundle.buildFromZip(new ZipFile(bundleFile.toFile()));
 assertThat(appBundle.getFeatureModules().keySet())
   .containsExactly(BundleModuleName.create("base"), BundleModuleName.create("detail"));
}

代码示例来源:origin: google/bundletool

@Test
public void validateBundleZipEntry_directory_throws() throws Exception {
 Path bundlePath =
   new ZipBuilder()
     .addDirectory(ZipPath.create("directory"))
     .writeTo(tempFolder.resolve("bundle.aab"));
 try (ZipFile bundleZip = new ZipFile(bundlePath.toFile())) {
  ArrayList<? extends ZipEntry> entries = Collections.list(bundleZip.entries());
  // Sanity check.
  assertThat(entries).hasSize(1);
  ValidationException exception =
    assertThrows(
      ValidationException.class,
      () -> new BundleZipValidator().validateBundleZipEntry(bundleZip, entries.get(0)));
  assertThat(exception).hasMessageThat().contains("zip file contains directory zip entry");
 }
}

代码示例来源:origin: google/bundletool

@Test
public void moduleWithWrongExtension_throws() throws Exception {
 Path nonZipModule = new ZipBuilder().writeTo(tmpDir.resolve("not_a_zip.txt"));
 IllegalArgumentException exception =
   assertThrows(
     IllegalArgumentException.class,
     () ->
       BuildBundleCommand.builder()
         .setOutputPath(bundlePath)
         .setModulesPaths(ImmutableList.of(nonZipModule))
         .build()
         .execute());
 assertThat(exception).hasMessageThat().contains("expected to have '.zip' extension");
}

代码示例来源:origin: google/bundletool

@Override
public void setTableOfContentsFile(BuildApksResult tableOfContentsProto) {
 apkSetZipBuilder.addFileWithProtoContent(
   ZipPath.create(TABLE_OF_CONTENTS_FILE), tableOfContentsProto);
}

代码示例来源:origin: google/bundletool

@Override
 public void writeTo(Path destinationPath) {
  try {
   apkSetZipBuilder.writeTo(destinationPath);
  } catch (IOException e) {
   throw new UncheckedIOException(
     String.format("Error while writing the APK Set archive to '%s'.", destinationPath), e);
  }
 }
}

代码示例来源:origin: google/bundletool

public ApkSetArchiveBuilder(
  SplitApkSerializer splitApkSerializer,
  StandaloneApkSerializer standaloneApkSerializer,
  Path tempDirectory) {
 this.splitApkSerializer = splitApkSerializer;
 this.standaloneApkSerializer = standaloneApkSerializer;
 this.tempDirectory = tempDirectory;
 this.apkSetZipBuilder = new ZipBuilder();
}

代码示例来源:origin: google/bundletool

/**
 * Copies all file entries from <code>srcZipFile</code> to <code>toDirectory</code> inside the
 * output being built. This operation cannot rewrite any already existing entry.
 *
 * <p>Note that zip directory entries (including empty directories) are not copied.
 */
public ZipBuilder copyAllContentsFromZip(
  ZipPath toDirectory, ZipFile srcZipFile, EntryOption... entryOptions) {
 Enumeration<? extends ZipEntry> zipEntries = srcZipFile.entries();
 while (zipEntries.hasMoreElements()) {
  ZipEntry zipEntry = zipEntries.nextElement();
  if (!zipEntry.isDirectory()) {
   addFileFromZip(toDirectory.resolve(zipEntry.getName()), srcZipFile, zipEntry, entryOptions);
  }
 }
 return this;
}

代码示例来源:origin: google/bundletool

private void addToApkSetArchive(String relativeApkPath) {
 Path fullApkPath = tempDirectory.resolve(relativeApkPath);
 checkFileExistsAndReadable(fullApkPath);
 apkSetZipBuilder.addFileFromDisk(
   ZipPath.create(relativeApkPath), fullApkPath.toFile(), EntryOption.UNCOMPRESSED);
}

代码示例来源:origin: google/bundletool

zipEntry.setSize(entryData.length);
zipEntry.setCompressedSize(entryData.length);
zipEntry.setCrc(computeCrc32(entryData));
outZip.putNextEntry(zipEntry);
outZip.write(entryData);

相关文章