unzip_dir: Unzip all files in a directory.

Description Usage Arguments Details Value Examples

Description

Unzip all files in a given directory, identified by extension, placing the unzipped file in a separate output directory.

Usage

1
unzip_dir(in_dir, out_dir, ext)

Arguments

in_dir

Input directory containing the zipped files to convert.

out_dir

Output directory to place the unzipped files.

ext

Zipped file extension. The extensions supported are

  • gz - gzipped files, unzipped with gunzip

  • zip - zip files, unzipped with unzip

  • zipx - zipx or archive files, unzipped with unar

Details

unzip_dir makes system calls to gunzip, unzip, and unar. Each of these need to be on the system PATH and able to be called from system2. Calls have only been tested on OSX at this time.

On OSX, unar may be installed via Homebrew using the install command brew install unar.

Finally, unzip_dir checks whether or not the output directory already contains the file to be unzipped. If the file already exists, then unzipping is skipped for the file. This allows one to re-run the function multiple times without continually unzipping all files in the input directory.

Value

Invisibly returns a character vector of the zipped file basenames.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
withr::with_dir(
  new = tempdir(),
  code = {

    # create two small tables to write out as files
    a_df <- tibble::tribble(
      ~a, ~b, ~c,
      1, 2, 3,
      4, 5, 6,
      7, 8, 9
    )

    d_df <- tibble::tribble(
      ~d, ~e, ~f,
      11, 22, 33,
      44, 55, 66,
      77, 88, 99
    )
    # make a new input directory
    system2(command = "mkdir",
            args = "in_dir")
    setwd("in_dir")

    # write csv files
    readr::write_csv(a_df, "a.csv")
    readr::write_csv(d_df, "d.csv")

    # convert into gzip files
    system2(command = "gzip",
            args = c("a.csv", "d.csv"))

    # make a new output directory
    setwd("..")
    system2(command = "mkdir",
            args = "out_dir")

    # unzip all files
    unzip_dir("in_dir", "out_dir", "gz")

    # list all files
    system2(command = "ls",
            args = c("-l", "out_dir"))
  }
)

curtisalexander/CRAmisc documentation built on May 14, 2019, 12:52 p.m.