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

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

}

Android下载压缩文件与解压案例,布布扣,bubuko.com

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

上一篇:想知道机器学习掌握的怎么样了吗?这有一份自测题(附答案和解析)


下一篇:统计常用知识随笔(1)