0

I am trying to automate fetch of chat transcripts using an API provided by the vendor. On a successful request to the API, the response contains a link from which the chat transcripts can be downloaded as a zip containing 1 csv file with the required data. Following the steps in the link here, I was able to download the zip successfully from the link in R and store it in the temp folder. However I wasn't able to extract the csv from the zip file

temp = tempfile(pattern = "", fileext = ".zip")
download.file(download_link,temp, mode = "wb")
file_name <- as.character(unzip(temp, list = TRUE)$Name)
con <- unz(temp,file_name)
chatsData <- read.csv(con, header = T)

I received the following error on the last line-

Error in open.connection(file, "rt") : cannot open the connection
In addition: Warning message:
In open.connection(file, "rt") :
  cannot open zip file 'C:\Users\Public\Documents\Wondershare\CreatorTemp\RtmpqWLYGf\file4a5435b13659:2021-04-05T10:00_2021-04-06T10'

On checking the temp location, I was able to locate, unzip the file and read its content using WinRar. Just clueless as to why this cant be replicated in code in R.

You can download a sample of the zipfile that I am trying to extract the csv from the following link

1 Answers1

0

There is a special package on CRAN that brings everything necessary to zip and unzip archives:

https://cran.r-project.org/package=zip

If you are sure you downloaded your zip file to a local directory you might be able to use unzip() function this package provides to extract your desired CSV.

You could download your file, unzip it, read the contained csv and delete both the original zip and the csv if you want to keep your harddrive "clean"...

shghm
  • 90
  • 5
  • Thanks unfortunately, on trying this I received the following error `zip::unzip(temp)` `Error in zip::unzip(temp) : zip error: Cannot extract file 2021-04-01T10:00_2021-04-02T10:00_Chat-Transcript_dc8d561e-4485-45b5-98c7-a99f4e3954c9.csv in file zip.c:211` – John Krauss Apr 15 '21 at 07:09
  • Have also added a link to the zip file in question. – John Krauss Apr 15 '21 at 10:15