-1

我在解压缩从服务器收到的文件时遇到问题。我发送请求,服务器向我显示带有代码字符的文本:S

使用此代码,我从服务器收到一个 zip,但我需要解压缩此文本文件,并在 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();
    }
}
}    
4

1 回答 1

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();
}
于 2013-01-23T17:28:19.670 回答