47

I am using 7z command line executable to zip files, but I see that while adding to an archive the path of the files is preserved in the archive.

So if I do

7z a -tzip  myzip.zip dir1\dir2\*

the archive myzip.zip will contain the path dir1\dir2. I do not want this, rather I want only the files to be added to the zip file without the paths being preserved. I searched quite a bit but do not seem to find any way of doing this, maybe I am missing something obvious?

Thanks

Raam
  • 10,296
  • 3
  • 26
  • 27

5 Answers5

94

Just add a dot before the path, i.e. 7z a -tzip -r myzip.zip .\Relative\Dir\*

Uli Gerhardt
  • 13,748
  • 1
  • 45
  • 83
Nickolay Olshevsky
  • 13,706
  • 1
  • 34
  • 48
34

Give the full path. That should work. Not the relative path from the current location. For example, I give the below, where I want the files in the man5 folder to be archived.

$ 7z a -tzip myzip.zip /home/pradeeban/Desktop/man4/man5/*

The zip contained only the files, without the directories.

Then I gave only the relative path. It had the directories, inside the zip.

$ 7z a -tzip myzip.zip Desktop/man4/man5/*

Tried with Linux (Ubuntu 12.04). Not sure whether that differs from Windows.

rkmax
  • 17,633
  • 23
  • 91
  • 176
  • 2
    +1 It works in Windows. I had exactly this problem, and exactly this solution worked. – Stephen Hosking Sep 03 '12 at 22:27
  • 4
    Adding the full path requires you (most likely) to look that up. The solution Nicklay O. is providing is more convenient. It only requires you to add a dot in front of the path. – Benjamin Wegman Feb 06 '13 at 20:51
  • NOTE: according to the same [7z manual](http://linux.die.net/man/1/7z) `dir1/dir2/*` **excludes** all hidden files (like `dir1/dir2/.htaccess`). – Kamiccolo Feb 20 '14 at 18:26
  • A note about note by @Kamiccolo Using shell wildcard expansion, typing `dir1/dir2/*` in the terminal emulator, indeed excludes dotfiles, however, this has nothing to do with `7z` behaviour, it's done by your shell. According to my tests, however, if you literally pass the argument `dir1/dir2/*` (like `7z a myzip.zip "dir1/dir2/*"`), it adds all files, _including_ dotfiles (hidden files). – Alex Potapenko Aug 14 '22 at 11:46
  • Inconsistent behaviour then *sigh* – Kamiccolo Aug 15 '22 at 15:03
15

I discovered a way to do this by using a relative path:

7z a -tzip  myzip.zip %CD%\dir1\dir2\*

%CD% is how you get the current path in a Windows batch file, but it also works from the command line. More info about Capturing the current directory from a batch file.

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
3

As explained in related question in 7-zip user FAQ, 7z stores paths relative to working directory, so you will need to first cd to desired top-level directory for archive and run 7-zip from here.

cd dir1\dir2\
7z a -tzip  myzip.zip *

If you run it from script and don't want to affect it with changed directory, use directory push/pop facilities available in your shell of choice or run cd+7-zip in spawned process to avoid affecting your entire script with changed directory. For example, using Windows' start that would be:

start /D dir1\dir2\ /wait 7z a -tzip  myzip.zip *
Oleg V. Volkov
  • 21,719
  • 4
  • 44
  • 68
  • I am running this in a script and do not want to change directory. Is there no other way of doing the same? – Raam May 25 '12 at 11:50
  • You will have to, because that's how 7z works. Use directory push/pop facilities available in your shell of choice or run cd+7-zip in spawned process to avoid affecting your entire script with changed directory. – Oleg V. Volkov May 25 '12 at 11:52
  • thanks for the creative solution, but as Kathiravelu points out giving the full path does the trick. – Raam May 27 '12 at 03:23
  • This seems to be the only solution if you need to preserve a part of the path – yonojoy Feb 03 '14 at 01:53
1

This worked for me

Consider folder structure like C:\Parent\SubFolders..... And you want to create parent.zip which will contain all files and folders C:\Parent without parent folder [i.e it will start from SubFolders.....]

cd /D "C:\Parent"

"7z.exe" a Parent.zip "*.*" -r

This will create Parent.zip in C:\Parent