7-Zip command to create and extract a password-protected ZIP file on Windows? [closed]
Asked Answered
S

4

100

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

Shayne answered 26/1, 2015 at 22:54 Comment(4)
sevenzip.sourceforge.jp/chm/cmdline/syntax.htmCatheycathi
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.Corncob
googlers... →please stop putting any trust in zip password „protection“. It is not. – consider 7zip or maybe rar.Blender
Using -P is a security vulnerability on multi-user operating systems. It is recommended to use -e instead.Siva
M
167

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 quotes

7z a secure.7z * -p"pa$$word @|"
Mello answered 26/1, 2015 at 23:7 Comment(14)
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 currentlyShayne
with a redirection: < you should be able to store the passwd in a file and launch the command like 7z x secure.7z < passwd.txtMello
great, I just implemented something just now using RoboTask Lite 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 filesRice
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...'Henni
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 "Subtonic
I tried this and it created a regular archive that was not password protected.Goatee
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 passwdMello
'then enclose it with single quotes' and you use regular quotesHauberk
It works as it should :-) Big thanks!Excrement
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 *Crosscheck
yeah sure, but it's up to you if you want to provide passwd in commandline or not, thanks !Mello
Pipe the password instead of specifying it on the command-line if you want to keep the password out of command history and the process tree (htop, ps, etc): printf '%s' "$PASSWORD" | 7zz x test.zip (see this post)Hui
Yes sure @Hui that's a good idea. Password can also be read from an env var containing the password.Mello
I don't think 7zz can read the password from an environment variable. You can pass an environment variable to the -p flag on the command-line, but this will be visible in the process tree to all users and processes on the system. If you pipe the environment variable to stdin using printf, it will only be visible to the current user and root. See this answer for details.Hui
M
48

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.

Myranda answered 26/1, 2017 at 4:7 Comment(1)
Same command structure works on linux if anyone wants to know.Proust
N
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% )

Nomadize answered 3/10, 2016 at 7:39 Comment(2)
What does mhc do?Seventeen
@Seventeen 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. "Haase
U
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!

Unruly answered 12/4, 2015 at 8:8 Comment(1)
general info: if you need to download the 7z program then download it from here 7-zip.org/download.html it will default install it here: C:\Program Files\7-ZipVow

© 2022 - 2024 — McMap. All rights reserved.