-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCacheExtractor.java
More file actions
189 lines (172 loc) · 6.97 KB
/
CacheExtractor.java
File metadata and controls
189 lines (172 loc) · 6.97 KB
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
//Created by Daniel on 24-07-2024
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.Toast;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.util.Collections;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class CacheExtractor extends AsyncTask<Void, Integer, Void> {
private Context mContext;
public String TAG = "EEFileUtility";
public String obbName;
public String obbZipName;
public long obbZipSize;
public String pDialogMessage;
public ProgressDialog progressDialog;
public CacheExtractor(Activity context, String oZn, String oN, long oZs){
mContext = context;
obbZipName = oZn;
obbName = oN;
obbZipSize = oZs;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(mContext);
progressDialog.setTitle("Extracting Obb/Cache"); // Customize message
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); // Set progress bar style
progressDialog.setMax(100); // Set maximum progress (adjust based on your task)
progressDialog.setCancelable(false); // Optional: Prevent user from canceling
progressDialog.show();
}
@Override
protected Void doInBackground(Void... voids) {
Handler handler = new Handler(Looper.getMainLooper());
if (checkIfFileExists(new File(mContext.getObbDir(), obbName))) {
return null;
}
if (checkIfFileExists(new File(mContext.getObbDir(), obbZipName))) new File(mContext.getObbDir(), obbZipName).delete();
Log.d(TAG, "Starting asset extraction: " + obbZipName);
AssetManager am = mContext.getAssets();
InputStream inputStream;
try {
inputStream = am.open(obbZipName, AssetManager.ACCESS_STREAMING);
} catch (IOException e){
Log.e(TAG, "extractObb: " + e);
return null;
}
byte[] buffer = new byte[8192]; // Define your chunk size here
int bytesRead;
File outputFile = null;
OutputStream outputStream = null;
long processed = 0;
pDialogMessage = "Extracting Cache from assets...";
try {
outputFile = new File(mContext.getObbDir(), obbZipName);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
outputStream = Files.newOutputStream(outputFile.toPath());
} else {
outputStream = new FileOutputStream(outputFile);
}
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
int progress = (int) ((bytesRead + processed) * 100 / obbZipSize);
handler.post(new Runnable() {
@Override
public void run() {
onProgressUpdate(progress);
}
});
processed += bytesRead;
}
} catch (Throwable throwable){
Log.e(TAG, "Error: " + throwable);
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
} catch (Exception e){
Log.d(TAG, "InputStream: " + e);
}
}
// Log the creation status after closing streams
if (checkIfFileExists(new File(mContext.getObbDir(), obbZipName))) {
Log.i(TAG, "extractObb: File " + obbZipName + " created successfully!");
} else {
Toast.makeText(mContext, "Extract Obb Error!", Toast.LENGTH_LONG).show();
Log.e(TAG, "extractObb: Failed to create file " + obbZipName);
return null;
}
Log.d(TAG, "Unzipping");
processed = 0;
pDialogMessage = "Unzipping cache to directory...";
try (ZipFile zip = new ZipFile(outputFile)) {
for (ZipEntry entry : Collections.list(zip.entries())) {
if (entry.isDirectory()) {
File dir = new File(mContext.getObbDir(), entry.getName());
dir.mkdirs();
} else {
File newFile = new File(mContext.getObbDir(), entry.getName());
try (InputStream in = zip.getInputStream(entry);
BufferedInputStream bis = new BufferedInputStream(in);
FileOutputStream out = new FileOutputStream(newFile);
BufferedOutputStream bos = new BufferedOutputStream(out)) {
byte[] buffer2 = new byte[8192];
int len;
while ((len = bis.read(buffer2)) > 0) {
bos.write(buffer2, 0, len);
int progress = (int) ((len + processed) * 100 / obbZipSize);
handler.post(new Runnable() {
@Override
public void run() {
onProgressUpdate(progress);
}
});
processed += len;
}
}
}
}
} catch (Throwable e){
Log.e(TAG, "UnZipError: ", e);
}
if (checkIfFileExists(new File(mContext.getObbDir(), obbName))) {
Log.d(TAG, "Successfully extracted zip file " + obbZipName + " and found " + obbName);
} else {
Toast.makeText(mContext, "Unzip Error!", Toast.LENGTH_LONG).show();
Log.e(TAG, "Successfully extracted zip file " + obbZipName + " but " + obbName + " not found.");
}
outputFile.delete();
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
progressDialog.setTitle(pDialogMessage);
progressDialog.setProgress(values[0]); // Update progress bar
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
progressDialog.dismiss(); // Dismiss progress dialog
try {
mContext.startActivity(new Intent(mContext, Class.forName(launchActivity)));
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
public boolean checkIfFileExists(File file) {
return file.exists();
}
}