在 React Native 中检查可用磁盘 space

Checking for available disk space in React Native

我实现了以下代码:

  1. 使用 RNFS.downloadFile()
  2. 下载 zip 文件
  3. 使用 ZipArchive.unzip()
  4. 解压缩文件
  5. 使用 RNFS.unlink()
  6. 删除 zip 文件

我可以从服务器发送信息,指示 zip 文件有多大以及解压后的目录有多大。但是,如何检测设备上是否有足够的 space 来下载和解压缩文件?我想一旦我弄明白了,我就可以像这样检查一下:

if (free_space_on_device > zip_size + unpacked_size){
   proceed with steps 1 through 3 (listed above)
}

我没有意识到 RNFS 有一个名为 getFSInfo 的函数。有了这些知识,我可以发出以下命令:

RNFS.getFSInfo()
.then ((info) => {
   console.log("Free Space is" + info.freeSpace + "Bytes")
   console.log("Free Space is" + info.freeSpace / 1024 + "KB")
})

如果您使用的是 react-native-fetch-blob 而不是 react-native-fs,这就是您要查找的函数:

RNFetchBlob.fs.df().then( response => {
  console.log('Free space in bytes: ' + response.free);
  console.log('Total space in bytes: ' + response.total);
} );

在 Android 上,响应对象如下所示:

RNFetchBlob.fs.df().then( response => {
  console.log('External free space in bytes: ' + response.external_free);
  console.log('External total space in bytes: ' + response.external_total);
  console.log('Internal free space in bytes: ' + response.internal_free);
  console.log('Internal total space in bytes: ' + response.internal_total);
} );