Unzip file from server http

那年仲夏 提交于 2019-12-25 17:08:04

问题


I have a problem to unzip a file received from server. I send a request, and server show me a text with code characters :S

With this code i receive a zip from server, but i need this text file unziped, and show in a 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:


If it's a standard zip file, you can use the java.util.zip package.

Here's an example of unzipping an archive that has a folder in it, and writing it to a file.

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();
}


来源:https://stackoverflow.com/questions/14485173/unzip-file-from-server-http

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!