前面的android-async-http下载zip文件存储或解压有个问题:当我下载的zip文件压缩的只是一个文档时解压或者存储都正常,但是如果我的zip文件含有很多子目录或者有多层子目录时,得到的zip文件大小会变成0字节,具体原因大概是因为read()方法只正对非压缩文件有效,有时间再去深究,这里先贴出正确的下载并存储同时解压的代码
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.BufferedInputStream;
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.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import cz.msebera.android.httpclient.Header;
public class MainActivity extends AppCompatActivity {
String url = "http://192.168.8.2:8088/download/www.zip";
private ZipEntry zipEntry = null;
private ZipInputStream zipIn = null;
private FileOutputStream fileOut = null;
private int readedBytes = 0;
private int total = 0;
@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) {
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = null;
String filename = "zip.zip";
File zip_path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_MOVIES);
try {
file = new File(zip_path.toString() + "/" + filename);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(responseBody);
} catch (IOException e) {
e.printStackTrace();
}
File zipFile = new File(zip_path + "/" + filename);
String unzip_path = zip_path + "/unzip/";
File file_unzip_path = new File(unzip_path);
if (!file_unzip_path.exists()) {
file_unzip_path.mkdir();
}
try {
upZip(zipFile,unzip_path);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
Log.d("fail", "fail");
}
});
}
public int upZip(File zipFile, String folderPath)throws ZipException,IOException {
//public static void upZipFile() throws Exception{
ZipFile zfile=new ZipFile(zipFile);
Enumeration zList=zfile.entries();
ZipEntry ze=null;
byte[] buf=new byte[1024];
while(zList.hasMoreElements()){
ze=(ZipEntry)zList.nextElement();
if(ze.isDirectory()){
Log.d("upZipFile", "ze.getName() = "+ze.getName());
String dirstr = folderPath + ze.getName();
//dirstr.trim();
dirstr = new String(dirstr.getBytes("8859_1"), "GB2312");
Log.d("upZipFile", "str = "+dirstr);
File f=new File(dirstr);
f.mkdir();
continue;
}
Log.d("upZipFile", "ze.getName() = "+ze.getName());
OutputStream os=new BufferedOutputStream(new FileOutputStream(getRealFileName(folderPath, ze.getName())));
InputStream is=new BufferedInputStream(zfile.getInputStream(ze));
int readLen=0;
while ((readLen=is.read(buf, 0, 1024))!=-1) {
os.write(buf, 0, readLen);
}
is.close();
os.close();
}
zfile.close();
return 0;
}
public static File getRealFileName(String baseDir, String absFileName){
String[] dirs=absFileName.split("/");
File ret=new File(baseDir);
String substr = null;
if(dirs.length>1){
for (int i = 0; i < dirs.length-1;i++) {
substr = dirs[i];
try {
//substr.trim();
substr = new String(substr.getBytes("8859_1"), "GB2312");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ret=new File(ret, substr);
}
Log.d("upZipFile", "1ret = "+ret);
if(!ret.exists())
ret.mkdirs();
substr = dirs[dirs.length-1];
try {
//substr.trim();
substr = new String(substr.getBytes("8859_1"), "GB2312");
Log.d("upZipFile", "substr = "+substr);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ret=new File(ret, substr);
Log.d("upZipFile", "2ret = "+ret);
return ret;
}
return ret;
}
}