Skip to content

Commit

Permalink
Update unzip so files logic
Browse files Browse the repository at this point in the history
  • Loading branch information
luoyesiqiu committed Apr 3, 2024
1 parent 40ca24b commit 595eb77
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 14 deletions.
1 change: 1 addition & 0 deletions shell/src/main/java/com/luoyesiqiu/shell/JniBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public static void loadShellLibs(String workspacePath,String apkPath) {
for(String libName : allowLibNames) {
String libSuffix = File.separator + libName;
if(fullLibPath.endsWith(libSuffix)) {
Log.d(TAG, "loadShellLibs: " + fullLibPath);
System.load(fullLibPath);
}
}
Expand Down
63 changes: 49 additions & 14 deletions shell/src/main/java/com/luoyesiqiu/shell/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

Expand All @@ -24,35 +27,67 @@ public static void unzipLibs(String sourceDir,String dataDir) {
String abiName = EnvUtils.getAbiDirName(sourceDir);

File libsOutDir = new File(dataDir + File.separator + Global.LIB_DIR + File.separator + abiName);
File shellSoFile = new File(libsOutDir.getAbsolutePath(),Global.SHELL_SO_NAME);
if(!shellSoFile.exists()) {
FileUtils.unzip(sourceDir,
"assets/" + Global.ZIP_LIB_DIR + "/" + abiName + "/", /* 注意最后一个斜杠 */
libsOutDir.getAbsolutePath());
FileUtils.unzipInNeeded(sourceDir,
"assets/" + Global.ZIP_LIB_DIR + "/" + abiName + "/", /* 注意最后一个斜杠 */
libsOutDir.getAbsolutePath());
}

public static long getCrc32(File f) {
FileInputStream fileInputStream = null;
CheckedInputStream checkedInputStream = null;
long crcResult = 0L;
try {
fileInputStream = new FileInputStream(f);
checkedInputStream = new CheckedInputStream(fileInputStream,new CRC32());
int len = -1;
byte[] buf = new byte[4096];
while((len = checkedInputStream.read(buf)) != -1) {
}
crcResult = checkedInputStream.getChecksum().getValue();
}
catch (Throwable e){
}
finally {
FileUtils.close(checkedInputStream);
}
return crcResult;
}
public static void unzip(String zipFilePath,String entryName,String outDir){
public static void unzipInNeeded(String zipFilePath, String entryName, String outDir){
long start = System.currentTimeMillis();
File out = new File(outDir);
if(!out.exists()){
out.mkdirs();
}

long localFileCrc = 0L;
File entryFile = new File(outDir + File.separator + Global.SHELL_SO_NAME);
if(entryFile.exists()){
localFileCrc = getCrc32(entryFile);
}
try {
ZipFile zip = new ZipFile(zipFilePath);
Enumeration<? extends ZipEntry> entries = zip.entries();
while(entries.hasMoreElements()){
ZipEntry entry = entries.nextElement();

if(entry.getName().startsWith(entryName)) {
byte[] buf = new byte[4096];
int len = -1;
File entryFile = new File(outDir + File.separator + Global.SHELL_SO_NAME);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(entryFile));
BufferedInputStream bufferedInputStream = new BufferedInputStream(zip.getInputStream(entry));
while ((len = bufferedInputStream.read(buf)) != -1) {
bufferedOutputStream.write(buf, 0, len);
if(localFileCrc != entry.getCrc()) {
byte[] buf = new byte[4096];
int len = -1;

FileOutputStream fileOutputStream = new FileOutputStream(entryFile);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
BufferedInputStream bufferedInputStream = new BufferedInputStream(zip.getInputStream(entry));
while ((len = bufferedInputStream.read(buf)) != -1) {
bufferedOutputStream.write(buf, 0, len);
}
Log.d(TAG, "unzip '" + entry.getName() + "' success. local = " + localFileCrc + ", zip = " + entry.getCrc());

FileUtils.close(bufferedOutputStream);
}
else {
Log.w(TAG, "no need unzip");
}
FileUtils.close(bufferedOutputStream);
break;
}
}
Expand Down

0 comments on commit 595eb77

Please sign in to comment.