Android下载压缩文件与解压案例

时间:2024-01-09 11:32:26

ackage com.example.jsontest.biz;

import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream;

import android.content.Context;

public class DownFile {  public static File downLoadFile(Context context,String url) throws MalformedURLException, Exception{   File file=new File(context.getExternalCacheDir()+"downzip.zip");   FileOutputStream out=new FileOutputStream(context.getExternalCacheDir()+"downzip.zip");   URL urlIn=new URL(url);   URLConnection conn=urlIn.openConnection();   BufferedInputStream in=new BufferedInputStream(conn.getInputStream());   byte[] bytes=new byte[1024];   while(in.read(bytes)!=-1){    out.write(bytes);   }   out.flush();   out.close();   return file;  }    public File Unzip (Context context,File zipFile){   File file=new File(context.getExternalCacheDir()+"downzip.zip");            try {                  BufferedInputStream bis = new BufferedInputStream(new FileInputStream(zipFile));                 ZipInputStream zis = new ZipInputStream(bis);                 BufferedOutputStream bos = null;                  ZipEntry entry = null;                 while ((entry=zis.getNextEntry()) != null) {                      //String entryName = entry.getName();                       bos = new BufferedOutputStream(new FileOutputStream(file));                       int b = 0;                      while ((b = zis.read()) != -1) {                            bos.write(b);                       }                      bos.flush();                      bos.close();                 }                  zis.close();              } catch (IOException e) {                             }             return file ;       }

}

public class JsonPaser {

public static void paser(File file) throws Exception {   StringBuffer stringBuffer = new StringBuffer();   String line = null;

BufferedReader br = new BufferedReader(new FileReader(file));   while ((line = br.readLine()) != null) {    stringBuffer.append(line);   }   // 将Json文件数据形成JSONObject对象   JSONObject jsonObject = new JSONObject(stringBuffer.toString());   // 获取JSONObject对象数据并打印   JSONArray provinces = jsonObject.getJSONArray("*********");  }

}