zip_files_by_pattern() compresses files by grouping them according to
specified patterns and splitting them into chunks if needed.
Usage
zip_files_by_pattern(
  files,
  pattern = NULL,
  prefix = NULL,
  suffix = NULL,
  max_size = fs::fs_bytes("1GB"),
  appendices = NULL,
  root = ".",
  dir = root,
  ...
)Arguments
- files
 A
charactervector specifying the paths to the files to be compressed. Only relative paths are supported. Seezip()for more details.- pattern
 (optional) A
charactervector specifying one or more patterns to group files for zipping. Files matching each pattern will be aggregated into a single zip file, named using the pattern andsuffix. IfNULL, each file will be zipped individually (default:NULL).- prefix
 (optional) A string specifying a prefix to be added to the zip file names (default:
NULL).- suffix
 (optional) A string specifying a suffix to be added to the zip file names (default:
NULL).- max_size
 (optional) An integer or
fs_bytesvalue specifying the maximum size of the zip files in bytes. The function will try to split the files into chunks if the total size of the files exceeds this value. If an individual file is larger than the limit, it will be placed in its own chunk. UseInfto disable chunking (default:fs_bytes("1GB")).- appendices
 (optional) A
charactervector specifying additional files to be included in each zip file. Like infiles, only relative paths are supported (default:NULL).- root
 (optional) A string specifying the root directory of the files. See
zip()for more details (default:.).- dir
 (optional) A string specifying the directory where the zip files must be saved (default:
root).- ...
 Additional arguments passed to the
zipfunction.
Value
An invisible character vector containing the
paths to the created zip files.
Details
This function uses the zip package for cross-platform
compatibility and efficient file compression.
See also
Other file functions:
identify_blank_line_neighbors(),
normalize_hashtags(),
normalize_names(),
peek_csv_file(),
remove_blank_line_dups(),
replace_in_file(),
sort_files_by_size(),
split_file(),
split_files_by_size()
Examples
files <- c("test_1_1.txt", "test_1_2.txt", "test_2.txt")
appendices <- c("appendix.txt")
txt_dir <- tempfile("dir")
zip_dir <- tempfile("dir")
dir.create(txt_dir)
dir.create(zip_dir)
for (i in files) file.create(file.path(txt_dir, i))
for (i in appendices) file.create(file.path(txt_dir, i))
list.files(txt_dir)
#> [1] "appendix.txt" "test_1_1.txt" "test_1_2.txt" "test_2.txt"  
#> [1] "appendix.txt" "test_1_1.txt" "test_1_2.txt" "test_2.txt" # Expected
zip_files_by_pattern(
  files = files,
  pattern = c("test_1", "test_2"),
  appendices = appendices,
  root = txt_dir,
  dir = zip_dir
)
#> ℹ Zipping files using 2 patterns: test_1 and test_2.
#> ℹ Zipping 2 files matching the pattern test_1 into 1 chunk.
#> ℹ Zipping 1 file matching the pattern test_2 into 1 chunk.
list.files(zip_dir)
#> [1] "test_1.zip" "test_2.zip"
#> [1] "test_1.zip" "test_2.zip" # Expected
unzip_dir <- tempfile("dir")
dir.create(unzip_dir)
library(zip)
#> 
#> Attaching package: ‘zip’
#> The following objects are masked from ‘package:utils’:
#> 
#>     unzip, zip
for (i in file.path(zip_dir, list.files(zip_dir))) {
  unzip(i, exdir = unzip_dir)
}
list.files(unzip_dir)
#> [1] "appendix.txt" "test_1_1.txt" "test_1_2.txt" "test_2.txt"  
#> [1] "appendix.txt" "test_1_1.txt" "test_1_2.txt" "test_2.txt" # Expected