Skip to content

Using the Zip & Unzip Feature

FolderItem has been extended with new methods to Zip and Unzip files and folders in Xojo 2023 Release 3. Take a peek as we look at how easy it is to use this new feature.

What is New?

Xojo 2023r3 adds new FolderItem.Zip and FolderItem.Unzip methods with support for the most widely used DEFLATE compression format.

  • Zip(destination As FolderItem, contentsOnly As Boolean = False, compression As ZipCompressions = ZipCompressions.Normal) As FolderItem
  • Zip(contentsOnly As Boolean = False, compression As ZipCompressions = ZipCompressions.Normal) As FolderItem
  • Unzip()
  • Unzip(destinationFolder As FolderItem)

How to Zip

Here’s an example of zipping SomeFolder on your Desktop:

Var someFolder As FolderItem = SpecialFolder.Desktop.Child("SomeFolder")
Var outputFile As FolderItem = someFolder.Zip

The outputFile will be created on your Desktop with the name “SomeFolder.zip”.
This zip file will contain a single folder called SomeFolder, along with the contents of that folder.

+- SomeFolder.zip
  +- SomeFolder
    +- File1.ext
    +- File2.ext
    +- etc.

If you’d rather just zip up the contents of SomeFolder then you can pass True to the contentsOnly parameter.

Var outputFile As FolderItem = myFolder.Zip(True)

Now the zip file only contains the contents of the SomeFolder folder.

+- SomeFolder.zip
  +- File1.ext
  +- File2.ext
  +- etc.

Note: on macOS, when the OS unzips this kind of zip folder hierarchy, it will automatically create a folder with the contents, regardless of what the actual zip hierarchy looks like. However, this does not affect our own FolderItem.Unzip method.

With the overloaded Zip method that takes a destination FolderItem, you can use this to specify where the zip file is created.

Var destinationFolder As FolderItem = SpecialFolder.Documents
Var outputFile As FolderItem = myFolder.Zip(destinationFolder)

In this case the outputFile is created in the user’s Documents folder. If you wanted to rename it to something other than the default name with a “.zip” appended to it, you can also use this syntax to pass a destination file instead of a destination folder.

Var destinationFile As FolderItem = SpecialFolder.Desktop.Child("MyZipFile.zext")
Var outputFile As FolderItem = myFolder.Zip(destinationFile)

While we only demonstrated how to zip a folder in these examples, the exact same syntax applies for zipping a single FolderItem file too.

How to Unzip

Conversely, if you wanted to unzip a file, this is a simple call to FolderItem.Unzip

outputFile.Unzip

This unzips the outputFile contents to the same directory that the outputFile resides in. If you wanted to unzip the contents to a different location, pass in a destination folder instead:

outputFile.Unzip(SpecialFolder.Temporary)

This feature is not yet supported in Android. In conclusion, Zipping/Unzipping has never been easier! Learn more in the Xojo Programming Documentation – Zip and UnZip. (Thanks in part to zlib)

William Yu grew up in Canada learning to program BASIC on a Vic-20. He is Xojo’s resident Windows and Linux engineer, among his many other skills. Some may say he has joined the dark side here in the USA, but he will always be a Canadian at heart.