Android怎么實現(xiàn)不解壓直接讀取zip包-創(chuàng)新互聯(lián)

這篇文章主要講解了“Android怎么實現(xiàn)不解壓直接讀取zip包”,文中的講解內(nèi)容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Android怎么實現(xiàn)不解壓直接讀取zip包”吧!

創(chuàng)新互聯(lián)長期為數(shù)千家客戶提供的網(wǎng)站建設服務,團隊從業(yè)經(jīng)驗10年,關注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為松山企業(yè)提供專業(yè)的成都網(wǎng)站建設、做網(wǎng)站,松山網(wǎng)站改版等技術服務。擁有十載豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。

文本:

zip包目錄結(jié)構(gòu):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é)構(gòu):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é)構(gòu):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怎么實現(xiàn)不解壓直接讀取zip包”的內(nèi)容了,經(jīng)過本文的學習后,相信大家對Android怎么實現(xiàn)不解壓直接讀取zip包這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián)網(wǎng)站建設公司,,小編將為大家推送更多相關知識點的文章,歡迎關注!

網(wǎng)站題目:Android怎么實現(xiàn)不解壓直接讀取zip包-創(chuàng)新互聯(lián)
鏈接分享:http://muchs.cn/article44/ceccee.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、營銷型網(wǎng)站建設、網(wǎng)站內(nèi)鏈外貿(mào)建站、ChatGPT用戶體驗

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設公司