Tests the integrity of a ZIP file using the unzip -t
system command.
Verifies that the file exists, is non-empty, has a .zip
extension, and
passes the integrity check. Returns FALSE
with a warning if the file is
invalid or if unzip
is unavailable.
Value
Logical: TRUE
if the file exists, is non-empty, and passes the
integrity check; FALSE
otherwise, accompanied by a warning explaining the
failure.
Examples
# |||||||||||||||||||||||||||||||||||||||
# Create ZIP files
# |||||||||||||||||||||||||||||||||||||||
# valid ZIP file
temp_dir <- fs::path_temp("check_zip")
fs::dir_create(temp_dir)
temp_file <- fs::path(temp_dir, "test.txt")
writeLines("Hello, world!", temp_file)
zip_file <- fs::path(temp_dir, "valid.zip")
zip(zip_file, temp_file, flags = "-jq")
# invalid ZIP file (corrupted)
bad_zip <- fs::path(temp_dir, "invalid.zip")
writeLines("Not a ZIP file", bad_zip)
# empty ZIP file
empty_zip <- fs::path(temp_dir, "empty.zip")
fs::file_create(empty_zip)
# non-ZIP file
non_zip_file <- fs::path(temp_dir, "test.txt")
writeLines("Hello, world!", non_zip_file)
# |||||||||||||||||||||||||||||||||||||||
# Check ZIP files
# |||||||||||||||||||||||||||||||||||||||
check_zip(zip_file) # TRUE
#> [1] TRUE
check_zip(bad_zip) # FALSE, with warning
#> Warning: Not a valid ZIP file: /tmp/RtmphTiYIO/check_zip/invalid.zip
#> [1] FALSE
check_zip(bad_zip, warning = FALSE) # FALSE, without warning
#> [1] FALSE
# non-existent file
check_zip("nonexistent.zip") # FALSE, with warning
#> Warning: File does not exist: /home/runner/work/ecokit/ecokit/docs/reference/nonexistent.zip
#> [1] FALSE
check_zip("nonexistent.zip", warning = FALSE) # FALSE, without warning
#> [1] FALSE
check_zip(empty_zip) # FALSE, with warning
#> Warning: File is empty: /tmp/RtmphTiYIO/check_zip/empty.zip
#> [1] FALSE
check_zip(empty_zip, warning = FALSE) # FALSE, without warning
#> [1] FALSE
check_zip(non_zip_file) # FALSE, with warning
#> Warning: Not a valid ZIP file: /tmp/RtmphTiYIO/check_zip/test.txt
#> [1] FALSE
check_zip(non_zip_file, warning = FALSE) # FALSE, without warning
#> [1] FALSE
# clean up
fs::file_delete(c(zip_file, bad_zip, empty_zip, temp_file))
fs::dir_delete(temp_dir)