![creating a zip folder creating a zip folder](https://www.lifewire.com/thmb/BUUZ4mmQhxMejDkdjl0FT4Z-Q-8=/960x640/filters:no_upscale():max_bytes(150000):strip_icc()/rename-zip-file-windows-10-5b802b28c9e77c00505d73de.png)
With this code you can use the Windows Shell API in C# to compress Zip files and do so without having to show the Copy Progress window shown above. This is a follow up article to the one that I wrote about decompressing Zip files. I have copied the article text below as written (original license: public domain) Compress Zip files with Windows Shell API and C Net 4.5 or later this is built-in to the frameworkįor earlier versions or for more control you can use Windows' shell functions as outlined here on CodeProject by Gerald Gibson Jr. While (targetFolderObj.ParseName(item.Edit: if you're using. ' (starting multiple asynchronous copy will not work as it causes error messages, an invalid ZIP file. ' this is needed because CopyHere() returns immediately after starting an asynchronous copy process ' wait until the file appears in the ZIP file, ' folder is empty, skip it as empty folders can't be compressed
#CREATING A ZIP FOLDER ZIP FILE#
' ZIP file is included in Items collection and is recognized as folder, thus skip it to avoid script errors Set targetFolderObj = app.NameSpace(target) Set sourceFolderObj = app.NameSpace(source) Set app = CreateObject("Shell.Application") ' write ZIP header, this ensures that Windows recognizes the file as "ZIP Folder" Set zip = fso.OpenTextFile(target, 2, vbtrue) Set fso = CreateObject("Scripting.FileSystemObject") ' remove trailing slashes as we add slashes when needed later
![creating a zip folder creating a zip folder](https://i.stack.imgur.com/3R2Wn.png)
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. Here is an updated version which fixes the "Object required" error reported by pihentagy. My wait loop is based on an answer to a similar issue posted here. 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.
![creating a zip folder creating a zip folder](https://thesweetsetup.com/wp-content/uploads/2017/10/zipped-01.jpg)
Using the CopyHere() method in VBS introduces several issues. I will post something related to WSkids answer as sadly i cannot use the comment function. ' delay until at least items at the top level are availableĭo Until objApp.NameSpace( target ).Items.Count + intSkipped = intSrcItems IntSrcItems = objApp.NameSpace( source ).Items.Count ObjApp.NameSpace( target ).CopyHere objItem ' if this folder is empty, then skip it as it can't compress empty folders Set objFolder = objFSO.GetFolder( objItem.Path ) ' Loop over items within folder and use CopyHere to put them into the zip folderįor Each objItem in objApp.NameSpace( source ).Items Set objApp = CreateObject( "Shell.Application" ) ' this is the header to designate a file as a zip Set zip = objFSO.OpenTextFile(target, 2, vbtrue) Set objFSO = CreateObject("Scripting.FileSystemObject")
![creating a zip folder creating a zip folder](https://www.wikihow.com/images/6/6c/1376283-8b4.jpg)
Here is the vbs code - Set Args = Wscript.Arguments It won't copy empty folders so be careful. Path to script, source folder, zip file to make (include. Usage: in the run box or command line put- "C:\zipper.vbs" "C:\folderToZip\" "C:\mynewzip.zip"
#CREATING A ZIP FOLDER WINDOWS 7#
The script was originally made for Windows XP, but it also works in Windows 7 圆4 Ultimate - no guarantee's if Windows will keep around the various Shell objects this uses. Copy and paste the script into a file with the extension ".vbs". I've combined this script from several different sources to suit my needs better.