React-Native:如何打开本地捆绑的二进制文件 [英] React-Native: How to open locally bundled binary file

查看:54
本文介绍了React-Native:如何打开本地捆绑的二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 react-native 应用,我希望它使用包含设备固件更新的 zip 文件进行部署.

I'm writing a react-native app, and I want it to deploy with a zip file that contains a device firmware update.

在让用户发送更新之前,我需要我的代码来打开 zip 并对其内容进行一些验证.

Before letting the user send the update, I need my code to open the zip and do some validation of its contents.

我发现了很多处理 zip 的 NPM 包,所以我需要做的就是加载文件内容,这样我就可以将其提供给其中之一.

I've found lots of zip-handling NPM packages, so all I need to do is load the file contents so I can feed it to one of these.

  • require('./firmware/fw.zip');<-- 打包器默认不包含 .zip
  • require('./firmware/fw.pdf');<-- [gross hack] 打包程序包括 pdf,但 require() 调用的实际结果是一个数字:5.我不知道我可以用这个数字做什么来获取文件内容,但我很确定这个 require() 系统是为加载图像而不是二进制数据而设计的.
  • ReactNativeFs.openFile('./firmware/fw.zip');<-- 因 ENOENT 失败
  • ReactNativeFs.openFile(${ReactNativeFs.MainBundlePath}/firmware/fw.zip);<-- MainBundlePath 在 android 上是 undefined.
  • require('./firmware/fw.zip'); <-- packager doesn't include .zip by default
  • require('./firmware/fw.pdf'); <-- [gross hack] packager includes pdfs, but the actual result of the require() call is a number: 5. I don't know what I can do with this number to get file contents, but I'm pretty sure this require() system is designed for loading images, not binary data.
  • ReactNativeFs.openFile('./firmware/fw.zip'); <-- fails with ENOENT
  • ReactNativeFs.openFile(${ReactNativeFs.MainBundlePath}/firmware/fw.zip); <-- MainBundlePath is undefined on android.

这似乎是一个非常基本的问题,所以我确定我在某处遗漏了一段文档,但我正在进入我的第三个小时尝试加载此文件的内容,但没有运气.

This seems like a really basic question, so I'm sure I've missed a piece of documentation somewhere, but I'm heading into my third hour trying to load the contents of this file with no luck.

我很确定我可以手动将 zip 文件放入适当的 androidios 资源目录中,但这似乎是一个难以-维护道路.

I'm pretty sure I could manually put the zip file into the appropriate android and ios resource directories, but that seems like a step down a hard-to-maintain road.

推荐答案

几个月后我再次遇到这个问题(我显然是唯一需要在 react-native 中打包 .zips 的人),上面的答案不适用于 iOS.所以我将 .zip 编码为 base64,将它们放入 .js 文件中,然后使用 import 从这些 .js 文件中获取数据.这实际上似乎是一个有点老套但又灵活的长期解决方案,而无需处理依赖于平台的文件位置.

I encountered this problem again a couple months later (I'm apparently the only guy that needs to package .zips in react-native), and the above answer didn't work out for iOS. So I encoded the .zips as base64, put them in .js files, then used import to get the data from those .js files. This actually seems like a somewhat hacky but also flexible long-term solution, without having to mess around with platform-dependent file locations.

在我的新问题中查看完整答案:React-native 打包器配置 - 如何在包中包含 .zip 文件?

See whole answer at my new question: React-native packager configuration - How to include .zip file in bundle?

这篇关于React-Native:如何打开本地捆绑的二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆