1
1
package com .plugin .util ;
2
2
3
+ import android .os .Build ;
4
+
5
+ import java .io .BufferedInputStream ;
6
+ import java .io .BufferedOutputStream ;
3
7
import java .io .BufferedReader ;
8
+ import java .io .ByteArrayOutputStream ;
4
9
import java .io .File ;
5
10
import java .io .FileInputStream ;
6
11
import java .io .FileNotFoundException ;
7
12
import java .io .FileOutputStream ;
8
13
import java .io .IOException ;
9
14
import java .io .InputStream ;
10
15
import java .io .InputStreamReader ;
16
+ import java .util .ArrayList ;
17
+ import java .util .Enumeration ;
18
+ import java .util .HashSet ;
19
+ import java .util .Set ;
11
20
import java .util .jar .JarEntry ;
12
21
import java .util .jar .JarFile ;
22
+ import java .util .zip .ZipEntry ;
23
+ import java .util .zip .ZipException ;
24
+ import java .util .zip .ZipFile ;
25
+ import java .util .zip .ZipInputStream ;
13
26
14
27
public class FileUtil {
15
-
28
+
29
+ public static boolean copyFile (String source , String dest ) {
30
+ try {
31
+ return copyFile (new FileInputStream (new File (source )), dest );
32
+ } catch (FileNotFoundException e ) {
33
+ e .printStackTrace ();
34
+ }
35
+ return false ;
36
+ }
37
+
16
38
public static boolean copyFile (final InputStream inputStream , String dest ) {
17
39
LogUtil .d ("copyFile to " + dest );
18
40
FileOutputStream oputStream = null ;
@@ -50,16 +72,129 @@ public static boolean copyFile(final InputStream inputStream, String dest) {
50
72
return false ;
51
73
}
52
74
53
- public static boolean copyFile (String source , String dest ) {
75
+ public static boolean copySo (File sourceDir , String so , String dest ) {
76
+
54
77
try {
55
- return copyFile (new FileInputStream (new File (source )), dest );
56
- } catch (FileNotFoundException e ) {
78
+
79
+ String name = "lib" + File .separator + Build .CPU_ABI + File .separator + so ;
80
+ File sourceFile = new File (sourceDir , name );
81
+
82
+ if (!sourceFile .exists () && Build .CPU_ABI2 != null ) {
83
+ name = "lib" + File .separator + Build .CPU_ABI2 + File .separator + so ;
84
+ sourceFile = new File (sourceDir , name );
85
+
86
+ if (!sourceFile .exists ()) {
87
+ name = "lib" + File .separator + "armeabi" + File .separator + so ;
88
+ sourceFile = new File (sourceDir , name );
89
+ }
90
+ }
91
+
92
+ if (sourceFile .exists ()) {
93
+ copyFile (sourceFile .getAbsolutePath (), dest + File .separator + so );
94
+ }
95
+ } catch (Exception e ) {
57
96
e .printStackTrace ();
58
97
}
59
- return false ;
98
+
99
+ return true ;
60
100
}
61
101
62
- public static String readFileFromJar (String jarFilePath , String metaInfo ) {
102
+
103
+ public static Set <String > unZipSo (String apkFile , File tempDir ) {
104
+
105
+ HashSet <String > result = null ;
106
+
107
+ if (!tempDir .exists ()) {
108
+ tempDir .mkdirs ();
109
+ }
110
+
111
+ LogUtil .d ("开始解压到" , tempDir .getAbsolutePath ());
112
+
113
+ ZipFile zfile = null ;
114
+ boolean isSuccess = false ;
115
+ BufferedOutputStream fos = null ;
116
+ BufferedInputStream bis = null ;
117
+ try {
118
+ zfile = new ZipFile (apkFile );
119
+ ZipEntry ze = null ;
120
+ Enumeration zList = zfile .entries ();
121
+ while (zList .hasMoreElements ()) {
122
+ ze = (ZipEntry ) zList .nextElement ();
123
+ String relativePath = ze .getName ();
124
+
125
+ if (!relativePath .startsWith ("lib" + File .separator )) {
126
+ LogUtil .d ("不是lib目录,跳过" , relativePath );
127
+ continue ;
128
+ }
129
+
130
+ if (ze .isDirectory ()) {
131
+ File folder = new File (tempDir , relativePath );
132
+ LogUtil .d ("正在创建目录" , folder .getAbsolutePath ());
133
+ if (!folder .exists ()) {
134
+ folder .mkdirs ();
135
+ }
136
+
137
+ } else {
138
+
139
+ if (result == null ) {
140
+ result = new HashSet <String >(4 );
141
+ }
142
+
143
+ File targetFile = new File (tempDir , relativePath );
144
+ LogUtil .d ("正在解压文件" , targetFile .getAbsolutePath ());
145
+ if (!targetFile .getParentFile ().exists ()) {
146
+ targetFile .getParentFile ().mkdirs ();
147
+ }
148
+ targetFile .createNewFile ();
149
+
150
+ fos = new BufferedOutputStream (new FileOutputStream (targetFile ));
151
+ bis = new BufferedInputStream (zfile .getInputStream (ze ));
152
+ byte [] buffer = new byte [2048 ];
153
+ int count = -1 ;
154
+ while ((count = bis .read (buffer )) != -1 ) {
155
+ fos .write (buffer , 0 , count );
156
+ fos .flush ();
157
+ }
158
+ fos .close ();
159
+ fos = null ;
160
+ bis .close ();
161
+ bis = null ;
162
+
163
+ result .add (relativePath .substring (relativePath .lastIndexOf (File .separator ) +1 ));
164
+ }
165
+ }
166
+ isSuccess = true ;
167
+ } catch (IOException e ) {
168
+ e .printStackTrace ();
169
+ } finally {
170
+ if (fos != null ) {
171
+ try {
172
+ fos .close ();
173
+ } catch (IOException e ) {
174
+ e .printStackTrace ();
175
+ }
176
+ }
177
+ if (bis != null ) {
178
+ try {
179
+ bis .close ();
180
+ } catch (IOException e ) {
181
+ e .printStackTrace ();
182
+ }
183
+ }
184
+ if (zfile != null ) {
185
+ try {
186
+ zfile .close ();
187
+ } catch (IOException e ) {
188
+ e .printStackTrace ();
189
+ }
190
+ }
191
+ }
192
+
193
+ LogUtil .d ("解压结束" , isSuccess );
194
+ return result ;
195
+ }
196
+
197
+ public static void readFileFromJar (String jarFilePath , String metaInfo ) {
63
198
LogUtil .d ("readFileFromJar:" , jarFilePath , metaInfo );
64
199
JarFile jarFile = null ;
65
200
try {
@@ -68,9 +203,7 @@ public static String readFileFromJar(String jarFilePath, String metaInfo) {
68
203
if (entry != null ) {
69
204
InputStream input = jarFile .getInputStream (entry );
70
205
71
- String info = streamToString (input );
72
-
73
- return info ;
206
+ return ;
74
207
}
75
208
76
209
} catch (IOException e ) {
@@ -84,22 +217,24 @@ public static String readFileFromJar(String jarFilePath, String metaInfo) {
84
217
}
85
218
}
86
219
}
87
- return null ;
220
+ return ;
88
221
89
222
}
90
223
91
- private static String streamToString (InputStream input ) throws IOException {
92
-
93
- InputStreamReader isr = new InputStreamReader (input );
94
- BufferedReader reader = new BufferedReader (isr );
95
-
96
- String line ;
97
- StringBuffer sb = new StringBuffer ();
98
- while ((line = reader .readLine ()) != null ) {
99
- sb .append (line );
224
+ /**
225
+ * 递归删除文件及文件夹
226
+ * @param file
227
+ */
228
+ public static boolean deleteAll (File file ) {
229
+ if (file .isDirectory ()) {
230
+ File [] childFiles = file .listFiles ();
231
+ if (childFiles != null && childFiles .length > 0 ) {
232
+ for (int i = 0 ; i < childFiles .length ; i ++) {
233
+ deleteAll (childFiles [i ]);
234
+ }
235
+ }
100
236
}
101
- reader .close ();
102
- isr .close ();
103
- return sb .toString ();
237
+ return file .delete ();
104
238
}
239
+
105
240
}
0 commit comments