Compressing only files using 7z without preserving the path

前端 未结 5 682
梦谈多话
梦谈多话 2021-02-03 18:57

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



        
相关标签:
5条回答
  • 2021-02-03 19:35

    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 *
    
    0 讨论(0)
  • 2021-02-03 19:40

    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.

    0 讨论(0)
  • 2021-02-03 19:48

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

    0 讨论(0)
  • 2021-02-03 19:52

    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

    0 讨论(0)
  • 2021-02-03 19:53

    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.

    0 讨论(0)
提交回复
热议问题