下载后实现解压缩

时间:2012-10-04 09:29:50

标签: android download unzip compression

您好我已经设法下载文件了。现在我正在使用

中的类

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的UNZIP

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

2 个答案:

答案 0 :(得分:2)

一些伪代码重申已经回答的内容:

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
        }
    }
}

答案 1 :(得分:1)

我建议您在AsyncTask中执行下载和解压缩。 这是一个很好的做法,避免在进程需要一段时间后冻结您的Activity或GUI。从ICS(或更早版本)开始,您无法在Activity代码中执行webrequests,并且必须使用Async方法。如果不这样做,webrequest或下载将失败。

编辑:这可能有用link tutorial