Zip/unzip files and folders with these commands

Before exchanging files (by email for example), we often compress them to reduce their size before transmitting them. With 4D v18, you can compress and uncompress your files by programming without the need for external libraries or tools. Here are the new commands that allow you to do so:

HDI Example

Compress files and folders

The new Zip Create archive command allows you to create a zip archive by passing either a file, a folder, or an object with parameters (e.g., a password for reading the archive).

Compress a file:

$destination:=Folder(fk desktop folder).file("MyDocs/")
$file:=Folder(fk desktop folder).file("MyDocs/text.txt")
ZIP Create archive($file;$destination)

Compress a folder:

$destination:=Folder(fk desktop folder).file("MyDocs/")
$folder:=Folder(fk desktop folder).folder("MyDocs/Images")
ZIP Create archive($folder;$destination)

Compress with a password and a progress bar:

$destination:=Folder(fk desktop folder).file("MyDocs/")
$zip:=New object
$zip.files:=Folder(fk desktop folder).folder("MyDocs/Resources").folders()
$zip.callback:=Formula(FormulaCompressing ($1))

progID:=Progress New
ZIP Create archive($zip;$destination)
Progress QUIT (progID)

The FormulaCompressing method:

Progress SET PROGRESS (progID;Num($1/100))

Uncompress files and folders

A new ZIP Read archive command returns an archive object. By manipulating this object, you can easily obtain the list of files within the archive, extract a particular file, or extract the entire archive, etc.

Read the content of archive

$path:=Folder(fk desktop folder).file("MyDocs/")
$archive:=ZIP Read archive($path)

Retrieve the list of files and folders


Read the content of a file without extraction

If ($files[$i].extension=".txt")
End if

Extract file from archive

$folderResult:=$files[$i].copyTo(Folder(fk desktop folder).folder("MyDocs"))

Extract all files

$folderResult:=$archive.root.copyTo(Folder(fk desktop folder).folder("MyDocs"))

Vanessa Talbot
• Product Owner •Vanessa Talbot joined 4D Program team in June, 2014. As a Product Owner, she is in charge of writing the user stories then translating it to functional specifications. Her role is also to make sure that the feature implementation delivered is meeting the customer need.Since her arrival, she has worked to define key features in 4D. She has worked on most of preemptive multi-threading new features and also on a very complex subject: the new architecture for engined application. Vanessa has a degree from Telecom Saint-Etienne. She began her career at the Criminal Research Institute as a developer for the audiovisual department. She has also worked in media and medical fields as expert in technical support, production as well as documenting new features.