Advertisement
themaleem

Untitled

Jul 5th, 2023 (edited)
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 0.82 KB | None | 0 0
  1. library(httr)
  2. library(unzip)
  3.  
  4. url <- "http://example.com/path/to/file.zip"
  5. output_dir <- path.expand("~/Desktop")
  6.  
  7. # Make a GET request to the URL and stream the response
  8. response <- httr::GET(url, write_disk(tempfile(), overwrite = TRUE))
  9.  
  10. # Check the response status
  11. if (httr::status_code(response) == 200) {
  12.   # Open the .zip file
  13.   zip_file <- unzip::zip(tempfile())
  14.  
  15.   # Iterate over the files in the .zip file
  16.   for (file_name in zip_file$files$Name) {
  17.     if (grepl("street", file_name, ignore.case = TRUE)) {
  18.       # Extract the file to the output directory
  19.       unzip::unzip(tempfile(), files = file_name, exdir = output_dir, junkpaths = TRUE)
  20.       print(paste0("Downloaded file: ", file_name))
  21.     }
  22.   }
  23.  
  24.   # Delete the .zip file
  25.   unlink(tempfile())
  26. } else {
  27.   print("Failed to download the file.")
  28. }
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement