R detect filename encoding for downloaded zipfile

Viewed 271

I downloaded a file zip file from the web, using Windows.

The files contained have latin characters as they're from a Mexican website: this one followed by the clickin on "Descargar".

If I extract the files with File Explorer, they display the names correctly.
E.g. "códigos_postales\tabla de códigos.txt" with a few more of them. (postal codes tables)

But if I extract it via R with unzip("códigos_postales.zip") (using RStudio) they are shown incorrectly.
E.g. "c¢digos postales\tabla de c¢digos.txt"

Setting the encodings beforehand I have tried:

zc_list <- unizp("códigos_postales.zip", list = TRUE)
zc_names <- zc_list$Name
Encoding(zc_names) <- "latin1" (also "utf-8" and "Windows-1252") 

But this doesn't show them correctly, and in any case it doesn't help me to extract them.
Moreover, guessing the encoding via:

> guess_encoding(zc_names)
ISO-8859-1
ISO-8859-2

> Encoding(zc_names) <- "ISO-8859-1"  #(or -2)
> zc_names 
...
c¢digos_postales/Tabla de C¢digos Postales.txt

So I also tried to download them first and rename them later:

cp_extracted <- unzip("códigos_postales.zip")
for (each_file in cp_extracted) {
    file.rename(each_file, each_file %>% str_replace_all("¢", "ó"))
}

This doesn't work, and even erases the files, for two reasons:
+ There are nested files and folders that have the latin characters, so the rename must only be applied to the filename.
+ Ultimately, I only identified this character "¢" -> "ó", not sure if there may be others. And it isn't right to have to identify wrong characters if not by the encoding overall.

The questions that I have are:
1. How to properly extract the zipfile so that the encoding is applied correctly.
2. If this is not possible, and I do need to rename them, how can I traverse the files and directories and replace them each with a proper set of latin characters identified.

My machine's locale is:

> Sys.getlocale()
[1] "LC_COLLATE=Spanish_Mexico.1252;LC_CTYPE=Spanish_Mexico.1252;LC_MONETARY=Spanish_Mexico.1252;LC_NUMERIC=C;LC_TIME=Spanish_Mexico.1252"

Thanks.

0 Answers
Related