Hello I have manage to download a file. And now I am using the class from
http://www.jondev.net/articles/Unzipping_Files_with_Android_%28Programmatically%29
So, I would like an advice as to where should I implement this class. And also is there any way to delete the zip file after the decompression? Thank you.
This is my main code
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.xml);
btn_src = (Button) findViewById(R.id.source);
btn_src.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String link;
link = resurl + "9_1342080926-1.0.zip";
downloadRes = new downloadRes();
downloadRes.execute(link);
}
});
String zipFile = Environment.getExternalStorageDirectory() +
"/aiyo/aiyomag/edition/9_1342080926-1.0.zip";
String unzipLocation = Environment.getExternalStorageDirectory() +
"/aiyo/aiyomag/edition/sourcetest";
Decompress d = new Decompress(zipFile, unzipLocation);
d.unzip();
Is that the right way for me to implement the unzipping process?
I am really new in android. Any kind of help would be appreciated.
EDIT - UNZIP IN ASYNCTASK
public class downloadRes extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(DIALOG_DOWNLOAD_PROGRESS);
}
@Override
protected String doInBackground(String... params) {
try {
File root = android.os.Environment
.getExternalStorageDirectory();
File dir = new File(root.getAbsolutePath() + "/aiyo/aiyomag/edition/sourcetest");
if (dir.exists() == false) {
dir.mkdirs();
}
Log.d("param", params[0]);
URL url = new URL(params[0]); // you can write here any link
URLConnection connection = url.openConnection();
connection.connect();
// get file name and file extension
String fileExtenstion = MimeTypeMap
.getFileExtensionFromUrl(params[0]);
String name = URLUtil.guessFileName(params[0], null,
fileExtenstion);
File file = new File(dir, name);
Log.d("File in content","The file is "+file.getName());
/*
* Define InputStreams to read from the URLConnection.
*/
InputStream is = connection.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
OutputStream fos = new FileOutputStream(file);
/*
* Read bytes to the Buffer until there is nothing more to
* read(-1).
*/
int lenghtOfFile = connection.getContentLength();
int total = 0;
byte baf[] = new byte[1024];
int current = 0;
while ((current = bis.read(baf)) != -1) {
total += current;
// publishProgress("" + (int) ((total * 100) /
// lenghtOfFile));
mProgressDialog.setProgress(((total * 100) / lenghtOfFile));
fos.write(baf, 0, current);
}
// close every file stream
fos.flush();
fos.close();
is.close();
} catch (IOException e) {
Log.e("DownloadManager", "Error: " + e);
}
return null;
}
@Override
protected void onProgressUpdate(String... values) {
mProgressDialog.setProgress(Integer.parseInt(values[0]));
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
// if (fileInteger == max) {
// dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
// return;
// }
Log.d("post execute", "i::" + fileInteger);
// fileInteger++;
// publishProgress("" + (int) ((fileInteger * 100) / max));
// mProgressDialog.setSecondaryProgress(((fileInteger * 100) / max));
String link = resurl+"9_1342080926-1.0.zip";
downloadRes = new downloadRes();
downloadRes.execute(link);
}
}
That is just the class however. And I still call it in the onCreate.
I suggest that you execute the download and decompression in an AsyncTask. This is a good practice and avoids freezing your Activity or GUI when the process takes a while. Starting from ICS (or earlier maybe) you can not execute webrequests in your Activity code and an Async approach gets mandatory. If you don't, the webrequest or download will fail.
EDIT : This might be useful link tutorial.