Skip to content

Instantly share code, notes, and snippets.

@dhavaln
Last active December 14, 2022 10:01
Show Gist options
  • Save dhavaln/7c7e3a95442a1a3e6af3 to your computer and use it in GitHub Desktop.
Save dhavaln/7c7e3a95442a1a3e6af3 to your computer and use it in GitHub Desktop.
Android Download and Unzip File
package com.jumpbyte.webserver;
import android.content.Context;
import android.util.Log;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/**
*
* @author dhaval nagar
*/
public class Decompress {
private File _zipFile;
private InputStream _zipFileStream;
private Context context;
private static final String ROOT_LOCATION = "/sdcard";
private static final String TAG = "UNZIPUTIL";
public Decompress(Context context, File zipFile) {
_zipFile = zipFile;
this.context = context;
_dirChecker("");
}
public Decompress(Context context, InputStream zipFile) {
_zipFileStream = zipFile;
this.context = context;
_dirChecker("");
}
public void unzip() {
try {
Log.i(TAG, "Starting to unzip");
InputStream fin = _zipFileStream;
if(fin == null) {
fin = new FileInputStream(_zipFile);
}
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
Log.v(TAG, "Unzipping " + ze.getName());
if(ze.isDirectory()) {
_dirChecker(ROOT_LOCATION + "/" + ze.getName());
} else {
FileOutputStream fout = new FileOutputStream(new File(ROOT_LOCATION, ze.getName()));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int count;
// reading and writing
while((count = zin.read(buffer)) != -1)
{
baos.write(buffer, 0, count);
byte[] bytes = baos.toByteArray();
fout.write(bytes);
baos.reset();
}
fout.close();
zin.closeEntry();
}
}
zin.close();
Log.i(TAG, "Finished unzip");
} catch(Exception e) {
Log.e(TAG, "Unzip Error", e);
}
}
private void _dirChecker(String dir) {
File f = new File(dir);
Log.i(TAG, "creating dir " + dir);
if(dir.length() >= 0 && !f.isDirectory() ) {
f.mkdirs();
}
}
}
package com.jumpbyte.webserver;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
/**
* Created by dhavalnagar on 03/02/15.
*/
public class DownloadFileAsync extends AsyncTask<String, String, String> {
private static final String TAG ="DOWNLOADFILE";
public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private PostDownload callback;
private Context context;
private FileDescriptor fd;
private File file;
private String downloadLocation;
public DownloadFileAsync(String downloadLocation, Context context, PostDownload callback){
this.context = context;
this.callback = callback;
this.downloadLocation = downloadLocation;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... aurl) {
int count;
try {
URL url = new URL(aurl[0]);
URLConnection connection = url.openConnection();
connection.connect();
int lenghtOfFile = connection.getContentLength();
Log.d(TAG, "Length of the file: " + lenghtOfFile);
InputStream input = new BufferedInputStream(url.openStream());
file = new File(downloadLocation);
FileOutputStream output = new FileOutputStream(file); //context.openFileOutput("content.zip", Context.MODE_PRIVATE);
Log.d(TAG, "file saved at " + file.getAbsolutePath());
fd = output.getFD();
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {}
return null;
}
protected void onProgressUpdate(String... progress) {
//Log.d(TAG,progress[0]);
}
@Override
protected void onPostExecute(String unused) {
if(callback != null) callback.downloadDone(file);
}
public static interface PostDownload{
void downloadDone(File fd);
}
}
@Override
public void onCreate(Bundle savedInstanceState){
Button downloadContent = (Button) findViewById(R.id.downloadContent);
downloadContent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
downloadAndUnzipContent();
}
});
}
private void downloadAndUnzipContent(){
String url = "https://github.com/NanoHttpd/nanohttpd/archive/master.zip";
DownloadFileAsync download = new DownloadFileAsync("/sdcard/content.zip", this, new DownloadFileAsync.PostDownload(){
@Override
public void downloadDone(File file) {
Log.i(TAG, "file download completed");
// check unzip file now
Decompress unzip = new Decompress(Home.this, file);
unzip.unzip();
Log.i(TAG, "file unzip completed");
}
});
download.execute(url);
}
@damilky
Copy link

damilky commented Oct 7, 2019

E/UNZIPUTIL: Unzip Error
java.io.FileNotFoundException: /nanohttpd-master/.gitignore (No such file or directory)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(FileOutputStream.java:308)
at java.io.FileOutputStream.(FileOutputStream.java:238)
at java.io.FileOutputStream.(FileOutputStream.java:180)
at com.example.ipscansearchcve.Decompress.unzip(Decompress.java:56)
at com.example.ipscansearchcve.MainActivity$1.downloadDone(MainActivity.java:72)
at com.example.ipscansearchcve.DownloadFileAsync.onPostExecute(DownloadFileAsync.java:81)
at com.example.ipscansearchcve.DownloadFileAsync.onPostExecute(DownloadFileAsync.java:21)
at android.os.AsyncTask.finish(AsyncTask.java:695)
at android.os.AsyncTask.access$600(AsyncTask.java:180)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

I have this errors...... plz help me.................

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment