How to keep the encoded file name (UTF-8) when unzipping in R?

Asked

Viewed 22 times

-1

I’m having trouble accessing files from a compressed folder. The filenames contain accents, but when unzipping the names are changed according to the configuration of each computer that opens and executes the code. Is there any way to keep the encoded file name when unzipping or to be able to access the files from that folder? I need to do everything via R code (version 4.0.2) in windows 10. By running the following code:

dir.create("./federal_highway")
setwd("./federal_highway/")
temp <- tempfile()
download.file(URLencode("https://servicos.dnit.gov.br/dnitcloud/index.php/s/oTpPRmYs5AAdiNr/download"), destfile = "./original/Repositorio.zip", method="auto", mode="wb")
unzip("./original/Repositorio.zip") 
zipfiles <- list.files(path = "./Repositório/SNV Bases Geométricas (2013-Atual) (SHP)", full.names = T, pattern = "*.zip")

It runs smoothly but I get the following message:

Warning message:
In list.files(path = "./Repositório/SNV Bases GeomÃ<U+0083>©tricas (2013-Atual) (SHP)",  :
  unable to translate './Repositório/SNV Bases GeomÃ<U+0083>©tricas (2013-Atual) (SHP)' to native encoding

And zipfiles returns "Character(0)"

1 answer

0

You can use the dir() command to mount the file path as follows:

setwd("./federal_highway/")
repo <- dir("./sf_all_years_original")
repozipfiles <- dir(paste0("./sf_all_years_original/",repo[1]))
SNV <- repozipfiles[3]
dir_repo <- paste0("./sf_all_years_original/",repo[1],"/",SNV,"/")
setwd(dir_repo)

a <- getwd()
zipfiles <- dir(a)

Unzip the zipped folders

for(i in 1:length(zipfiles)){
  unzip(zipfiles[i])
}

List only shp files, for example

raw_shapes <- list.files(".", pattern = ".shp", recursive = T, full.names = T)
raw_shapes <- raw_shapes[raw_shapes %nlike% '.xml']

Browser other questions tagged

You are not signed in. Login or sign up in order to post.