21

I am trying to create a zip file and want to preserve most of the directory structure, but not the rootdir as defined from the command line. The command I'm using is:

zip -r out.zip /foo/bar/

I'd like it to recurse through bar and add all files with preserved directory structure (which it does). However I do not want 'foo' to be the top level directory in the zip file created. I would like bar to be the top level directory.

Is there any easy way to go about this? I realize I could change directories before zipping to avoid the problem, but I'm looking for a solution that doesn't require this.

Brandon Yates
  • 2,022
  • 3
  • 22
  • 33

4 Answers4

12

This should do it:

cd /foo/bar/ 
zip -r ../out.zip *

The archive will be in /foo/out.zip

Claude Schlesser
  • 849
  • 6
  • 15
9

I don't believe zip has a way to exclude the top level directory. I think your best bet would be to do something like: pushd /foo; zip -r out.zip ./bar; popd;

But this is exactly the sort of answer you said you didn't want.

kbyrd
  • 3,321
  • 27
  • 41
6

7z a -tzip out.zip -w foo/bar/.

reisio
  • 3,242
  • 1
  • 23
  • 17
-1

If someone stumbles upon this and is not satisfied with the above solution, here follows a very simple workaround to not zip long subdirectories. It involves temporarily creating a folder in C:/, and after zipping simply deleting it:

ZipFiles <- list.files(".../ZipFiles") # Insert your long subdirectory into .../

dir.create("C:/ZipFiles")
dir.create(".../FolderToBeZipped")
file.copy(from = ZipFiles,to = "C:/ZipFiles")
zip(".../FolderToBeZipped",
    files = "C:/ZipFiles")
unlink("C:/ZipFiles",recursive = TRUE)

The result then is .../FolderToBeZipped.zip/ZipFiles/

The benefit is that you need not be within the subdirectory (or project) when executing the code.

Nick
  • 3,262
  • 30
  • 44