通过cmd命令解压文件(7-zip)

88
我尝试通过 CMD 解压文件。因此,我安装了 WinZip(以及其与 CMD 的插件)、WinRAR 和 7-Zip。但是,当我尝试通过 CMD 执行命令时: execute a command
7z e myzip.zip

它给出了以下错误:

7z is not recognized as an internal or external command
此外,我将7-z文件夹添加到环境变量中(属性-->高级-->环境变量-->用户变量-->选择路径,然后添加C:\Program Files\7-Zip)。
可能的原因是什么?

1
我相信你缺少了作为命令行工具使用的7za.exe(请参见http://www.7-zip.org/download.html,命令行版本)。 - Najzero
你可能是想使用7z x而不是e... - Yordan Georgiev
此外,如果设置环境变量和复制粘贴 .exe 文件后仍无法正常工作,请尝试重新启动您的 bash。 - vytaute
4个回答

135
在命令提示符中执行以下操作对我有效,将其添加到我的用户环境变量中也可以正常工作:
set PATH=%PATH%;C:\Program Files\7-Zip\
echo %PATH%
7z

你应该能够看到输出(或类似的内容——因为这是在我的运行Windows 7的笔记本电脑上):
C:\Users\Phillip>set PATH=%PATH%;C:\Program Files\7-Zip\

C:\Users\Phillip>echo %PATH%
C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Wi
ndows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\
WirelessCommon\;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\To
ols\Binn\;C:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Fil
es (x86)\QuickTime\QTSystem\;C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Notepad+
+;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files\7-Zip\

C:\Users\Phillip>7z

7-Zip [64] 9.20  Copyright (c) 1999-2010 Igor Pavlov  2010-11-18

Usage: 7z <command> [<switches>...] <archive_name> [<file_names>...]
       [<@listfiles...>]

<Commands>
  a: Add files to archive
  b: Benchmark
  d: Delete files from archive
  e: Extract files from archive (without using directory names)
  l: List contents of archive
  t: Test integrity of archive
  u: Update files to archive
  x: eXtract files with full paths
<Switches>
  -ai[r[-|0]]{@listfile|!wildcard}: Include archives
  -ax[r[-|0]]{@listfile|!wildcard}: eXclude archives
  -bd: Disable percentage indicator
  -i[r[-|0]]{@listfile|!wildcard}: Include filenames
  -m{Parameters}: set compression Method
  -o{Directory}: set Output directory
  -p{Password}: set Password
  -r[-|0]: Recurse subdirectories
  -scs{UTF-8 | WIN | DOS}: set charset for list files
  -sfx[{name}]: Create SFX archive
  -si[{name}]: read data from stdin
  -slt: show technical information for l (List) command
  -so: write data to stdout
  -ssc[-]: set sensitive case mode
  -ssw: compress shared files
  -t{Type}: Set type of archive
  -u[-][p#][q#][r#][x#][y#][z#][!newArchiveName]: Update options
  -v{Size}[b|k|m|g]: Create volumes
  -w[{path}]: assign Work directory. Empty path means a temporary directory
  -x[r[-|0]]]{@listfile|!wildcard}: eXclude filenames
  -y: assume Yes on all queries

14
为避免每次使用时都需要这样做,您可以使用 setx 命令: setx PATH "%PATH%;C:\Program Files\7-Zip\"。然后重新启动 CMD(或使用原始的 set 命令也可以将其设置为当前会话)。 - Ewout
@Ewout 对我来说,这第一次“确信”地证明添加到 PATH 通常意味着添加到 用户路径,而不是 系统路径,因为 setx 正在添加到 用户路径。这可能对每个人都很清楚,但即使在多次更改 环境变量 后,我仍然不知道它。 - questionto42
1
我的7zip文件夹包括一个Uninstall.exe,我认为它也会将其显示在路径中,以及7zFM和7zG。我建议您将命令行工具与其他工具隔离开来。 - AgentM

8

关于Phil Street的帖子:

如果您正在运行64位操作系统,它实际上可能安装在32位程序文件夹中,而不是默认的x64文件夹。请检查7-zip的安装位置,如果它在Program Files (x86)中,则尝试使用此位置代替。

PATH=%PATH%;C:\Program Files (x86)\7-Zip

1
确保您的路径指向C:\Program Files\7-Zip中的.exe文件(可能在bin目录中)。

3
不,它应该指向 "C:\Program Files\7-Zip" 文件夹。然后系统将从那里读取所有的 "exe" 文件,并使其在命令行中可用(确保关闭当前的命令行并启动新的,因为更改直到重新启动这些命令行后才可用)。 - Beemo

0
在Windows 10中,我必须以管理员身份运行批处理文件。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接