96

On Mac/Linux to zip/unzip password protected zip files, I use: Zip:

zip -P password -r encrypted.zip folderIWantToZip

Unzip:

unzip -P password encrypted.zip

What are the equivalent command on Windows on the command line (assuming that 7zip has been installed)?

I have been doing research and found that it is not possible to password encrypt using the Java zip4j library. Also Windows does not have a zip command prompt like Mac/Linux

user3254893
  • 1,071
  • 2
  • 13
  • 14
  • http://sevenzip.sourceforge.jp/chm/cmdline/syntax.htm – ben75 Jan 26 '15 at 23:06
  • Your last sentence suggests that you might actually be wanting to do this from Java, which would be a programming question. But the question you actually asked is not, and belongs on SuperUser. – Ben Voigt Jan 26 '15 at 23:56
  • googlers... →please stop putting _any_ trust in zip password „protection“. It is **not**. – consider 7zip or maybe rar. – Frank N Sep 18 '17 at 08:47
  • Using `-P` is a security vulnerability on multi-user operating systems. It is recommended to use `-e` instead. – sunknudsen Aug 20 '20 at 12:48

4 Answers4

161

From http://www.dotnetperls.com:

7z a secure.7z * -pSECRET

Where:

7z        : name and path of 7-Zip executable
a         : add to archive
secure.7z : name of destination archive
*         : add all files from current directory to destination archive
-pSECRET  : specify the password "SECRET"

To open :

7z x secure.7z

Then provide the SECRET password

Note: If the password contains spaces or special characters, then enclose it with single quotes

7z a secure.7z * -p"pa$$word @|"
Gerard Rozsavolgyi
  • 4,834
  • 4
  • 32
  • 39
  • is it possible to provide the secret password when opening such that it can all be done as one command? I dont have a windows machine to test it currently – user3254893 Jan 27 '15 at 04:56
  • 1
    with a redirection: < you should be able to store the passwd in a file and launch the command like 7z x secure.7z < passwd.txt – Gerard Rozsavolgyi Jan 27 '15 at 07:09
  • 1
    great, I just implemented something just now using [RoboTask Lite](http://robotask.com/) to upload to the cloud (welll 7zip a file to a local folder, that is cloud synced) - by clicking a tray icon using these parameters. Before I was manually 7zipping, and getting very annoyed and having to rename files – ycomp Apr 13 '16 at 19:07
  • The example password in the final note on the command line is actually a bad choice since `$$` inside double quoted will extract to the current process id. Thus you pretty much don't know which password you gave. Same with single `$` which will treat the following characters as a variable name and most likely will be empty. Use single quotes instead like `-p 'pa$$word...' – Karsten S. Aug 15 '19 at 06:45
  • The last example doesn't work on windows because `'` is also included on the password and to make it work, I'd to use double quotes `"` – Pedro Lobito Jun 15 '20 at 03:02
  • 1
    I tried this and it created a regular archive that was not password protected. – BrainSlugs83 Nov 12 '20 at 19:40
  • That's strange... Did you provide the password as indicated ? or just try 7z a secure.7z * -p and you'll be prompted for a passwd – Gerard Rozsavolgyi Nov 12 '20 at 19:50
  • 2
    'then enclose it with single quotes' and you use regular quotes – J. Doe Feb 14 '22 at 11:42
  • It works as it should :-) Big thanks! – Tomasz Kuter Sep 09 '22 at 08:36
  • 3
    It's better to let terminal/console to ask you for the password to make sure special characters are captured without issues `7z a -p secure.7z *` – Pawel Cioch Nov 01 '22 at 03:09
  • yeah sure, but it's up to you if you want to provide passwd in commandline or not, thanks ! – Gerard Rozsavolgyi Nov 01 '22 at 09:08
45

General Syntax:

7z a archive_name target parameters

Check your 7-Zip dir. Depending on the release you have, 7z may be replaced with 7za in the syntax.

Parameters:

  • -p encrypt and prompt for PW.
  • -pPUT_PASSWORD_HERE (this replaces -p) if you want to preset the PW with no prompt.
  • -mhe=on to hide file structure, otherwise file structure and names will be visible by default.

Eg. This will prompt for a PW and hide file structures:

7z a archive_name target -p -mhe=on

Eg. No prompt, visible file structure:

7z a archive_name target -pPUT_PASSWORD_HERE

And so on. If you leave target blank, 7z will assume * in current directory and it will recurs directories by default.

thebunnyrules
  • 1,520
  • 15
  • 22
14

To fully script-automate:

Create:

7z -mhc=on -mhe=on -pPasswordHere a %ZipDest% %WhatYouWantToZip%

Unzip:

7z x %ZipFile% -pPasswordHere

(Depending, you might need to: Set Path=C:\Program Files\7-Zip;%Path% )

FearlessCoward
  • 327
  • 3
  • 6
  • 1
    What does mhc do? – scrollout Jul 14 '22 at 00:18
  • @scrollout according to 7Zip doc "Enables or disables archive header compressing. The default mode is hc=on. If archive header compressing is enabled, the archive header will be compressed with LZMA method. " – Helder Oct 22 '22 at 19:52
1

I'm maybe a little bit late but I'm currently trying to develop a program which can brute force a password protected zip archive. First I tried all commands I found in the internet to extract it through cmd... But it never worked....Every time I tried it, the cmd output said, that the key was wrong but it was right. I think they just disenabled this function in a current version.

What I've done to Solve the problem was to download an older 7zip version(4.?) and to use this for extracting through cmd.

This is the command: "C:/Program Files (86)/old7-zip/7z.exe" x -pKey "C:/YOURE_ZIP_PATH"

The first value("C:/Program Files (86)/old7-zip/7z.exe") has to be the path where you have installed the old 7zip to. The x is for extract and the -p For you're password. Make sure you put your password without any spaces behind the -p! The last value is your zip archive to extract. The destination where the zip is extracted to will be the current path of cmd. You can change it with: cd YOURE_PATH

Now I let execute this command through java with my password trys. Then I check the error output stream of cmd and if it is null-> then the password is right!

JustCoding
  • 61
  • 6
  • general info: if you need to download the 7z program then download it from here https://www.7-zip.org/download.html it will default install it here: C:\Program Files\7-Zip – serup Jan 22 '19 at 06:12