• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.compatibility.common.util;
18 
19 import java.io.BufferedInputStream;
20 import java.io.BufferedOutputStream;
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.OutputStream;
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.List;
30 import java.util.zip.ZipEntry;
31 import java.util.zip.ZipOutputStream;
32 
33 public class ZipUtil {
34 
35     /**
36      * Utility method to create a zip file containing the given directory and
37      * all its contents.
38      *
39      * @param dir the directory to zip
40      * @param zipFile the zip file to create - it should not already exist
41      * @throws IOException if failed to create zip file
42      */
createZip(File dir, File zipFile)43     public static void createZip(File dir, File zipFile) throws IOException {
44         createZip(Collections.singletonList(dir), zipFile);
45     }
46 
47     /**
48      * Utility method to create a zip file containing the given directory and all its contents.
49      *
50      * @param files list of files or directories to zip
51      * @param zipFile the zip file to create - it should not already exist
52      * @throws IOException if failed to create zip file
53      */
createZip(List<File> files, File zipFile)54     public static void createZip(List<File> files, File zipFile) throws IOException {
55         ZipOutputStream out = null;
56         try {
57             FileOutputStream fileStream = new FileOutputStream(zipFile);
58             out = new ZipOutputStream(new BufferedOutputStream(fileStream));
59             for (File file : files) {
60                 addToZip(out, file, new ArrayList<>());
61             }
62         } catch (IOException e) {
63             zipFile.delete();
64             throw e;
65         } catch (RuntimeException e) {
66             zipFile.delete();
67             throw e;
68         } finally {
69             out.close();
70         }
71     }
72 
73     /**
74      * Recursively adds given file and its contents to ZipOutputStream
75      *
76      * @param out the {@link ZipOutputStream}
77      * @param file the {@link File} to add to the stream
78      * @param relativePathSegs the relative path of file, including separators
79      * @throws IOException if failed to add file to zip
80      */
addToZip(ZipOutputStream out, File file, List<String> relativePathSegs)81     public static void addToZip(ZipOutputStream out, File file, List<String> relativePathSegs)
82             throws IOException {
83         relativePathSegs.add(file.getName());
84         if (file.isDirectory()) {
85             // note: it appears even on windows, ZipEntry expects '/' as a path separator
86             relativePathSegs.add("/");
87         }
88         ZipEntry zipEntry = new ZipEntry(buildPath(relativePathSegs));
89         out.putNextEntry(zipEntry);
90         if (file.isFile()) {
91             writeToStream(file, out);
92         }
93         out.closeEntry();
94         if (file.isDirectory()) {
95             // recursively add contents
96             File[] subFiles = file.listFiles();
97             if (subFiles == null) {
98                 throw new IOException(String.format("Could not read directory %s",
99                         file.getAbsolutePath()));
100             }
101             for (File subFile : subFiles) {
102                 addToZip(out, subFile, relativePathSegs);
103             }
104             // remove the path separator
105             relativePathSegs.remove(relativePathSegs.size()-1);
106         }
107         // remove the last segment, added at beginning of method
108         relativePathSegs.remove(relativePathSegs.size()-1);
109     }
110 
111     /**
112      * Builds a file system path from a stack of relative path segments
113      *
114      * @param relativePathSegs the list of relative paths
115      * @return a {@link String} containing all relativePathSegs
116      */
buildPath(List<String> relativePathSegs)117     private static String buildPath(List<String> relativePathSegs) {
118         StringBuilder pathBuilder = new StringBuilder();
119         for (String segment : relativePathSegs) {
120             pathBuilder.append(segment);
121         }
122         return pathBuilder.toString();
123     }
124 
125     /**
126      * Helper method to write input file contents to output stream.
127      *
128      * @param file the input {@link File}
129      * @param out the {@link OutputStream}
130      *
131      * @throws IOException
132      */
writeToStream(File file, OutputStream out)133     private static void writeToStream(File file, OutputStream out) throws IOException {
134         InputStream inputStream = null;
135         try {
136             inputStream = new BufferedInputStream(new FileInputStream(file));
137             StreamUtil.copyStreams(inputStream, out);
138         } finally {
139             inputStream.close();
140         }
141     }
142 
143 }
144