Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Java code to unzip Flutter assets
private static void unzipLibs(AssetManager am, String nativeLibraryDir) {
try {
File complete = Paths.get(appDir, "mono_libs_complete").toFile();
if(complete.exists() && !BuildConfig.DEBUG) {
Log.e(TAG, "Libs have already been unzipped to data directory, skipping unzip...");
return;
}
String baseDir = Paths.get(appDir, "app_flutter").toString();
byte[] buffer = new byte[BUFFER];
int count;
FileOutputStream fout;
Log.e(TAG, "Fetching libfile from assets.");
InputStream is = am.open(libFile);
Log.e(TAG, "Creating zip input stream.");
ZipInputStream zin = new ZipInputStream(is);
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
Log.e(TAG, "Extracting " + ze.getName());
Path dest = Paths.get(baseDir, ze.getName());
if (ze.isDirectory()) {
if (!Files.exists(dest))
Files.createDirectories(dest);
} else {
fout = new FileOutputStream(dest.toString());
while((count = zin.read(buffer, 0, BUFFER)) != -1) { fout.write(buffer, 0, count); }
zin.closeEntry();
fout.close();
}
}
zin.close();
is.close();
is = new FileInputStream(Paths.get(nativeLibraryDir, "libmonosgen-2.0.so").toString());
fout = new FileOutputStream(Paths.get(baseDir, "lib", "libmonosgen-2.0.so").toString());
while((count = is.read(buffer)) != -1){
fout.write(buffer, 0, count);
}
is.close();
fout.close();
is = new FileInputStream(Paths.get(nativeLibraryDir, "libmono-native.so").toString());
fout = new FileOutputStream(Paths.get(baseDir, "lib", "libmono-native.so").toString());
while((count = is.read(buffer)) != -1){
fout.write(buffer, 0, count);
}
is.close();
fout.close();
complete.createNewFile();
Log.e(TAG, "Completed unzipping libs.");
} catch(IOException e) {
Log.e(TAG, "Error unzipping libs : " + e.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.