After prowling google, and checking an old thread in ServerFault, I thought I would try here. Without any additional downloads, on a fresh install of XP SP3, how can I run a script to extract AND zip a file into a .zip?
People have tried saying to use third party utilities, but that requires downloading a program as well, and making sure they're in the same location all the time. I don't want to download the Microsoft Resource Kit just for this one chunk of functionality on multiple computers
答案1
You can do this with VBScript. This question has been asked on Stack Overflow, and this answer comes from Jay:
Dim fso, winShell, MyTarget, MySource, file
Set fso = CreateObject("Scripting.FileSystemObject")
Set winShell = createObject("shell.application")
MyTarget = Wscript.Arguments.Item(0)
MySource = Wscript.Arguments.Item(1)
Wscript.Echo "Adding " & MySource & " to " & MyTarget
'create a new clean zip archive
Set file = fso.CreateTextFile(MyTarget, True)
file.write("PK" & chr(5) & chr(6) & string(18,chr(0)))
file.close
winShell.NameSpace(MyTarget).CopyHere winShell.NameSpace(MySource).Items
do until winShell.namespace(MyTarget).items.count = winShell.namespace(MySource).items.count
wscript.sleep 1000
loop
Set winShell = Nothing
Set fso = Nothing
答案2
There is a single, simple cmd.exe command for this. (Through PowerShell v5.0+)
To zip:
powershell Compress-Archive -LiteralPath 'C:\mypath\testfile.txt' -DestinationPath "C:\mypath\Test.zip"
To unzip:
powershell Expand-Archive -LiteralPath "C:\mypath\Test.Zip" -DestinationPath "C:\mypath" -Force
Sources:
Special thanks to @Ramhound
答案3
I believe that Windows XP does have a built in function for this called Compact.
答案4
To do this on Windows XP without installation of VB runtime and other components, I would use 7zip's command line tools 7za.exe and copy the required files to windows/system32 to save having to PATH to the 7zip installation directory. 7za can zip up as a zip file instead of a .7z file to aid compatibility. Futher information here https://www.7-zip.org/faq.html