Create .zip folder from the command line - (Windows)
Is it possible to create a .zip file from a folder in the command line, I don't want to use any third party executable.
I was thinking something like 'send to compressed folder' but I don't know how to do it...
Tar
Windows 10 includes tar.exe:
# example 1
tar.exe -a -c -f out.zip in.txt
# example 2
tar.exe -x -f out.zip
If you have older Windows, you can still download from libarchive/libarchive.
PowerShell
PowerShell has Compress-Archive:
# example 1
Compress-Archive in.txt out.zip
# example 2
Expand-Archive out.zip
Directory
For both tools, you can use a file or directory for the input.
Imagine that you want to compress the same folder that you are on Command Prompt WITHOUT opening a powershell window:
powershell Compress-Archive . publish.zip
I don't think there is a command line for ZIP files built in to Windows (Other than compress
in Server 2003 Resource Kit). You'd have to use a third party. Everybody loves 7zip!
I've combined this script from several different sources to suit my needs better. Copy and paste the script into a file with the extension ".vbs". The script was originally made for Windows XP, but it also works in Windows 7 x64 Ultimate - no guarantee's if Windows will keep around the various Shell objects this uses.
Usage: in the run box or command line put-
"C:\zipper.vbs" "C:\folderToZip\" "C:\mynewzip.zip"
Path to script, source folder, zip file to make (include .zip at the end).
It won't copy empty folders so be careful.
Here is the vbs code ---
Set Args = Wscript.Arguments
source = Args(0)
target = Args(1)
' make sure source folder has \ at end
If Right(source, 1) <> "\" Then
source = source & "\"
End If
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set zip = objFSO.OpenTextFile(target, 2, vbtrue)
' this is the header to designate a file as a zip
zip.Write "PK" & Chr(5) & Chr(6) & String( 18, Chr(0) )
zip.Close
Set zip = nothing
wscript.sleep 500
Set objApp = CreateObject( "Shell.Application" )
intSkipped = 0
' Loop over items within folder and use CopyHere to put them into the zip folder
For Each objItem in objApp.NameSpace( source ).Items
If objItem.IsFolder Then
Set objFolder = objFSO.GetFolder( objItem.Path )
' if this folder is empty, then skip it as it can't compress empty folders
If objFolder.Files.Count + objFolder.SubFolders.Count = 0 Then
intSkipped = intSkipped + 1
Else
objApp.NameSpace( target ).CopyHere objItem
End If
Else
objApp.NameSpace( target ).CopyHere objItem
End If
Next
intSrcItems = objApp.NameSpace( source ).Items.Count
wscript.sleep 250
' delay until at least items at the top level are available
Do Until objApp.NameSpace( target ).Items.Count + intSkipped = intSrcItems
wscript.sleep 200
Loop
'cleanup
Set objItem = nothing
Set objFolder = nothing
Set objApp = nothing
Set objFSO = nothing
It is possible to run PowerShell script from BAT. Bat file receive path to dir to be zipped and zip file name as parameters.
@echo off
setlocal
rem First parameter - path to dir to be zipped
rem Second parameter- zip file name
set sourceDir=%1
set zipFile=%2
rem Create PowerShell script
echo Write-Output 'Custom PowerShell profile in effect!' > %~dp0TempZipScript.ps1
echo Add-Type -A System.IO.Compression.FileSystem >> %~dp0TempZipScript.ps1
echo [IO.Compression.ZipFile]::CreateFromDirectory('%sourceDir%','%~dp0%zipFile%') >> %~dp0TempZipScript.ps1
rem Execute script with flag "-ExecutionPolicy Bypass" to get around ExecutionPolicy
PowerShell.exe -ExecutionPolicy Bypass -Command "& '%~dp0TempZipScript.ps1'"
del %~dp0TempZipScript.ps1
endlocal