Compressing only files using 7z without preserving the path

白昼怎懂夜的黑 提交于 2019-12-03 08:12:33

问题


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


回答1:


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.




回答2:


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




回答3:


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.




回答4:


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 *



回答5:


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



来源:https://stackoverflow.com/questions/10753667/compressing-only-files-using-7z-without-preserving-the-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!