Home » Questions » Computers [ Ask a new question ]

Can you zip a file from the command prompt using ONLY Windows' built-in capability to zip files?

Can you zip a file from the command prompt using ONLY Windows' built-in capability to zip files?

I have a batch file that outputs a text file. I thought it would be nice if I could zip it up too.

Asked by: Guest | Views: 238
Total answers/comments: 5
Guest [Entry]

"Here is an all batch file solution (a variation of my other answer) that will zip a file named c:\ue_english.txt and put it in 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

Write access is required to the parent of the folder stored in TEMPDIR. As this is often not the case for the root of drive C TEMPDIR may have to be changed.

Write access is also required for the folder the .bat script is in (as it generates a file there).

Also, please note that the file extension for the compressed file must be .zip. Attempts to use another extension may result in a script error. Instead, generate the .zip file and rename it."
Guest [Entry]

"If you are open to using PowerShell, zip capabilities are available in .NET 2.0 (PowerShell is .NET). Here's an a example (source) 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)}"
Guest [Entry]

"If you are able to install the Resource Kit Tools, you will find a command line tool called COMPRESS that can create compressed archive files like zip.

Microsoft ® File Compression Utility Version 5.00.2134.1
Copyright © Microsoft Corp. 1990-1999. All rights reserved.

Compresses one or more files.

COMPRESS [-r] [-d] [-z] Source Destination
COMPRESS -r [-d] [-z] Source [Destination]

-r Rename compressed files.
-d Update compressed files only if out of date.
-zx LZX compression.
-z MS-ZIP compression.
-zq[n] Quantum compression and optional level
(in range 1-7, default is 4).
Source Source file specification. Wildcards may be used.
Destination Destination file | path specification.
Destination may be a directory.
If Source is multiple files and -r is not specified,
Destination must be a directory."
Guest [Entry]

"'Keep script waiting until compression is done
Do Until objShell.NameSpace( ZipFile ).Items.Count = objShell.NameSpace( InputFolder ).Items.Count
WScript.Sleep 200
Loop"
Guest [Entry]

"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:

Compress-Archive
Expand-Archive

Special thanks to @Ramhound."