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:
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"))