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
Android Emulator Not Receiving Push Notifications
How to Detect When the Notification/System Bar Is Opened
Out of Memory Exception Due to Large Bitmap Size
Embed Activity Feed of a Public Facebook Page Without Forcing User to Login/Allow
Android 2.1 View's Getdrawingcache() Method Always Returns Null
Compatiblity of Material Design to Versions Below Android 5.0
Scrolling Edittext Inside Scrollview
Ask for Password Before Uninstalling Application
Extracting Information from a Scanned Gs1-Type Barcode
Moving or Copying Data from One Node to Another in Firebase Database
Out of Memory Error While Loading Bitmaps
How to Display Gif in React-Native Android App
How to Create/Write File in the Root of the Android Device