Search code examples
androidiosreact-nativediskspace

Checking for available disk space in React Native


I have implemented the following code:

  1. Download a zip file using RNFS.downloadFile()
  2. Unzip the file using ZipArchive.unzip()
  3. Delete the zip file using RNFS.unlink()

I can send info from the server indicating how big the zip file is and how big the unpacked directory is. However, how can I detect if there is enough space on the device to download and unzip the file? I assume once I figure this out I can just do a check like this:

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

Solution

  • I did not realize that RNFS had a function called getFSInfo. With that knowledge, I can just issue the following command:

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