1 /* 2 * Copyright (C) 2016 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 package com.android.tradefed.util; 17 18 import com.android.tradefed.error.HarnessIOException; 19 import com.android.tradefed.log.LogUtil.CLog; 20 import com.android.tradefed.result.error.InfraErrorIdentifier; 21 22 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; 23 import org.apache.commons.compress.archivers.zip.ZipFile; 24 25 import java.io.File; 26 import java.io.FileNotFoundException; 27 import java.io.IOException; 28 import java.nio.file.Files; 29 import java.util.Enumeration; 30 import java.util.HashSet; 31 import java.util.Set; 32 33 /** 34 * A helper class for zip extraction that takes POSIX file permissions into account 35 */ 36 public class ZipUtil2 { 37 38 /** 39 * A util method to apply unix mode from {@link ZipArchiveEntry} to the created local file 40 * system entry if necessary 41 * 42 * @param entry the entry inside zipfile (potentially contains mode info) 43 * @param localFile the extracted local file entry 44 * @return True if the Unix permissions are set, false otherwise. 45 * @throws IOException 46 */ applyUnixModeIfNecessary(ZipArchiveEntry entry, File localFile)47 private static boolean applyUnixModeIfNecessary(ZipArchiveEntry entry, File localFile) 48 throws IOException { 49 if (entry.getPlatform() == ZipArchiveEntry.PLATFORM_UNIX) { 50 Files.setPosixFilePermissions(localFile.toPath(), 51 FileUtil.unixModeToPosix(entry.getUnixMode())); 52 return true; 53 } 54 return false; 55 } 56 57 /** 58 * Utility method to extract a zip entry to a file. 59 * 60 * @param zipFile the {@link ZipFile} to extract 61 * @param entry the {@link ZipArchiveEntry} to extract 62 * @param destFile the {@link File} to extract to 63 * @return whether the Unix permissions are set 64 * @throws IOException if failed to extract file 65 */ extractZipEntry(ZipFile zipFile, ZipArchiveEntry entry, File destFile)66 private static boolean extractZipEntry(ZipFile zipFile, ZipArchiveEntry entry, File destFile) 67 throws IOException { 68 FileUtil.writeToFile(zipFile.getInputStream(entry), destFile); 69 return applyUnixModeIfNecessary(entry, destFile); 70 } 71 72 /** 73 * Utility method to extract entire contents of zip file into given directory 74 * 75 * @param zipFile the {@link ZipFile} to extract 76 * @param destDir the local dir to extract file to 77 * @throws IOException if failed to extract file 78 */ extractZip(ZipFile zipFile, File destDir)79 public static void extractZip(ZipFile zipFile, File destDir) throws IOException { 80 Enumeration<? extends ZipArchiveEntry> entries = zipFile.getEntries(); 81 Set<String> noPermissions = new HashSet<>(); 82 while (entries.hasMoreElements()) { 83 ZipArchiveEntry entry = entries.nextElement(); 84 File childFile = new File(destDir, entry.getName()); 85 ZipUtil.validateDestinationDir(destDir, entry.getName()); 86 childFile.getParentFile().mkdirs(); 87 if (entry.isDirectory()) { 88 childFile.mkdirs(); 89 if (!applyUnixModeIfNecessary(entry, childFile)) { 90 noPermissions.add(entry.getName()); 91 } 92 continue; 93 } else { 94 if (!extractZipEntry(zipFile, entry, childFile)) { 95 noPermissions.add(entry.getName()); 96 } 97 } 98 } 99 if (!noPermissions.isEmpty()) { 100 CLog.d( 101 "Entries '%s' exist but do not contain Unix mode permission info. Files will " 102 + "have default permission.", 103 noPermissions); 104 } 105 } 106 107 /** 108 * Utility method to extract a zip file into a given directory. The zip file being presented as 109 * a {@link File}. 110 * 111 * @param toUnzip a {@link File} pointing to a zip file. 112 * @param destDir the local dir to extract file to 113 * @throws IOException if failed to extract file 114 */ extractZip(File toUnzip, File destDir)115 public static void extractZip(File toUnzip, File destDir) throws IOException { 116 // Extract fast 117 try (java.util.zip.ZipFile zipFile = new java.util.zip.ZipFile(toUnzip)) { 118 ZipUtil.extractZip(zipFile, destDir); 119 } catch (IOException e) { 120 if (e instanceof FileNotFoundException) { 121 throw new HarnessIOException(e, InfraErrorIdentifier.ARTIFACT_INVALID); 122 } 123 throw e; 124 } 125 // Then restore permissions 126 try (ZipFile zip = new ZipFile(toUnzip)) { 127 restorePermissions(zip, destDir); 128 } 129 } 130 131 /** 132 * Utility method to extract one specific file from zip file 133 * 134 * @param zipFile the {@link ZipFile} to extract 135 * @param filePath the file path in the zip 136 * @param destFile the {@link File} to extract to 137 * @return whether the file is found and extracted 138 * @throws IOException if failed to extract file 139 */ extractFileFromZip(ZipFile zipFile, String filePath, File destFile)140 public static boolean extractFileFromZip(ZipFile zipFile, String filePath, File destFile) 141 throws IOException { 142 ZipArchiveEntry entry = zipFile.getEntry(filePath); 143 if (entry == null) { 144 return false; 145 } 146 extractZipEntry(zipFile, entry, destFile); 147 return true; 148 } 149 150 /** 151 * Utility method to extract one specific file from zip file into a tmp file 152 * 153 * @param zipFile the {@link ZipFile} to extract 154 * @param filePath the filePath of to extract 155 * @throws IOException if failed to extract file 156 * @return the {@link File} or null if not found 157 */ extractFileFromZip(ZipFile zipFile, String filePath)158 public static File extractFileFromZip(ZipFile zipFile, String filePath) throws IOException { 159 ZipArchiveEntry entry = zipFile.getEntry(filePath); 160 if (entry == null) { 161 return null; 162 } 163 File createdFile = FileUtil.createTempFile("extracted", FileUtil.getExtension(filePath)); 164 extractZipEntry(zipFile, entry, createdFile); 165 return createdFile; 166 } 167 168 /** 169 * Extract a zip file to a temp directory prepended with a string 170 * 171 * @param zipFile the zip file to extract 172 * @param nameHint a prefix for the temp directory 173 * @return a {@link File} pointing to the temp directory 174 */ extractZipToTemp(File zipFile, String nameHint)175 public static File extractZipToTemp(File zipFile, String nameHint) throws IOException { 176 File localRootDir = FileUtil.createTempDir(nameHint); 177 try { 178 extractZip(zipFile, localRootDir); 179 return localRootDir; 180 } catch (IOException e) { 181 // clean tmp file since we couldn't extract. 182 FileUtil.recursiveDelete(localRootDir); 183 throw e; 184 } 185 } 186 187 /** 188 * Close an open {@link ZipFile}, ignoring any exceptions. 189 * 190 * @param zipFile the file to close 191 */ closeZip(ZipFile zipFile)192 public static void closeZip(ZipFile zipFile) { 193 if (zipFile != null) { 194 try { 195 zipFile.close(); 196 } catch (IOException e) { 197 // ignore 198 } 199 } 200 } 201 202 /** Match permission on an already extracted destination directory. */ restorePermissions(ZipFile zipFile, File destDir)203 private static void restorePermissions(ZipFile zipFile, File destDir) throws IOException { 204 Enumeration<? extends ZipArchiveEntry> entries = zipFile.getEntries(); 205 Set<String> noPermissions = new HashSet<>(); 206 while (entries.hasMoreElements()) { 207 ZipArchiveEntry entry = entries.nextElement(); 208 File childFile = new File(destDir, entry.getName()); 209 if (!applyUnixModeIfNecessary(entry, childFile)) { 210 noPermissions.add(entry.getName()); 211 } 212 } 213 if (!noPermissions.isEmpty()) { 214 CLog.e( 215 "Entries '%s' exist but do not contain Unix mode permission info. Files will " 216 + "have default permission.", 217 noPermissions); 218 } 219 } 220 } 221