从服务器上解压缩文件,HTTP [英] Unzip file from server http

查看:138
本文介绍了从服务器上解压缩文件,HTTP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要解压缩从服务器接收到一个文件有问题。我发送一个请求,和服务器显示我code字符的文本:•

有了这个$ C C I接收来自服务器的zip,但我需要这个文本文件解压的,并显示在EdText。

 公共无效postLoginData(){    //创建一个新的HttpClient和邮政头
    HttpClient的HttpClient的=新DefaultHttpClient();
    HttpPost httppost =新HttpPost(
            HTTP://本地主机:80 / MOBILE / MBSERVER.V25?);
    尝试{
        字符串VENDED_VEN =2,EM preS_CFG =1,VERSAO_CFG =100,功能=GetMARCAS,REQUERY = NULL;        COMANDO =(EditText上)findViewById(R.id.comando);
        //字符串PLS = comando.getText()的toString()。        清单<&的NameValuePair GT;值=新的ArrayList<&的NameValuePair GT;(6);        值=新的ArrayList<&的NameValuePair GT;();
        values​​.add(新BasicNameValuePair(EM preS,EM preS_CFG));
        values​​.add(新BasicNameValuePair(USUARI,VENDED_VEN));
        values​​.add(新BasicNameValuePair(VERSAO,VERSAO_CFG));
        values​​.add(新BasicNameValuePair(ORIGEM,AD));
        values​​.add(新BasicNameValuePair(FUNCAO功能));
        values​​.add(新BasicNameValuePair(RQUERY,REQUERY));
        httppost.setEntity(新UrlEn codedFormEntity(值));
        //执行HTTP POST请求
        Log.w(日志,执行HTTP POST请求);
        HTT presponse响应= httpclient.execute(httppost);
        Log.w(日志,值:+值);        字符串str = inputStreamToString(response.getEntity()的getContent())
                的ToString();        Log.w(日志,STR);
        如果(str.toString()。equalsIgnoreCase(真)){
            Log.w(日志,TRUE);
            result.setText(登录成功);        }其他{
            Log.w(LOG,假);
            result.setText(STR);
        }
    }赶上(ClientProtocolException E){
        e.printStackTrace();
    }赶上(IOException异常五){
        e.printStackTrace();
    }
}私人的StringBuilder inputStreamToString(InputStream为){
    串线=;
    StringBuilder的总=新的StringBuilder();    //环绕式InputStream的一个BufferedReader
    RD的BufferedReader =新的BufferedReader(新的InputStreamReader(是));
    //读取响应,直到结束
    尝试{
        而((行= rd.readLine())!= NULL){
            total.append(线);
        }
    }赶上(IOException异常五){
        e.printStackTrace();
    }
    //返回满弦
    总回报;
}@覆盖
公共无效的onClick(查看视图){
    如果(查看== OK){
        postLoginData();
    }
}
}


解决方案

如果这是一个标准的zip文件,可以使用 java.util.zip 包。

下面是解压即中有一个文件夹归档,并将其写入到文件中的一个例子。

 的FileInputStream zipInputStream =新的FileInputStream(新文件(cacheDir,zipFileName));
FileOutputStream中datOutputStream =新的FileOutputStream(新文件(cacheDir,datFileName));//解压缩和处理ZIP文件
ZipInputStream ZIS =新ZipInputStream(zipInputStream);
ZipEntry的泽= NULL;
//通过循环存档
而((ZE = zis.getNextEntry())!= NULL){
    如果(ze.getName()。的toString()。等于(MyFolder中/ MYFILE.DAT)){//改变这一切你的文件夹/文件被命名为存档内
        而(量(bufferLength = zis.read(缓冲液,0,1023))!= - 1){
            datOutputStream.write(缓冲液,0,BufferLength中);
        }
     }
     zis.closeEntry();
}

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

解决方案

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

这篇关于从服务器上解压缩文件,HTTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆