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

}

转载于:https://www.cnblogs.com/linyu-03-28/p/3658532.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值