国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

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

2019-10-22 18:16:33
字體:
來源:轉載
供稿:網友

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

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

文本:

zip包目錄結構: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。最后返回讀取的內容String就完事了。

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

圖片:

zip包目錄結構: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包目錄結構: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包的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阳谷县| 南城县| 福清市| 安龙县| 汨罗市| 喜德县| 嘉定区| 宁城县| 桐柏县| 扶绥县| 陆河县| 留坝县| 麦盖提县| 山丹县| 蒙自县| 六盘水市| 旺苍县| 六安市| 汉中市| 贵溪市| 龙口市| 樟树市| 淳安县| 吉木萨尔县| 丰城市| 涞源县| 南漳县| 贵阳市| 任丘市| 潢川县| 永丰县| 赣榆县| 南通市| 清徐县| 宽甸| 屏山县| 江达县| 昌江| 沾化县| 攀枝花市| 牟定县|