2012-04-11 42 views
2

我尝试下载一个zip文件到android,然后解压zip文件。我调试我的代码,并有一个有趣的问题:下载的zip文件比原始文件大一点。并且下载的zip文件不能被winrar解压缩。据说下载的文件以错误结束。 (。上我的网站的zip文件是好的,我尝试用IE下载它工作正常) 以下是我的代码:如何在android中正确地下载带有java代码的zip文件?

public void download(final String url, final String savePath, final String saveName) { 
    new Thread(new Runnable() { 
     public void run() { 
      try { 
       sendMessage(FILE_DOWNLOAD_CONNECT); 
       URL sourceUrl = new URL(url); 
       URLConnection conn = sourceUrl.openConnection(); 
       conn.connect(); 
       InputStream inputStream = conn.getInputStream(); 

       int fileSize = conn.getContentLength(); 

       File savefilepath = new File(savePath); 
       if (!savefilepath.exists()) { 
        savefilepath.mkdirs(); 
       } 
       File savefile = new File(savePath+saveName); 
       if (savefile.exists()) { 
        savefile.delete(); 
       } 
       savefile.createNewFile(); 

       FileOutputStream outputStream = new FileOutputStream(
        savePath+saveName, true); 

       byte[] buffer = new byte[1024]; 
       int readCount = 0; 
       int readNum = 0; 
       int prevPercent = 0; 
       while (readCount < fileSize && readNum != -1) { 
        readNum = inputStream.read(buffer); 
        if (readNum > -1) { 
         outputStream.write(buffer); 

         readCount = readCount + readNum; 

         int percent = (int) (readCount * 100/fileSize); 
         if (percent > prevPercent) { 
          sendMessage(FILE_DOWNLOAD_UPDATE, percent, 
           readCount); 

          prevPercent = percent; 
         } 
        } 
       } 
       outputStream.flush(); 
       outputStream.close(); 
       inputStream.close(); 
       //Thread.sleep(50); 
       sendMessage(FILE_DOWNLOAD_COMPLETE, savePath); 

      } catch (Exception e) { 
       sendMessage(FILE_DOWNLOAD_ERROR, e); 
      } 
     } 
    }).start(); 
} 

有人知道这个问题?

+0

我曾尝试之前讨论的解决方案:[链接](http://stackoverflow.com/questions/9324103/download-and-extract-zip-file-in-android)。但同样的问题在那里。 – David 2012-04-11 05:23:44

+0

另一个有趣的问题。下载后的文件大小会在我再次下载后发生变化(不是每次都是这样)我想这可能是由android文件缓冲策略造成的。所以我尝试//Thread.sleep(50);让android完全保存文件。但它似乎不起作用。 (zip文件需要在下载后立即提取。) – David 2012-04-11 05:27:51

回答

5

当你读入缓冲区,你正在写的整个缓冲区输出流:

outputStream.write(buffer); 

你应该只写了心中缓冲区的部分:

outputStream.write(buffer, 0, readNum); 

特别对于网络下载,不能保证对inputStream.read(buffer)的调用将填充缓冲区(即使文件中有超过1024个字节)。

+0

嗨,泰德。你是对的!它工作正常。我应该考虑一下。我很愚蠢。:) – David 2012-04-11 05:43:41

2
public class Download extends Activity { 
public static final int DIALOG_DOWNLOAD_PROGRESS = 0; 

private ProgressDialog mProgressDialog; 
    public String app_name ; 
    public String urlpath ; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 



app_name="test.rar";   
    urlpath = "https://www.abc.com/download/"+ app_name; 
    if (android.os.Environment.getExternalStorageState().equals 
      (android.os.Environment.MEDIA_MOUNTED)) 
    {   
     startDownload(); 
    } 

    else 
    { 
     Toast.makeText(getApplicationContext(), "SD Card not found,Insert SD card and try again", Toast.LENGTH_SHORT).show(); 
    } 

} 

private void startDownload() { 

    new DownloadFileAsync().execute(urlpath); 
} 
@Override 
protected Dialog onCreateDialog(int id) { 
    switch (id) { 
     case DIALOG_DOWNLOAD_PROGRESS: 
      mProgressDialog = new ProgressDialog(this); 
      mProgressDialog.setMessage("Downloading Updates.."); 
      mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
      mProgressDialog.setCancelable(false); 
      mProgressDialog.setIndeterminate(false); 
      mProgressDialog.setMax(100); 
      mProgressDialog.show(); 
      return mProgressDialog; 
     default: 
      return null; 
    } 
} 
class DownloadFileAsync extends AsyncTask<String, String, String> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     showDialog(DIALOG_DOWNLOAD_PROGRESS); 
    } 

    @Override 
    protected String doInBackground(String... aurl) { 


     try { 


      URL url = new URL(urlpath.toString()); // Your given URL. 

      HttpURLConnection c = (HttpURLConnection) url.openConnection(); 
      c.setRequestMethod("GET"); 
      c.setDoOutput(true); 
      c.connect(); // Connection Complete here.! 


      int lenghtOfFile = c.getContentLength(); 


      Log.d("Downloading Updates", "Lenght of file: " + lenghtOfFile); 

      String PATH = Environment.getExternalStorageDirectory() + "/download/"; 

      File file = new File(PATH); // PATH = /mnt/sdcard/download/ 

      if (!file.exists()) { 
       file.mkdirs(); 
      } 
      File outputFile = new File(file, app_name);   
      FileOutputStream fos = new FileOutputStream(outputFile); 



      InputStream is = c.getInputStream();/

      byte[] buffer = new byte[1024]; 
      int len1 = 0; 
      long total = 0; 
      while ((len1 = is.read(buffer)) != -1) { 
       total += len1; 
       publishProgress(""+(int)((total*100)/lenghtOfFile)); 

       fos.write(buffer, 0, len1); // Write In FileOutputStream. 
      } 
      fos.flush(); 
      fos.close(); 
      is.close(); 
     } catch (Exception e) {} 
     return null; 

    } 
    protected void onProgressUpdate(String... progress) { 
     Log.d("Downloading Updates",progress[0]); 
     mProgressDialog.setProgress(Integer.parseInt(progress[0])); 
    } 

    @Override 
    protected void onPostExecute(String unused) { 
     dismissDialog(DIALOG_DOWNLOAD_PROGRESS); 
     finish(); 
    } 
} 
} 
+0

谢谢Deepsan。 – David 2012-04-12 01:01:50

+0

它适用于zip或rar吗? – albert 2016-10-20 05:28:09