Search code examples
rtarraster

untar a selection of files


I am trying to untar several .tar files, but I only want some compressed files within the .tar files to be extracted. All the .tar files have the following content in different orders:

[1] "README_V4.txt"                                "F182011.v4c_web.avg_vis.tfw"                 
[3] "F182011.v4c_web.avg_vis.tif.gz"               "F182011.v4c_web.cf_cvg.tfw"                  
[5] "F182011.v4c_web.cf_cvg.tif.gz"                "F182011.v4c_web.stable_lights.avg_vis.tfw"   
[7] "F182011.v4c_web.stable_lights.avg_vis.tif.gz"

I only need the "F182011.v4c_web.stable_lights.avg_vis.tif.gz" file to be extracted. I tried the following code, but nothing seems to happen:

untar_tiff <- function(filename, folder) { 
  dir.create(folder, showWarnings = F)
  list <- untar(filename, list = T)
  untar(filename, files = str_c(folder, "/", list[str_detect(list, "web.stable")]), exdir = folder)
} 

lapply(filenames_list, untar_tiff,
      folder = "TIFF")

I think the problem is in the selection of the files to be decompressed (the fileoption in the untar function), but I have tried several options without good results.

Thanks in advance,


Solution

  • The problem I was having was related with a bad understanding of the arguments in the untarfunction.

    The code i ran was this one:

    untar_tiff <- function(filename, folder) { 
      dir.create(folder, showWarnings = F)
      list <- untar(filename, list = T)
      untar(filename, files = str_c(list[str_detect(list, "web.stable")]), exdir = folder)
    } 
    
    lapply(filenames_list, untar_tiff,
          folder = "TIFF")
    

    As you may note, I remove the complete file path that was stated in the filesoption and change it for the name of the file that I want to extract from the tarball. The reason behind why this worked remains unclear to me, my best guess is that the exdiroption already put the files in that folder, and the code was only cheking the existence of this files into the .tar file.

    If someone can give a better answer it would be great to other people with a related problem.