1 // Copyright 2017 The Bazel Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 package com.google.devtools.build.android.desugar.io; 15 16 import com.google.common.base.Functions; 17 import com.google.common.collect.Iterators; 18 import java.io.IOException; 19 import java.io.InputStream; 20 import java.nio.file.Path; 21 import java.util.Iterator; 22 import java.util.zip.ZipEntry; 23 import java.util.zip.ZipFile; 24 25 /** Input provider is a zip file. */ 26 class ZipInputFileProvider implements InputFileProvider { 27 28 private final Path root; 29 30 private final ZipFile zipFile; 31 ZipInputFileProvider(Path root)32 public ZipInputFileProvider(Path root) throws IOException { 33 this.root = root; 34 this.zipFile = new ZipFile(root.toFile()); 35 } 36 37 @Override close()38 public void close() throws IOException { 39 zipFile.close(); 40 } 41 42 @Override toString()43 public String toString() { 44 return root.getFileName().toString(); 45 } 46 47 @Override getZipEntry(String filename)48 public ZipEntry getZipEntry(String filename) { 49 ZipEntry zipEntry = zipFile.getEntry(filename); 50 zipEntry.setCompressedSize(-1); 51 return zipEntry; 52 } 53 54 @Override getInputStream(String filename)55 public InputStream getInputStream(String filename) throws IOException { 56 return zipFile.getInputStream(zipFile.getEntry(filename)); 57 } 58 59 @Override iterator()60 public Iterator<String> iterator() { 61 return Iterators.transform( 62 Iterators.forEnumeration(zipFile.entries()), Functions.toStringFunction()); 63 } 64 } 65