sevenzip_makecmd: Create commands to perform system calls with 7-zip

View source: R/sevenzip_makecmd.R

sevenzip_makecmdR Documentation

Create commands to perform system calls with 7-zip

Description

Helper to construct argument lists to use with system2, to invoke 7-zip commands

Usage

sevenzip_makecmd(
  action = NULL,
  target = NULL,
  output = NULL,
  extras = NULL,
  verbose = TRUE
)

Arguments

action

(required) One of a,d,e,l,t,u,x, or the literal names, archive,delete,extract,list,test,update,extract with full paths. Intentionally not all-encompassing

target

(required) The (input) path upon which to ultimately perform the action

output

The (output) path; required for actions a,e,u,x.

extras

(optional) Other commands to pass to 7zip. If provided, will be appended last

verbose

Output the full command string to console? Defaults to TRUE

Details

This is simply a call-construction helper that also checks for the presence of 7-zip in your PATH on Windows systems. It does not invoke any system calls beyond said check (and no check is performed on a non-Windows OS. There is also minimal validation, meaning that it is possible to construct invalid calls.

The inputs to target, output are automatically reversed for archive actions, i.e. a, u. This is done to ensure that the semantic intent of the arguments is consistent with the actual command ordering. For any actions other than archive, update, the values passed to target, output are passed through as-is.

Value

A list of length 2, containing the command and args; the former will always be 7z. Specifically designed to be compatible with system2 via do.call

Note

There is no guarantee that this will generate valid commands for a non-Windows OS.

The main purpose of this function is to make it easier to construct syntactically valid commands, and the main audience comprises those who are not already fluent in invoking 7-zip from the command line, but need to use some of its capabilities programatically. If you feel something is missing, it probably is, but then you are also likely not the target end-user.

If your file reference(s) contain a space, make sure you quote the values with e.g. shQuote(). Also, to e.g. archive multiple files at a time, paste() the values together to create a vector of length 1, using the default whitespace separator for paste; in this situation, you must shQuote each of the file references prior to pasting them.

See Also

Other sevenzip functions: sevenzip_checkpath(), sevenzip_listcont(), sevenzip_parseinfo()

Examples

## Not run: 
# make a temp dir and populate with a simple file
my_tempdir <- tempdir()
my_tempfile <- tempfile(tmpdir = my_tempdir, fileext = ".txt")
sink(my_tempfile)
cat(1:10)
sink()
list.files(my_tempdir)

# make a zip command
my_cmd_zip <- sevenzip_makecmd("a",
                               target = paste(my_tempfile),
                               output = paste(my_tempdir, "test.7z", sep = "/")
)
do.call(system2, my_cmd_zip)
list.files(my_tempdir)
# make an unzip command, into a new dir called 'exracted'
my_cmd_unzip <- sevenzip_makecmd("e",
                                 target = paste(my_tempdir, "test.7z", sep = "/"),
                                 output = paste(my_tempdir, "extracted", sep = "/")
)
do.call(system2, my_cmd_unzip)
file.info(paste(my_tempdir, "extracted", sep = "/"))
# if you try to extract a file and it exists, you will get an error unless you pass
#  required flags, e.g:
my_cmd_unzip_overwrite <- sevenzip_makecmd("e",
                                           target = paste(my_tempdir, "test.7z", sep = "/"),
                                           output = paste(my_tempdir, "extracted", sep = "/"),
                                           extras = "-aoa"
)
do.call(system2, my_cmd_unzip_overwrite)
file.info(paste(my_tempdir, "extracted", sep = "/"))
Map(file.remove, list.files(my_tempdir, recursive = TRUE, full.names = TRUE))
unlink(paste(my_tempdir, "extracted", sep = "/"), recursive = TRUE)

## End(Not run)

slin30/wzMisc documentation built on Jan. 27, 2023, 1 a.m.