您好,我已设法下载文件。现在我正在使用来自
http://www.jondev.net/articles/Unzipping_Files_with_Android_%28Programmatically%29
所以,我想要一个关于我应该在哪里实现这个类的建议。还有有什么方法可以在解压后删除 zip 文件?谢谢你。
这是我的主要代码
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();
这是我实施解压缩过程的正确方法吗?
我真的是android的新手。任何形式的帮助将不胜感激。
编辑 - 在 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);
}
}
然而,这只是课程。我仍然在 onCreate 中调用它。