KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Utility Method to Return Files Content inside a ZIP Archive
PRODUCT: 4D | VERSION: 20 R | PLATFORM: Mac & Win
Published On: August 26, 2025

Below is a utility method that will return the content of a file or files inside a zip archive file :

// Method: Util_selectZipAndListContents
#DECLARE($extension : Text)->$textContents : Collection

var $docRef : Time
var $archive : 4D.ZipArchive
var $textFiles : Collection
var $textFile : 4D.ZipFile

$docRef:=Open document(""; ".zip")

If (OK=1)
   $archive:=ZIP Read archive(File(Document; fk platform path))

   If ($extension="")
     $extension:=".txt"
   End if

   $textFiles:=$archive.root.files(fk recursive).filter(Formula(($1.value.extension=$extension) && ($1.value.hidden=False)))

   $textContents:=[]
   For each ($textFile; $textFiles)
     $textContents.push({filename: $textFile.path; content: $textFile.getText()})
   End for each

   CLOSE DOCUMENT($docRef)
End if


Example:

var $textFiles1; $textFiles2; $textFiles3 : Collection

$textFiles1:=Util_selectZipAndListContents(".log")

$textFiles2:=Util_selectZipAndListContents(".txt")

$textFiles3
:=Util_selectZipAndListContents.sort(Formula($1.value.filename<$1.value2.filename))