Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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...

share|improve this question

migrated from Oct 20 '10 at 8:11

This question came from our site for professional and enthusiast programmers.

There's a tool in the Windows resource kit called compress.exe. – ho1 Oct 20 '10 at 7:56
Here's a link to a ServerFault question that discusses just this:… – ho1 Oct 20 '10 at 7:57

PowerShell can do this

Add-Type -A System.IO.Compression.FileSystem
[IO.Compression.ZipFile]::CreateFromDirectory('foo', '')
[IO.Compression.ZipFile]::ExtractToDirectory('', 'bar')
share|improve this answer

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!

share|improve this answer

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

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) )
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
            objApp.NameSpace( target ).CopyHere objItem
        End If
        objApp.NameSpace( target ).CopyHere objItem
    End If

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

Set objItem = nothing
Set objFolder = nothing
Set objApp = nothing
Set objFSO = nothing
share|improve this answer
Looks promising, but I got an "Object required: 'objApp.NameSpace(...)' error on line 16 – pihentagy Nov 20 '12 at 8:55
I have the same problem. The above code doesn't work, or needs some extra stuff in the system the poster forgot to mention. – Shaamaan Feb 1 '13 at 11:13
It uses WScript (vbs environment) and several Shell objects. All were installed by default in Windows XP (business?) and Win 7 Ultimate x64. Perhaps they are longer there for Win8? – WSkid Feb 1 '13 at 18:25

Here is a great link that shows how to zip a file using windows native commands.

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

I tested it with a directory containing multiple nested files and folders and it worked perfectly. Just follow the format of the command line.

There is also a way to unzip the files via command line which I found as well. One way, just brings open an explorer window showing what the content of the zipped file is. Some of these also use Java which isn't necessarily native to windows but is so common that it nearly seems so.

Does Windows 7 have unzip at the command line installed by default?

share|improve this answer
You should include the instructions in your answer. If the links go stale, your answer will be worthless... – MoonSire Nov 15 '13 at 6:50

I will post something related to WSkids answer as sadly i cannot use the comment function.

Using the CopyHere() method in VBS introduces several issues. One of these issues is that the method returns immediately while the copy process starts in background whereas multiple CopyHere() calls will interfere each other and the ZIP won't be created correctly. A wait loop is needed here to fix that. My wait loop is based on an answer to a similar issue posted here.

Here is an updated version which fixes the "Object required" error reported by pihentagy. It's a timing issue as the newly created ZIP file is included in the Items collection when the script is executed on fast machines.

set Args = WScript.Arguments
source = Args(0)
' remove trailing slashes as we add slashes when needed later
while Right(source, 1) = "\"
    source = Mid(source, 1, Len(source) - 1)

target = Args(1)

' create empty ZIP file
set fso = CreateObject("Scripting.FileSystemObject")
set zip = fso.OpenTextFile(target, 2, vbtrue)
' write ZIP header, this ensures that Windows recognizes the file as "ZIP Folder"
zip.Write "PK" & Chr(5) & Chr(6) & String(18, Chr(0))
set zip = nothing
set fso = nothing

' copy files to ZIP file
set app = CreateObject("Shell.Application")

set sourceFolderObj = app.NameSpace(source)
set targetFolderObj = app.NameSpace(target)

for each item in sourceFolderObj.Items
  itemPath = source & "\" & item.Name

  copyItem = false

  ' ZIP file is included in Items collection and is recognized as folder, thus skip it to avoid script errors
  if itemPath <> target then
    if item.IsFolder then
      if item.GetFolder.Items().Count = 0 then
        ' folder is empty, skip it as empty folders can't be compressed
        copyItem = true
      end if
      copyItem = true
    end if
  end if

  if copyItem then
    targetFolderObj.CopyHere item

    ' wait until the file appears in the ZIP file, 
    ' this is needed because CopyHere() returns immediately after starting an asynchronous copy process 
    ' (starting multiple asynchronous copy will not work as it causes error messages, an invalid ZIP file, ...)
    while (targetFolderObj.ParseName(item.Name) is nothing)
      WScript.Sleep 1
  end If

set targetFolderObj = nothing
set sourceFolderObj = nothing
set app = nothing
share|improve this answer

Here'a my attempt to summarize built-in capabilities windows for compression and uncompression -

with a few given solutions that should work on almost every windows machine.

share|improve this answer
Please expand your answer - it relies much too heavily on a link that may die anytime. – studiohack May 18 '15 at 21:58

You must log in to answer this question.