温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android如何实现不解压直接读取zip包

发布时间:2021-06-28 14:45:46 来源:亿速云 阅读:170 作者:小新 栏目:移动开发

这篇文章主要为大家展示了“Android如何实现不解压直接读取zip包”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Android如何实现不解压直接读取zip包”这篇文章吧。

文本:

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包”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI