Creating zip file from folders in R

前端 未结 5 1477
故里飘歌
故里飘歌 2021-02-13 12:12

Try to create a zip file from one folder using R.

It mentioned \"Rcompression\" package here: Creating zip file from folders

But I didn\'t find where I can downl

5条回答
  •  轮回少年
    2021-02-13 12:57

    You can create a zip file with the function zip from utils package quite easily. Say you have a directory testDir and you wish to zip a file (or multiple files) inside the directory,

    dir('testDir')
    # [1] "cats.csv" "test.csv" "txt.txt" 
    zip(zipfile = 'testZip', files = 'testDir/test.csv')
    # adding: testDir/test.csv (deflated 68%)
    

    The zipped file is saved in the current working directory, unless a different path is specified in the zipfile argument. We can see its size relative to the original unzipped file with

    file.info(c('testZip.zip', 'testDir/test.csv'))['size']
    #                  size
    # testZip.zip       805
    # testDir/test.csv 1493
    

    You can zip the whole directory of files (if no sub-folders) with

    files2zip <- dir('testDir', full.names = TRUE)
    zip(zipfile = 'testZip', files = files2zip)
    # updating: testDir/test.csv (deflated 68%)
    # updating: testDir/cats.csv (deflated 27%)
    # updating: testDir/txt.txt (stored 0%)
    

    And unzip it to view the files,

    unzip('testZip.zip', list = TRUE)
    #               Name Length                Date
    # 1 testDir/test.csv   1493 2014-05-14 20:54:00
    # 2 testDir/cats.csv    116 2014-05-14 20:54:00
    # 3  testDir/txt.txt     32 2014-05-08 09:37:00
    

    Note: From ?zip, regarding the zip argument.

    On Windows, the default relies on a zip program (for example that from Rtools) being in the path.

提交回复
热议问题