Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
152 views
in Technique[技术] by (71.8m points)

android - Unzip file from server http

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();
    }
}
}    
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...