• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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 android.webgl.cts;
18 
19 import android.util.Log;
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.FileOutputStream;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.lang.String;
26 import java.util.zip.ZipEntry;
27 import java.util.zip.ZipInputStream;
28 
29 
30 /**
31  * Some boilerplate code to unzip files.
32  */
33 public class ZipUtil {
34     private final static String TAG = "ZipUtil";
35 
36     /**
37      * Stream to a file.
38      */
streamToPath(InputStream is, File directory, String name)39     public static void streamToPath(InputStream is,
40                                   File directory,
41                                   String name) throws Exception {
42         File file = new File(directory, name);
43         streamToPath(is, file);
44     }
45 
streamToPath(InputStream is, File file)46     public static void streamToPath(InputStream is,
47                                   File file) throws Exception {
48         Log.i(TAG, "Streaming to path " + file.getPath());
49         OutputStream os = null;
50         os = new FileOutputStream(file);
51         int count = -1;
52         byte[] buffer = new byte[10 * 1024];
53         while ((count = is.read(buffer)) != -1) {
54             os.write(buffer, 0, count);
55         }
56         os.close();
57     }
58 
59     /**
60      * Unzip to a directory.
61      */
unzipToPath(InputStream is, File filePath)62     public static void unzipToPath(InputStream is,
63                                    File filePath) throws Exception {
64         ZipInputStream zis = new ZipInputStream(is);
65         unzipToPath(zis, filePath.getPath());
66     }
67 
unzipToPath(ZipInputStream zis, String path)68     public static void unzipToPath(ZipInputStream zis,
69                                    String path) throws Exception {
70         Log.i(TAG, "Unzipping to path " + path);
71         byte[] buffer = new byte[10 * 1024];
72         ZipEntry entry;
73         while ((entry = zis.getNextEntry()) != null) {
74             File entryFile = new File(path, entry.getName());
75             if (entry.isDirectory()) {
76                 if (!entryFile.exists()) {
77                    entryFile.mkdirs();
78                 }
79                 continue;
80             }
81             if (entryFile.getParentFile() != null &&
82                     !entryFile.getParentFile().exists()) {
83                 entryFile.getParentFile().mkdirs();
84             }
85             if (!entryFile.exists()) {
86                 entryFile.createNewFile();
87                 entryFile.setReadable(true);
88                 entryFile.setExecutable(true);
89             }
90             streamToPath(zis, entryFile);
91         }
92         zis.close();
93     }
94 
95     /**
96      * Cleanup a directory.
97      */
deleteDirectory(String directoryPath)98     static public boolean deleteDirectory(String directoryPath) {
99         File path = new File(directoryPath);
100         return deleteDirectory(path);
101     }
102 
deleteDirectory(File path)103     static public boolean deleteDirectory(File path) {
104         if (path.exists()) {
105             File[] files = path.listFiles();
106             for(int i = 0; i < files.length; i++) {
107                 if(files[i].isDirectory()) {
108                     deleteDirectory(files[i]);
109                 } else {
110                     files[i].delete();
111                 }
112             }
113             return path.delete();
114         }
115         return false;
116     }
117 }
118