1. 程式人生 > >下載zip檔案並解壓

下載zip檔案並解壓

前面的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; } }