Skip to contents

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"),
  root = ".",
  dir = root,
  ...
)

Arguments

files

A character vector specifying the paths to the files to be compressed. Only relative paths are supported. See zip() for more details.

pattern

(optional) A character vector 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 and suffix. If NULL, 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_bytes value 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. Use Inf to disable chunking (default: fs_bytes("1GB")).

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 zip function.

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.

Examples

files <- c("test_1_1.txt", "test_1_2.txt", "test_2.txt", "test_3.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))

list.files(txt_dir)
#> [1] "test_1_1.txt" "test_1_2.txt" "test_2.txt"   "test_3.txt"  
#> [1] "test_1_1.txt" "test_1_2.txt" "test_2.txt" "test_3.txt" # Expected

zip_files_by_pattern(
  files = files,
  pattern = c("test_1", "test_2"),
  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] "test_1_1.txt" "test_1_2.txt" "test_2.txt"  
#> [1] "test_1_1.txt" "test_1_2.txt" "test_2.txt" # Expected