Zipping with an OLEObject

I’m trying to zip a file on Windows using an OLEObject and am not getting very far because of a strange error.

First, I am working off of this post:

https://stackoverflow.com/questions/19302634/creating-a-zip-then-copying-folders-to-it

I tried this code which is purely for testing. I’ve marked the error:

  dim sourceFolder as FolderItem = SpecialFolder.Desktop.Child( "xxtest" )
  if not sourceFolder.Directory then
    AddToResult "Source is not a directory!"
    return
  end if
  
  dim zipFile as FolderItem = SpecialFolder.Desktop.Child( "xxtest2.docx" )
  
  static nulls as string
  if nulls = "" then
    dim mb as new MemoryBlock( 18 )
    nulls = mb
  end if
  
  static pkHeader as string = "PK" + ChrB( 5 ) + ChrB( 6 ) + nulls
  
  dim objFSO as new OLEObject( "Scripting.FileSystemObject" )
  objFSO.OpenTextFile( zipFile.NativePath, 2, true ).Write( pkHeader )
  
  dim bsZip as BinaryStream = BinaryStream.Open( zipFile, true )
  bsZip.Position = 0
  bsZip.Write pkHeader
  bsZip.Close
  bsZip = nil
  
  zipFile = new FolderItem( zipFile.NativePath, FolderItem.PathTypeNative )
  
  dim sourceParams( 1 ) as variant
  sourceParams( 1 ) = sourceFolder.NativePath
  
  dim zipParams( 1 ) as variant
  zipParams( 1 ) = zipFile.NativePath
  
  dim copyHereOpts as integer = _
  kCopyHereOptionNoProgressDialog + _
  kCopyHereOptionYesToAll + _
  kCopyHereOptionNoDirectoryConfirmation + _
  kCopyHereOptionNoDialogOnError
  
  dim shellApp as new OLEObject( "Shell.Application" )
  dim source as OLEObject = shellApp.Invoke( "NameSpace", sourceParams )
  // Next line goes BOOM!
  dim zip as OLEObject = shellApp.Invoke( "NameSpace", zipParams )
  // OLEException: Exception, (failed on "NameSpace")
  zip.CopyHere( source.Items, copyHereOpts )
  
  Exception err as RuntimeException
    System.DebugLog Introspection.GetType( err ).Name + ": " + err.Message

Since the file is created, and has content, what am I doing wrong?

(Yes, I know I am writing to the file twice, but I wanted to show the different methods and combinations I’ve tried.)

It turns out Windows really wants that name to end in “.zip” and then it works. The last step is to (somehow) change the name of the file to end in “.docx” in my case, but anyone looking for native zipping, well, there it is.