2

您好,我已设法下载文件。现在我正在使用来自

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

4

2 回答 2

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
        }
    }
}
于 2012-10-05T02:19:27.007 回答
1

我建议你在AsyncTask中执行下载和解压。这是一个很好的做法,可以避免在该过程需要一段时间时冻结您的 Activity 或 GUI。从 ICS(或更早)开始,您无法在 Activity 代码中执行 webrequest,并且必须使用 Async 方法。否则,webrequest 或下载将失败。

编辑:这可能是有用的链接教程

于 2012-10-04T10:04:13.163 回答