最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android 不解壓直接讀取zip包的方法

 更新時間:2018年01月23日 09:50:53   作者:Mackkill  
下面小編就為大家分享一篇Android 不解壓直接讀取zip包的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

之前項目中遇到個需求,總監(jiān)讓我們把從服務器下載下來的資源不解壓直接讀取里面的資源,這樣的話就省去了一個個校驗資源是否正確的步驟,聽著貌似有點道理。。。廢話不多說直接上代碼。

目前我所試驗過的可以讀取的資源有文本、圖片、xml文件。

文本:

zip包目錄結(jié)構:res/txt/data.json

文件sd卡路徑:android.os.Environment.getExternalStorageDirectory() + “/res.zip”

public static String readDataFile(String file) throws Exception {
  //截取路徑的文件名 res
  String fileName = file.substring(file.length() - 9, file.length() - 4);
  ZipFile zf = new ZipFile(file);
  InputStream in = new BufferedInputStream(new FileInputStream(file));
  ZipInputStream zin = new ZipInputStream(in);
  ZipEntry ze;
  while ((ze = zin.getNextEntry()) != null) {
   if (ze.isDirectory()) {
    //Do nothing
   } else {
    if (ze.getName().equals(fileName + "/txt/data.json")) {
     BufferedReader br = new BufferedReader(
       new InputStreamReader(zf.getInputStream(ze)));
     String line;
     while ((line = br.readLine()) != null) {
      return line;
     }
     br.close();
    }
   }
  }
  zin.closeEntry();
  return "";
 }

上面方法比較簡單沒什么好說的,大家理解就行,有點需要注意的就是在判斷是否是想要讀取的文件的時候,這里的路徑是以zip的壓縮目錄為根目錄做比較。也就是if (ze.getName().equals(fileName + "/txt/data.json")) 這句話中的fileName 當前值為res。最后返回讀取的內(nèi)容String就完事了。

圖片和xml文件的讀取都差不多,下面直接貼出代碼了。

圖片:

zip包目錄結(jié)構:res/pic/haha.png

文件sd卡路徑:android.os.Environment.getExternalStorageDirectory() + “/res.zip”

public static Bitmap readGuidePic(String file, String ResId) throws Exception {
  String fileName = file.substring(file.length() - 9, file.length() - 4);
  ZipFile zf = new ZipFile(file);
  InputStream in = new BufferedInputStream(new FileInputStream(file));
  ZipInputStream zin = new ZipInputStream(in);
  ZipEntry ze;
  while ((ze = zin.getNextEntry()) != null) {
   if (ze.isDirectory()) {
    //Do nothing
   } else {
    Log.i("tag", "file - " + ze.getName() + " : " + ze.getSize() + " bytes");
    if (ze.getName().equals(fileName + "/pic/haha.png")) {
     InputStream is = zf.getInputStream(ze);
     Bitmap bitmap = BitmapFactory.decodeStream(is);
     return bitmap;
    }
   }
  }
  zin.closeEntry();
  return null;
 }

xml文件:

zip包目錄結(jié)構:res/xml/app.xml

文件sd卡路徑:android.os.Environment.getExternalStorageDirectory() + “/res.zip”

public static InputStream readAppFile(String file) throws IOException {
  String fileName = file.substring(file.length() - 9, file.length() - 4);
  ZipFile zf = new ZipFile(file);
  InputStream in = new BufferedInputStream(new FileInputStream(file));
  ZipInputStream zin = new ZipInputStream(in);
  ZipEntry ze;
  while ((ze = zin.getNextEntry()) != null) {
   if (ze.isDirectory()) {
    //Do nothing
   } else {
    if (ze.getName().equals(fileName + "/xml/app.xml")) {
     InputStream inputStream = zf.getInputStream(ze);
     return inputStream;
    }
   }
  }
  zin.closeEntry();
  return null;
 }

以上這篇Android 不解壓直接讀取zip包的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

最新評論

思南县| 高尔夫| 宣化县| 通城县| 栖霞市| 邢台县| 襄樊市| 铜川市| 韶山市| 独山县| 宜兰市| 柳林县| 阿拉尔市| 镇宁| 卓资县| 九龙坡区| 六安市| 吉安县| 连江县| 宁津县| 浦城县| 清水县| 枣阳市| 玉龙| 吉安县| 马公市| 宜都市| 商河县| 绍兴县| 嘉黎县| 磴口县| 南京市| 阿坝县| 大化| 华阴市| 台中县| 宜黄县| 容城县| 黑龙江省| 神农架林区| 馆陶县|