2013-07-16 25 views
1

我創建了3個xml文件並壓縮到一個zip文件夾中。該文件夾是從服務器發送的。當我通過瀏覽器下載zip文件夾時,其工作正常並可以提取文件。但是,當我從android應用程序下載它並存儲在SD卡中時,它已損壞。我把文件從SD卡拉到電腦並試圖解壓文件夾,它顯示Zip文件夾無效。我下面的代碼給出:android:Zip文件夾無效

DefaultHttpClient httpclient1 = new DefaultHttpClient(); 
       HttpPost httpPostRequest = new HttpPost(
         Configuration.URL_FEED_UPDATE); 
          byte[] responseByte = httpclient1.execute(httpPostRequest, 
         new BasicResponseHandler()).getBytes(); 

       InputStream is = new ByteArrayInputStream(responseByte); 


       // --------------------------------------------------- 

       File file1 = new File(Environment 
     .getExternalStorageDirectory() + "/ast"); 
       file1.mkdirs(); 
       // 
       File outputFile = new File(file1, "ast.zip"); 
       FileOutputStream fos = new FileOutputStream(outputFile); 

       byte[] buffer = new byte[1024]; 
       int len1 = 0; 
       while ((len1 = is.read(buffer)) != -1) { 
        fos.write(buffer, 0, len1); 
       } 
       fos.close();     
       is.close(); 

當我用

ZipInputStream zin = new ZipInputStream(new BufferedInputStream(is)); 

ZipInputStream不能從流存儲值。

回答

1

我猜你的主要錯誤是你得到輸入流的地方。你實際做的是將服務器響應作爲String(BasicResponseHandler),然後再將其轉換爲字節。由於Java都是UTF-8,這很可能不起作用。

最好嘗試像

HttpResponse response = httpclient1.execute(httpPostRequest); 
InputStream is = response.getEntity().getContent() 

(而且做得更好空指針檢查,看在try-catch塊的內容,並確保您關閉所有資源在finally塊)。