Create zip without extension

I want to create a zip file named "out", not "out.zip". When I ran this line:

zip("out", zippedfiles) 

where zippedfiles is a list of files, I get out.zip . I am doing this in a windows environment.

Thanks.

+4
source share
2 answers

Several people mentioned that this is zip behavior, but not why this is the reason you see it. If you look at the source for zip() or even help ?zip , you should immediately understand that the behavior you see comes from the zip function of the system and has nothing to do with R. All R is a call to the system function for zipping which is the default zip :

 R> zip function (zipfile, files, flags = "-r9X", extras = "", zip = Sys.getenv("R_ZIPCMD", "zip")) { if (missing(flags) && (!is.character(files) || !length(files))) stop("'files' must a character vector specifying one or more filepaths") args <- c(flags, shQuote(path.expand(zipfile)), shQuote(files), extras) invisible(system2(zip, args, invisible = TRUE)) ## simply calling system command } <bytecode: 0x27faf30> <environment: namespace:utils> 

If the extension annoys you, just call file.rename() after zip() :

 file.rename("out.zip", "out") 
+7
source

For me, the extension is not used if I add . (i.e. period) to the file name, for example. out. must work. Full expression: zip("out.", zippedfiles) .

For what it's worth, this is due to the default zip behavior and is not a problem with R or Windows.


Update 1: In general, it is best to avoid an OS-specific approach. I think this approach can cause problems if the code runs on other platforms. Gavin’s answer regarding renaming is more portable. What else, as I suggested in the comments, testing if the target exists using file.exists() adds another layer of security before renaming. An additional level of security is achieved by obtaining a temporary file name through tempfile() . An alternative way to avoid name clashes when writing or renaming is to use a timestamp in the name.

+2
source

Source: https://habr.com/ru/post/1379936/


All Articles