2013-01-23 175 views
-1

我有一個問題,解壓縮從服務器接收到的文件。我發送請求和服務器顯示我的代碼字符的文本:S從服務器解壓縮文件http

有了這個代碼,我收到來自服務器拉鍊,但我需要在EdText解壓的這個文本文件,並顯示。

public void postLoginData() { 

    // Create a new HttpClient and Post Header 
    HttpClient httpclient = new DefaultHttpClient(); 


    HttpPost httppost = new HttpPost(
      "http://localhost:80/MOBILE/MBSERVER.V25?"); 
    try { 
     String VENDED_VEN = "2", EMPRES_CFG = "1", VERSAO_CFG = "100", function = "GetMARCAS", REQUERY = null; 

     comando = (EditText) findViewById(R.id.comando); 
     // String PLS = comando.getText().toString(); 

     List<NameValuePair> values = new ArrayList<NameValuePair>(6); 

     values = new ArrayList<NameValuePair>(); 
     values.add(new BasicNameValuePair("EMPRES", EMPRES_CFG)); 
     values.add(new BasicNameValuePair("USUARI", VENDED_VEN)); 
     values.add(new BasicNameValuePair("VERSAO", VERSAO_CFG)); 
     values.add(new BasicNameValuePair("ORIGEM", "AD")); 
     values.add(new BasicNameValuePair("FUNCAO", function)); 
     values.add(new BasicNameValuePair("RQUERY", REQUERY)); 


     httppost.setEntity(new UrlEncodedFormEntity(values)); 
     // Execute HTTP Post Request 
     Log.w("LOG", "Execute HTTP Post Request"); 
     HttpResponse response = httpclient.execute(httppost); 
     Log.w("LOG", "VALUES:"+values); 

     String str = inputStreamToString(response.getEntity().getContent()) 
       .toString(); 



     Log.w("LOG", str); 
     if (str.toString().equalsIgnoreCase("true")) { 
      Log.w("LOG", "TRUE"); 
      result.setText("Login successful"); 

     } else { 
      Log.w("LOG", "FALSE"); 
      result.setText(str); 
     } 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

private StringBuilder inputStreamToString(InputStream is) { 
    String line = ""; 
    StringBuilder total = new StringBuilder(); 

    // Wrap a BufferedReader around the InputStream 
    BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 
    // Read response until the end 
    try { 
     while ((line = rd.readLine()) != null) { 
      total.append(line); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    // Return full string 
    return total; 
} 

@Override 
public void onClick(View view) { 
    if (view == ok) { 
     postLoginData(); 
    } 
} 
}  

回答

1

如果是標準zip文件,可以使用java.util.zip包。

這裏解壓,在它有一個文件夾歸檔,並將其寫入到一個文件中的一個例子。

FileInputStream zipInputStream = new FileInputStream(new File(cacheDir, zipFileName)); 
FileOutputStream datOutputStream = new FileOutputStream(new File(cacheDir, datFileName)); 

// unzip and process ZIP file 
ZipInputStream zis = new ZipInputStream(zipInputStream); 
ZipEntry ze = null; 
// loop through archive 
while ((ze = zis.getNextEntry()) != null) { 
    if (ze.getName().toString().equals("myfolder/myfile.dat")) { // change this to whatever your folder/file is named inside the archive 
     while ((bufferLength = zis.read(buffer, 0, 1023)) != -1) { 
      datOutputStream.write(buffer, 0, bufferLength); 
     } 
    } 
    zis.closeEntry(); 
} 
+0

我在接收的字符串 字符串str = inputStreamToString(response.getEntity()的getContent()。)的toString(); 像這樣:2¤¿Öïé·噸餘; 2英寸±k½ý¿¿〜OI÷WïB¿üðÓïÿúËïTHO???? hehehehehehe 我可以通過zip得到這個檔案嗎?怎麼樣? 如何將它保存在設備中? Thx! :) –

+0

我應該指定。只需將您的輸入流輸入到您的temp/cache目錄下的文件輸出流即可。然後使用zip輸入流讀取它,然後您可以將該文件寫出來,或者做任何您想要的文件。你不能只用一個字符串來完成。你必須寫入磁盤。 – jlindenbaum