Android怎么实现不解压直接读取zip包-创新互联

这篇文章主要讲解了“Android怎么实现不解压直接读取zip包”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Android怎么实现不解压直接读取zip包”吧!

创新互联长期为数千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为松山企业提供专业的成都网站建设、做网站,松山网站改版等技术服务。拥有十载丰富建站经验和众多成功案例,为您定制开发。

文本:

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包”的内容了,经过本文的学习后,相信大家对Android怎么实现不解压直接读取zip包这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联网站建设公司,,小编将为大家推送更多相关知识点的文章,欢迎关注!

网站题目:Android怎么实现不解压直接读取zip包-创新互联
链接分享:https://www.cdcxhl.com/article44/ceccee.html

成都网站建设公司_创新互联,为您提供全网营销推广营销型网站建设网站内链外贸建站ChatGPT用户体验

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都网站建设公司