2011-09-10 130 views
0

我正在下載一些.zip文件,雖然當我嘗試解壓縮它們時,我得到「數據錯誤」。現在我去看到下載的文件,並且它們比原版的。這可能是錯誤的原因嗎?文件下載代碼下載文件比原來大

代碼下載文件:

URL=intent.getStringExtra("DownloadService_URL"); 
    FileName=intent.getStringExtra("DownloadService_FILENAME"); 
    Path=intent.getStringExtra("DownloadService_PATH"); 

    try{ 


    URL url = new URL(URL); 
    URLConnection conexion = url.openConnection(); 

    conexion.connect(); 
    int lenghtOfFile = conexion.getContentLength(); 
    lenghtOfFile/=100; 

    InputStream input = new BufferedInputStream(url.openStream()); 
    OutputStream output = new FileOutputStream(Path+FileName); 

    byte data[] = new byte[1024]; 
    long total = 0; 

    int count = 0; 
    while ((count = input.read(data)) != -1) { 
     output.write(data); 
     total += count; 

     notification.setLatestEventInfo(context, contentTitle, "Starting download " + FileName + " " + (total/lenghtOfFile), contentIntent); 
     mNotificationManager.notify(1, notification); 
    } 


    output.flush(); 
    output.close(); 
    input.close(); 

代碼解壓:

try { 

      String zipFile = Path + FileName; 


      FileInputStream fin = new FileInputStream(zipFile); 
      ZipInputStream zin = new ZipInputStream(fin); 

      ZipEntry ze = null; 
      while ((ze = zin.getNextEntry()) != null) { 
       UnzipCounter++; 
       if (ze.isDirectory()) { 
        dirChecker(ze.getName()); 
       } else { 
        FileOutputStream fout = new FileOutputStream(Path 
          + ze.getName()); 
        while ((Unziplength = zin.read(Unzipbuffer)) > 0) { 
         fout.write(Unzipbuffer, 0, Unziplength);      
        } 
        zin.closeEntry(); 
        fout.close(); 

       } 

      } 
      zin.close(); 
      File f = new File(zipFile); 
      f.delete(); 
      notification.setLatestEventInfo(context, contentTitle, "File successfully downloaded", contentIntent); 
      mNotificationManager.notify(1, notification); 

     } catch (Exception e) { 

      notification.setLatestEventInfo(context, contentTitle, "Problem in downloading file ", contentIntent); 
      mNotificationManager.notify(1, notification); 

     } 

    } 

的解壓proccess啓動,但與錯誤提取一些文件後停止。我試圖anothe [R .zip文件我得到了CRC失敗的錯誤..我測試了兩個.zip文件與winrar ..

原始文件大小:3.67mb ..下載文件大小:3.93mb

回答

1

,你老是寫完整的字節數組到磁盤不檢查你有多少數據讀取

而且從視野中的任何性能上來說< 1500byte(即通常的以太網MTU)是一個非常糟糕的主意 - 儘管我無論如何,都認爲Java緩衝在某個地方,但爲什麼會冒險。

+0

你能告訴我如何解決這些問題 – Omar

+1

用'output.write(data,0,count)'替換'output.write(data)'。 –