android-async-http下载zip文件存储或解压

首先Android-async-http下载返回的是byte[],首先要把它转换为ZipInputStream,转换之后可以实现存储为zip文件或者直接解压
1、直接存储为zip文件
首先在AndroidMainfest.xml文件添加权限:

 <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

在MainActivity.java文件:

package com.example.chengvi.download;

import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import cz.msebera.android.httpclient.Header;

public class MainActivity extends AppCompatActivity {
    private ZipEntry zipEntry = null;
    private ZipInputStream zipIn = null;
    private FileOutputStream fileOut = null;
    private int readedBytes = 0;
    private int total = 0;
    private String folderPath = "D:\\Vivien\\android\\bookdownload";
    String url = "http://192.168.8.2:8088/download/biancheng.zip";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AsyncHttpClient client = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("name", "java");
        client.get(url, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                InputStream sbs = new ByteArrayInputStream(responseBody);
                zipIn = new ZipInputStream(sbs);
                try {
                    File path = Environment.getExternalStoragePublicDirectory(
                            Environment.DIRECTORY_MOVIES);
                    Log.d("path", path + " ");
                    File file = new File(path, "/" + "book.zip");
                    FileOutputStream fos = new FileOutputStream(file);
                    if ((zipEntry = zipIn.getNextEntry()) != null) {
                        int count;
                        byte data[] = new byte[1024];
                        while ((count = zipIn.read(data)) > 0) {
                            fos.write(data);
                        }
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }


            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                Log.d("fail", "fail");

            }
        });
    }
}

运行之后在logcat中Log初path为:
D/path: /storage/emulated/0/Movies
在模拟器上打开Movies目录就可以看到book.zip

2、解压为html(因为压缩文件里面是html,文件格式可以自己根据情况改)

package com.example.chengvi.download;

import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import cz.msebera.android.httpclient.Header;

public class MainActivity extends AppCompatActivity {
    private ZipEntry zipEntry = null;
    private ZipInputStream zipIn = null;
    private FileOutputStream fileOut = null;
    private int readedBytes = 0;
    private int total = 0;
    private String folderPath = "D:\\Vivien\\android\\bookdownload";
    String url = "http://192.168.8.2:8088/download/biancheng.zip";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AsyncHttpClient client = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("name", "java");
        client.get(url, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                InputStream sbs = new ByteArrayInputStream(responseBody);
                zipIn=new ZipInputStream(sbs);
                try {
                    File path = Environment.getExternalStoragePublicDirectory(
                            Environment.DIRECTORY_MOVIES);
                    Log.d("path",path+" ");
                    File file = new File(path, "/" + "book.html");
                    FileOutputStream fos=new FileOutputStream(file);
                    while((zipEntry=zipIn.getNextEntry())!=null){
                        String entryName=zipEntry.getName();
                        if(zipEntry.isDirectory()){

                            entryName = entryName.substring(0, entryName.length() - 1);
                            File folder = new File(folderPath + File.separator+ entryName);
                            folder.mkdirs();
                        } else {
                            String fileName=folderPath + File.separator + entryName;
                            file.createNewFile();
                            fileOut = new FileOutputStream(file);
                            byte data[]=new byte[1024];
                            while ((readedBytes = zipIn.read(data)) > 0) {
                                fileOut.write(data, 0, readedBytes);
                                total+=readedBytes;
                            }
                            fileOut.close();
                        }


                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }


            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                Log.d("fail", "fail");

            }
        });
    }
}

同样在Movies目录下就可以看到book.html文件

你可能感兴趣的:(Android)