Home | History | Annotate | Download | only in bundletool
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License
     15  */
     16 
     17 package com.android.tools.appbundle.bundletool;
     18 
     19 import com.google.common.collect.ImmutableMap;
     20 import com.google.common.collect.Maps;
     21 import java.io.IOException;
     22 import java.io.InputStream;
     23 import java.nio.file.Path;
     24 import java.nio.file.Paths;
     25 import java.util.Enumeration;
     26 import java.util.HashMap;
     27 import java.util.Map;
     28 import java.util.zip.ZipEntry;
     29 import java.util.zip.ZipFile;
     30 
     31 /** Represents an app bundle. */
     32 public class AppBundle {
     33 
     34   private ZipFile bundleFile;
     35   private Map<String, BundleModule> modules;
     36 
     37   public AppBundle(ZipFile bundleFile) {
     38     this.bundleFile = bundleFile;
     39     this.modules = new HashMap<>();
     40     open();
     41   }
     42 
     43   private void open() {
     44     Map<String, BundleModule.Builder> moduleBuilders = new HashMap<>();
     45     Enumeration<? extends ZipEntry> entries = bundleFile.entries();
     46     while (entries.hasMoreElements()) {
     47       ZipEntry entry = entries.nextElement();
     48       Path path = Paths.get(entry.getName());
     49       if (path.getNameCount() > 1) {
     50         String moduleName = path.getName(0).toString();
     51         BundleModule.Builder moduleBuilder =
     52             moduleBuilders.computeIfAbsent(
     53                 moduleName, name -> new BundleModule.Builder(name, this));
     54         moduleBuilder.addZipEntry(entry);
     55       }
     56     }
     57     modules.putAll(Maps.transformValues(moduleBuilders, BundleModule.Builder::build));
     58   }
     59 
     60   public Map<String, BundleModule> getModules() {
     61     return ImmutableMap.copyOf(modules);
     62   }
     63 
     64   public BundleModule getModule(String moduleName) {
     65     return modules.get(moduleName);
     66   }
     67 
     68   public InputStream getEntryInputStream(ZipEntry entry) throws IOException {
     69     return bundleFile.getInputStream(entry);
     70   }
     71 }
     72