2010-02-19 19:46:23 +0000 2010-02-19 19:46:23 +0000
113
113
Advertisement

能否在命令提示符中只使用Windows内置的压缩文件功能,从命令提示符中压缩一个文件?

Advertisement

我有一个批处理文件,输出一个文本文件。

这将在一个不受控制的环境中使用,所以我不能假设有第三方软件产品,如7-Zip等。这就需要利用Windows现在内置的功能来压缩文件。

Advertisement
Advertisement

答案 (12)

86
86
86
2010-02-22 17:28:45 +0000

这里是一个全批处理文件的解决方案(我的另一个答案的变体),它将压缩一个名为c:\ue_english.txt的文件,并将其放到C:\someArchive.zip中:

set FILETOZIP=c:\ue_english.txt

set TEMPDIR=C:\temp738
rmdir %TEMPDIR%
mkdir %TEMPDIR%
xcopy /s %FILETOZIP% %TEMPDIR%

echo Set objArgs = WScript.Arguments > _zipIt.vbs
echo InputFolder = objArgs(0) >> _zipIt.vbs
echo ZipFile = objArgs(1) >> _zipIt.vbs
echo CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" ^& Chr(5) ^& Chr(6) ^& String(18, vbNullChar) >> _zipIt.vbs
echo Set objShell = CreateObject("Shell.Application") >> _zipIt.vbs
echo Set source = objShell.NameSpace(InputFolder).Items >> _zipIt.vbs
echo objShell.NameSpace(ZipFile).CopyHere(source) >> _zipIt.vbs
echo wScript.Sleep 2000 >> _zipIt.vbs

CScript _zipIt.vbs %TEMPDIR% C:\someArchive.zip

pause

写入权限需要对存储在TEMPDIR中的父文件夹进行写入。

还需要对TEMPDIR脚本所在的文件夹进行写入访问(因为它在那里生成一个文件)。试图使用其他扩展名可能会导致脚本错误。相反,生成.bat文件并重命名。

40
40
40
2010-02-20 15:48:33 +0000

可以在不安装任何额外软件的情况下压缩文件(我已经测试过了)。解决方案是:

在命令行窗口中运行这个命令,创建一个名为C:\someArchive.zip的ZIP文件,包含C:\test3文件夹中的所有文件:

CScript zip.vbs C:\test3 C:\someArchive.zip

zip.vbs,其中文件包含:

' Get command-line arguments.
Set objArgs = WScript.Arguments
Set FS = CreateObject("Scripting.FileSystemObject")
InputFolder = FS.GetAbsolutePathName(objArgs(0))
ZipFile = FS.GetAbsolutePathName(objArgs(1))

' Create an empty ZIP file.
CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)

Set objShell = CreateObject("Shell.Application")

Set source = objShell.NameSpace(InputFolder).Items

objShell.NameSpace(ZipFile).CopyHere(source)

' Required to let the ZIP command execute
' If this script randomly fails or the ZIP file is not complete,
' just increase to more than 2 seconds
wScript.Sleep 2000

我还没有测试过路径和文件名中包含空格的情况。


它是如何工作的:Windows(Windows XP和更高版本的?)内置的ZIP功能是通过Windows shell的COM接口暴露出来的,即 “Shell.Application "部分。这个COM接口可以从VBScript脚本中使用,因为这样的脚本可以访问COM组件。为了使该脚本完全独立,它创建了一个空的ZIP文件来启动(也可以创建一个空的ZIP文件,然后将其与VBScript脚本一起复制到目标系统中)。Windows Script Host是在Windows 98和更高版本的Windows中默认分发和安装的。如果安装了Internet Explorer 5(或以后的版本),它也会被安装。

12
Advertisement
12
12
2010-02-19 21:59:36 +0000
Advertisement

如果你愿意使用PowerShell**,zip功能可以在.NET 2.0中使用(PowerShell是.NET)。下面是一个例子源码) credit to Mike Hodnick:

########################################################
# out-zip.ps1
#
# Usage:
# To zip up some files:
# ls c:\source\*.txt | out-zip c:\target\archive.zip $_
#
# To zip up a folder:
# gi c:\source | out-zip c:\target\archive.zip $_
########################################################

$path = $args[0]
$files = $input

if (-not $path.EndsWith('.zip')) {$path += '.zip'} 

if (-not (test-path $path)) { 
  set-content $path ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18)) 
} 

$ZipFile = (new-object -com shell.application).NameSpace($path) 
$files | foreach {$zipfile.CopyHere($_.fullname)}
7
7
7
2011-11-23 00:47:36 +0000

你可以通过轮询压缩对话框窗口的存在来消除压缩过程中的超时风险。这种方法还可以处理用户取消压缩窗口的退出。

objShell.NameSpace(ZipFile).CopyHere(source)

' Wait for compression window to open
set scriptShell = CreateObject("Wscript.Shell")
Do While scriptShell.AppActivate("Compressing...") = FALSE   
   WScript.Sleep 500 ' Arbitrary polling delay
Loop  

' Wait for compression to complete before exiting script
Do While scriptShell.AppActivate("Compressing...") = TRUE   
   WScript.Sleep 500 ' Arbitrary polling delay
Loop
5
Advertisement
5
5
2010-02-19 20:29:14 +0000
Advertisement

如果你能够安装资源包工具,你会发现一个名为compress的命令行工具,可以创建压缩后的压缩存档文件,如zip。

3
3
3
2012-08-07 07:46:33 +0000
'Keep script waiting until compression is done
Do Until objShell.NameSpace( ZipFile ).Items.Count = objShell.NameSpace( InputFolder ).Items.Count
    WScript.Sleep 200
Loop
2
Advertisement
2
2
2017-06-07 21:53:59 +0000
Advertisement

这是对公认答案的一个变种。我一次要做的自动化任务多达上千个文件,所以不能只睡2秒就不管不顾。我搜集到的工作方法这里,也是类似于Jiří Kočara的答案这里

2
2
2
2012-11-05 21:06:59 +0000

多个文件/目录的简化代码。

cscript zip.vbs target.zip sourceFile1 sourceDir2 ... sourceObjN

zip.vbs文件

Set objArgs = WScript.Arguments
ZipFile = objArgs(0)

' Create empty ZIP file and open for adding
CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)
Set zip = CreateObject("Shell.Application").NameSpace(ZipFile)

' Add all files/directories to the .zip file
For i = 1 To objArgs.count-1
  zip.CopyHere(objArgs(i))
  WScript.Sleep 10000 'REQUIRED!! (Depending on file/dir size)
Next
1
Advertisement
1
1
2015-01-22 12:31:49 +0000
Advertisement

以下是我对windows内置的压缩和解压功能的总结 - https://stackoverflow.com/questions/28043589/how-can-i-compres-zip-and-uncopress-unzip-files-and-folders-with-batch-f

,并给出了一些解决方案,几乎每台windows机器上都可以使用。

1
1
1
2019-08-20 13:24:16 +0000

Windows 10 build 17063 或更高版本的 Windows 10 捆绑了 tar.exe,它可以从命令行中使用 zip 工作。

1
1
1
2016-08-02 11:19:52 +0000

如果在Windows 8或Windows Server 2012上,你会有PowerShell和.NET 4.5,所以你可以这样做:

zip.ps1*(用法:-directory <directory to zip up> -name <zip name>):

param (
    [string]$directory,
    [string]$name
)

Add-Type -Assembly System.IO.Compression.FileSystem
[System.IO.Compression.ZipFile]::CreateFromDirectory($directory, $name, [System.IO.Compression.CompressionLevel]::Optimal, $false)

zip.bat*(如果你需要一个帮手帮你调用PowerShell,目录是第一个参数,zip名是第二个):

@Echo Off
powershell -ExecutionPolicy ByPass -Command "& '%~dpn0.ps1' -directory '%1' -name '%2'"
-4
-4
-4
2013-05-31 19:54:34 +0000

Windows的命令行现在提供了紧凑型命令,据我所知,它是Windows的本地命令。这应该可以满足要求,除非我漏掉了什么。

Advertisement

相关问题

3
19
10
28
4
Advertisement