Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 stackoverflow.com Oct 20 '10 at 8:11

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

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

3 Answers

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
add comment

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
share|improve this answer
1  
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
add comment

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?

http://stackoverflow.com/questions/1021557/how-to-unzip-a-file-using-the-command-line

share|improve this answer
1  
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.