Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(httr)
- library(unzip)
- url <- "http://example.com/path/to/file.zip"
- output_dir <- path.expand("~/Desktop")
- # Make a GET request to the URL and stream the response
- response <- httr::GET(url, write_disk(tempfile(), overwrite = TRUE))
- # Check the response status
- if (httr::status_code(response) == 200) {
- # Open the .zip file
- zip_file <- unzip::zip(tempfile())
- # Iterate over the files in the .zip file
- for (file_name in zip_file$files$Name) {
- if (grepl("street", file_name, ignore.case = TRUE)) {
- # Extract the file to the output directory
- unzip::unzip(tempfile(), files = file_name, exdir = output_dir, junkpaths = TRUE)
- print(paste0("Downloaded file: ", file_name))
- }
- }
- # Delete the .zip file
- unlink(tempfile())
- } else {
- print("Failed to download the file.")
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement