Creating zip file from folders in R

前端 未结 5 1334
故里飘歌
故里飘歌 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:33

    For avoiding (a) an issue with relative paths (i.e., the zip file itself containing a folder structure with the full folder path to be zipped) and (b) for loops (well, style), you may use

    my_wd<-getwd() # save your current working directory path
    dest_path<-"C:/.../folder_with_files_to_be_zipped" 
    setwd(dest_path)
    files<-list.files(dest_path)
    named<-paste0(files,".zip")
    mapply(zip,zipfile=named,files=files)
    setwd(my_wd) # reset working directory path
    

    Unlike R´s build-in unzip function, zip requires a zip-program like 7-zip (Windows) or the one being part of Rtools to be present in your system path.

    0 讨论(0)
  • 2021-02-13 12:48

    For people still looking for this: there is now a "zip" package that does not depend on external executables.

    0 讨论(0)
  • 2021-02-13 12:53

    Make that

        #Convertir todas las carpetas en .zip
        d <- "C:/Users/Eric/Documents/R/win-library/3.3"
        array <- list.files(d)
    
        for (i in 1:length(array)){
          name <- paste0(array[i],".zip")
    
          zip(name, files = paste0(d,paste0("/",array[i])))
        }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-13 12:57

    You can install from the omegahat repos:

    install.packages('Rcompression', repos = "http://www.omegahat.org/R", type = "source")
    

    for windows you will need to jump through hoops installing zlib and bzip2 and linking appropriately.

    utils::zip can be used in some cases. There are a number of issues with it. One case is that the maximum length of the string that you can use at the command prompt is 8191 characters (2047 characters on some versions) for windows. If you are zipping a directory with alot of characters for the names of directories/files this will cause issues. For example if you zip your firefox profile directory. Also I found the zip command needed to be issued relative the directory I was zipping to use relative directory names. Rcompression has a altNames argument which handles this. That being said I have always had problems getting Rcompression to run on windows.

    0 讨论(0)
提交回复
热议问题