-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTools.java
313 lines (265 loc) · 10.6 KB
/
Tools.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package com.ferreiraz.lib;
import android.os.Environment;
import android.util.Log;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class Tools {
public static void AssyncDownloads() {
/* new Thread(new Runnable() {
@Override
public void run() {
EVPContract.Aulas aula = EVPContract.GetAulaToDownload();
if(aula.status == 1) {
if(aula.link_pdf != "") {
String[] stringArray = aula.link_pdf.split("/");
File file = new File(LoadingActivity.DOWNLOAD_DIR, stringArray[stringArray.length - 1]);
DownloadFile(file, aula.link_pdf, aula.name);
}
if(aula.link_mp4 != "") {
String[] stringArray = aula.link_mp4.split("/");
File file = new File(LoadingActivity.DOWNLOAD_DIR, stringArray[stringArray.length - 1]);
DownloadFile(file, aula.link_mp4, aula.name);
}
if(aula.link_flv != "") {
String[] stringArray = aula.link_flv.split("/");
File file = new File(LoadingActivity.DOWNLOAD_DIR, stringArray[stringArray.length - 1]);
DownloadFile(file, aula.link_flv, aula.name);
}
}
}
}).start();*/
}
static Boolean isDownloading = false;
static HashMap<String, DownloadDataInfo> StatusList;
public static void LogException(Exception e) {
LogDetails(e.getLocalizedMessage().concat("\r\n\t").concat(e.getMessage()));
}
public static void LogDetails(String string) {
Log.e("FZ", string.concat("\r\n======================================================================"));
}
public static String ExpansionPath(String packName) {
return Environment.getExternalStorageDirectory().getAbsolutePath()
.concat("/Android/obb/").concat(StaticPackageName())
.concat("/").concat(packName);
}
public static String MainExpansionPath() {
return ExpansionPath(MainExpansionName());
}
public static String MainExpansionName() {
return "main.1.".concat(StaticPackageName()).concat(".obb");
}
public static String StaticPackageName() {
return Tools.class.getPackage().getName();
}
public static Boolean DownloadFile(File file, String url, String descriptor) {
if(!file.exists()) {
OutputStream fileOutput;
try {
LogDetails("Iniciando Download de " + url);
fileOutput = new FileOutputStream(file);
byte[] bytes = DownloadData(url, descriptor).toByteArray();
if(bytes == null)
throw new Exception("Erro de Download");
fileOutput.write(bytes);
fileOutput.flush();
fileOutput.close();
LogDetails("Concluido Download de " + url);
} catch(Exception e) {
LogException(e);
}
}
return file.exists();
}
public static Boolean DownloadFile(File file, String urlString) {
return DownloadFile(file, urlString, urlString);
}
public static String GZIPStreamToString(GZIPInputStream gzipInputStream) {
String returnString = "";
try {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
for (int value = 0; value != -1;) {
value = gzipInputStream.read();
if (value != -1) {
byteArrayOutputStream.write(value);
}
}
gzipInputStream.close();
byteArrayOutputStream.close();
returnString = new String(byteArrayOutputStream.toByteArray(), "UTF-8");
} catch (Exception e) {
}
return returnString;
}
static class DownloadDataInfo {
String name = "";
int bytesLength = 0;
int bytesDone = 0;
int bytesPercent = 0;
public DownloadDataInfo(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setBytesLength(int bytesLength) {
this.bytesLength = bytesLength;
}
public void setBytesDone(int bytesDone) {
this.bytesDone = bytesDone;
if(bytesDone > 0) {
bytesPercent = (int) (bytesDone * 100 / bytesLength);
}
}
public int getPercent() {
return bytesPercent;
}
}
public static ByteArrayOutputStream DownloadData(String uri) {
String[] stringArray = uri.split("/");
return DownloadData(uri, stringArray[stringArray.length - 1]);
}
public static ByteArrayOutputStream DownloadData(String uri, String descriptor) {
if(StatusList == null)
StatusList = new HashMap<String, DownloadDataInfo>();
URL url;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
DownloadDataInfo downloadDataInfo = new DownloadDataInfo(descriptor);
StatusList.put(uri, downloadDataInfo);
url = new URL (uri);
URLConnection urlConnection = url.openConnection();
urlConnection.connect();
StatusList.get(uri).setBytesLength(urlConnection.getContentLength());
InputStream inputStream = urlConnection.getInputStream();
try {
try {
byte[] byteArray = new byte[2048];
int bytesRead = 0;
while ((bytesRead = inputStream.read(byteArray, 0, byteArray.length)) >= 0) {
byteArrayOutputStream.write(byteArray, 0, bytesRead);
StatusList.get(uri).setBytesDone(byteArrayOutputStream.size());
}
} finally {
if(byteArrayOutputStream != null)
byteArrayOutputStream.close();
}
} finally {
if(inputStream != null)
inputStream.close();
}
} catch(Exception e) {
LogException(e);
} finally {
if(StatusList.containsKey(uri))
StatusList.remove(uri);
}
return byteArrayOutputStream;
}
public static boolean unpackZip(String path, String zipname)
{
InputStream inputStream;
ZipInputStream zipInputStream;
try
{
LogDetails("Iniciando Unzip de " + zipname);
String filename;
inputStream = new FileInputStream(zipname);
zipInputStream = new ZipInputStream(new BufferedInputStream(inputStream));
ZipEntry zipEntry;
byte[] buffer = new byte[1024];
int count;
if(path.substring(path.length() - 1) != "/")
path = path.concat("/");
while ((zipEntry = zipInputStream.getNextEntry()) != null)
{
filename = zipEntry.getName();
if (zipEntry.isDirectory()) {
File fmd = new File(path + filename);
fmd.mkdirs();
continue;
}
File previous = new File(path + filename);
if(previous.exists())
previous.delete();
FileOutputStream fileOutputStream = new FileOutputStream(path + filename);
while ((count = zipInputStream.read(buffer)) != -1)
{
fileOutputStream.write(buffer, 0, count);
}
fileOutputStream.close();
zipInputStream.closeEntry();
}
zipInputStream.close();
LogDetails("Concluido Unzip de " + zipname);
}
catch(IOException e)
{
LogException(e);
return false;
}
return true;
}
public static HashMap<String, ByteArrayOutputStream> UnpackToStreams(ByteArrayInputStream byteArrayInputStream) {
ZipInputStream zipInputStream;
HashMap<String, ByteArrayOutputStream> returnInformation = new HashMap<String, ByteArrayOutputStream>();
try
{
LogDetails("Iniciando Unzip");
zipInputStream = new ZipInputStream(byteArrayInputStream);
ZipEntry zipEntry;
byte[] buffer = new byte[1024];
int count;
while ((zipEntry = zipInputStream.getNextEntry()) != null)
{
String entryName = zipEntry.getName();
if(!entryName.contains("fzn"))
continue;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
while ((count = zipInputStream.read(buffer)) != -1)
{
byteArrayOutputStream.write(buffer, 0, count);
}
byteArrayOutputStream.close();
returnInformation.put(entryName, byteArrayOutputStream);
zipInputStream.closeEntry();
}
zipInputStream.close();
LogDetails("Concluido Unzip");
} catch(IOException e) {
LogException(e);
}
return returnInformation;
}
public static String StreamToString(InputStream inputStream) throws Exception {
StringBuilder stringBuilder = new StringBuilder();
try {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String readedLine = "";
while ((readedLine = bufferedReader.readLine()) != null) {
stringBuilder.append(readedLine);
}
} catch (Exception e) {
LogException(e);
}
return stringBuilder.toString();
}
public static String FileToString(File file) {
String string = "";
try {
LogDetails("Iniciando Leitura de " + file.getAbsolutePath());
FileInputStream fileInputStream = new FileInputStream(file);
string = StreamToString(fileInputStream);
fileInputStream.close();
LogDetails("Concluida Leitura de " + file.getAbsoluteFile());
} catch (Exception e) {
LogException(e);
}
return string;
}
}