Hello everyone.
Ask this question so that they could give me a hand and guide me on my way.
My problem
I want to be able to unzip a zip to a folder or directory on the sdcard, but my code hasn't hit the mark. Its flaw is that it does not unzip or copy any of the files inside the zip located in the Assets resource folder.
My code
private boolean copyFile1(String filename1, String outPath1) {
AssetManager assetManager = this.getAssets();
final int CHUNK_SIZE = 1024 * 4;
InputStream in;
OutputStream out;
try {
in = assetManager.open(filename1);
String newFileName = outPath1;
ZipInputStream zipStream = new ZipInputStream(in);
ZipEntry zEntry = null;
while ((zEntry = zipStream.getNextEntry()) != null) {
if (zEntry.isDirectory()) {
} else {
FileOutputStream fout = new FileOutputStream(new File(outPath1));
BufferedOutputStream bufout = new BufferedOutputStream(fout);
byte[] buffer = new byte[CHUNK_SIZE];
int read = 0;
while ((read = zipStream.read(buffer)) != -1) {
bufout.write(buffer, 0, read);
}
zipStream.closeEntry();
bufout.close();
fout.close();
}
}
zipStream.close();
Log.d("Unzip", "Unzipping complete. path : " );
} catch (Exception e) {
Log.e("TAG", e.getMessage());
}
return true;
}
If you notice where I fail or know another way. Please let me know. Thank you
If you have the permission defined and you use android 6+ in addition to adding the permission to your
AndroidManifest.xml
:You must require permissions manually by:
I was asking about the paths that you send to your method, the paths of the files must be defined within the external storage, for example:
Copy contents of a .zip to a directory in android.
For this you have to decompress and add this class that will be useful:
To use it, it would be like this:
It may be a permissions error. Check that you have included the following line in your AndroidManifest.xml:
On the other hand, I don't know how you pass the path to your method, but you should do it in a path whose base is obtained like this: