gpt4 book ai didi

android - 下载后实现解压缩

转载 作者:搜寻专家 更新时间:2023-11-01 09:04:54 25 4
gpt4 key购买 nike

您好,我已经成功下载了一个文件。现在我正在使用

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 新手。任何形式的帮助将不胜感激。

编辑 - 在异步任务中解压

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 中调用它。

最佳答案

一些伪代码来重复已回答的内容:

onCreate()
{
buttonOnClick
{
DownloadAndUnzip.execute(the url)
}
}

inner class DownloadAndUnzip()
{
boolean failed;

preExecute()
{
failed = false;
}

doInBackground()
{
try
{
start download stream
}
catch
{
failed = true
}
finally
{
close streams
}

if failed == false
{
try
{
decompress(zip file)
}
catch
{
failed = true
}
finally
{
close streams
}
}
}

postExecute()
{
if failed
{
download failed

notify user
}
else
{
download and unzip is good

zip.delete
}
}
}

关于android - 下载后实现解压缩,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12724275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com