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

  • If you already unzipped the file then you should use `read.csv` directly with the path of the file you unzipped – Basti Apr 09 '21 at 07:19
  • But I don't want to manually unzip files and then read csv from the location. I was hoping to download the file from the link, unzip the file and then read the content all by code – John Krauss Apr 09 '21 at 07:24
  • Can you share the link from where you are trying to read the data directly in R? – Ronak Shah Apr 09 '21 at 07:46
  • Possible duplicate of https://stackoverflow.com/q/3053833/680068 – zx8754 Apr 09 '21 at 08:00
  • `tidyr::read_csv()` will uncompress and read .zip files directly. https://readr.tidyverse.org/reference/read_delim.html#arguments – mrhellmann Apr 09 '21 at 12:03
  • @RonakShah, I've added a link from where the zip file can be downloaded – John Krauss Apr 15 '21 at 10:02
  • @mrhellmann, received the same error using the read_csv method. `data<- readr::read_csv(temp) Error in open.connection(con, "rb") : cannot open the connection In addition: Warning message: In open.connection(con, "rb") : cannot open zip file 'C:/Users/Public/Documents/Wondershare/CreatorTemp/Rtmp8ICkE0/2f8502453e3.zip:2021-04-01T10:00_2021-04-01T10'` – John Krauss Apr 16 '21 at 00:47

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
  • 239
  • 2
  • 8
  • 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