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:

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

Compress a folder:

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

Compress with a password and a progress bar:

C_OBJECT($zip)
$destination:=Folder(fk desktop folder).file("MyDocs/Archive.zip")
$zip:=New object
$zip.files:=Folder(fk desktop folder).folder("MyDocs/Resources").folders()
$zip.password:="password"
$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

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

Retrieve the list of files and folders

$folders:=$archive.root.folders()
$files:=$archive.root.files()

Read the content of a file without extraction

If ($files[$i].extension=".txt")
   $txt:=$files[$i].getText()
Else
   $blob:=$files[$i].getContent()
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.