Download and Extract Zip File in Android

Android 10 (Android Q): How to unzip file due to External Storage Changes (without android:requestLegacyExternalStorage)

Solved the problem by changing Environment.getExternalStorageDirectory() to getExternalFilesDir(null).getAbsolutePath()

android unzip folder from zip file and read content from that folder

private void unzip(String src, String dest){

final int BUFFER_SIZE = 4096;

BufferedOutputStream bufferedOutputStream = null;
FileInputStream fileInputStream;
try {
fileInputStream = new FileInputStream(src);
ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(fileInputStream));
ZipEntry zipEntry;

while ((zipEntry = zipInputStream.getNextEntry()) != null){

String zipEntryName = zipEntry.getName();

String name = dest.substring(dest.lastIndexOf("/")-1);

File FileName = new File(FolderName);
if (!FileName.isDirectory()) {
try {
if (FileName.mkdir()) {
} else {
}
} catch (Exception e) {
e.printStackTrace();
}
}

File file = new File(FolderName+"/" +zipEntryName);

if (file.exists()){

} else {
if(zipEntry.isDirectory()){
file.mkdirs();
}else{
byte buffer[] = new byte[BUFFER_SIZE];
FileOutputStream fileOutputStream = new FileOutputStream(file);
bufferedOutputStream = new BufferedOutputStream(fileOutputStream, BUFFER_SIZE);
int count;

while ((count = zipInputStream.read(buffer, 0, BUFFER_SIZE)) != -1) {
bufferedOutputStream.write(buffer, 0, count);
}

bufferedOutputStream.flush();
bufferedOutputStream.close();
}
}
}
zipInputStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Try This code it's working for me.

How to download zip file with java code in android correctly?

When you read into the buffer, you are writing the entire buffer to the output stream:

outputStream.write(buffer);

You should only write the part of the buffer that was filled:

outputStream.write(buffer, 0, readNum);

Particularly with network downloads, there is no guarantee that a call to inputStream.read(buffer) will fill the buffer (even if there is more than 1024 bytes to go in the file).

Download zip file using download manager

Yes you can use it, here is a small snippet:

DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request req = new DownloadManager.Request(Uri.parse("url-of-the-zip"));
req.setDestinationExternalFilesDir(Environment.DIRECTORY_DOWNLOADS, "filename.zip");
long id = dm.enqueue(req);

The id can later be used to request the local Uri of the downloaded file using DownloadManager.getUriForDownloadedFile(int). To unzip this file you can use ZipFile

How to download and extract a ZIP file in Java

I was looking into the same thing not too long ago, have a look at zip4j. I found this answer from another poster here.

public static void unzip(){
String source = "some/compressed/file.zip";
String destination = "some/destination/folder";
String password = "password";

try {
ZipFile zipFile = new ZipFile(source);
if (zipFile.isEncrypted()) {
zipFile.setPassword(password);
}
zipFile.extractAll(destination);
} catch (ZipException e) {
e.printStackTrace();
}
}


Related Topics



Leave a reply



Submit