120

I have the following folder structure:

  • root
    • folder1
    • folder2
    • folder3
    • 7za.exe

I want to run the 7-zip command line tool to compress all the files in folder1 to a zip file called folder1.zip.

Running the following

7za.exe a -tzip folder1.zip folder1\\*.*

produces a zip file as expected. However, when I open the zip file, it has a folder in it called folder1, and inside that I have all the files that were inside that folder. I don't want the folder name added to the zip folder, i.e. I would like to add all the files in a "Flat" file format.

I also don't want to recursively run the command line tool for each individual file/folder.

Is there a switch that provides this functionality?

robinCTS
  • 4,327
  • 4
  • 20
  • 29
gg.
  • 1,303
  • 2
  • 8
  • 4
  • Can you `cd` into folder1 and `7za.exe a -tzip ..\folder1.zip *.*`? – zpletan Sep 26 '11 at 17:09
  • He can, but then 7za.exe is not inside that folder, so running the original command won't work because it won't be able to find 7za.exe. Maybe using `start ..\7za.exe` might fix that but it's messy and 7-Zip should be able to do this, without having to do that. – bat_cmd Sep 14 '22 at 13:08

4 Answers4

188

From the 7-Zip Help file:

a (Add) command

Adds files to archive.

Examples

7z a archive1.zip subdir\

adds all files and subfolders from folder subdir to archive archive1.zip. The filenames in archive will contain subdir\ prefix.

7z a archive2.zip .\subdir\*

adds all files and subfolders from folder subdir to archive archive2.zip. The filenames in archive will not contain subdir\ prefix.

cd /D c:\dir1\

7z a c:\archive3.zip dir2\dir3\

The filenames in archive c:\archive3.zip will contain dir2\dir3\ prefix, but they will not contain c:\dir1\ prefix.

So the command you'd want would be: 7za.exe a folder1.zip .\folder1\*

Also, pay attention to 7-Zip's handling of wildcards. It doesn't treat *.* as "all files" -- it means "all files with a period in the filename." Extension-less files will be missed. If you really want all files, just use * instead.

Finally, the -tzip parameter isn't needed if the archive filename ends in .zip. 7-Zip is smart enough to figure out which format you want in those cases. It's only required when you want a custom extension (e.g. 7za.exe a -tzip foo.xpi <files> for a Mozilla Add-on).

afrazier
  • 22,987
  • 3
  • 60
  • 88
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

  • 3
    This duplicates another answer and adds no new content. Please don't post an answer unless you actually have something new to contribute. – DavidPostill Jun 07 '16 at 20:38
  • Well while I was searching for solution I didn’t get exact this solution. Obviously I will not just copy paste some answers I found online. I spend some time on it so I felt I should share it. As per me you can find this exact answer here and on Stack Overflow posted by me. – Pritesh Dhokchaule Jun 09 '16 at 04:54
  • 1
    Note that 7-Zip does not consider `*.*` to mean "all files" but rather "all files that have a period as a part of the filename." So this answer will actually omit files that do not have an extension (this is noted in the accepted answer). – Bill_Stewart Feb 25 '19 at 19:33
1

Just to expand on the accepted answer (I was not able to add comment there):

On Linux adding '*' didn't work for me, so I ended up concocting more verbose command line which gave desired result:

curr_dir=$(pwd); \ 
cd source_code/lambda/ ; \
7z a ../../lambda.zip .; \
cd $curr_dir \
unset curr_dir

===========

script explanation:

  • save current dir path to use later;
  • navigate to directory which needs to be archived;
  • create archive (notice '.' (dot) in the end);
  • go back to the original dir;
  • get rid of the variable which stored original dir path

I hope it might be useful for somebody.

Toto
  • 17,001
  • 56
  • 30
  • 41
Humanier
  • 11
  • 1
-5

How about this. e.g. Folder/subFolder1, Folder/subfolder2

select all subfolder > rightclick then create archive > select whatever format

*The archive name would be the parent folder..

Simple right?

  • 6
    The question is about the CLI for 7-zip, not how to use the GUI. Learning how to use the GUI is pointless when you want to automate things. – binki Mar 17 '17 at 21:18