Compressing only files using 7z without preserving

2019-03-14 07:23发布

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

5条回答
做自己的国王
2楼-- · 2019-03-14 08:00

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

查看更多
别忘想泡老子
3楼-- · 2019-03-14 08:07

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.

查看更多
何必那么认真
4楼-- · 2019-03-14 08:11

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.

查看更多
三岁会撩人
5楼-- · 2019-03-14 08:11

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 *
查看更多
够拽才男人
6楼-- · 2019-03-14 08:13

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

查看更多
登录 后发表回答