This is the legacy 4D documentation web site. Documentations are progressively being moved to developer.4d.com

Home

 
4D v19.8
ZIP Create archive

ZIP Create archive 


 

ZIP Create archive ( fileOrFolder | zipStructure ; destinationFile {; options} ) -> Function result 
Parameter Type   Description
fileOrFolder | zipStructure   Object in File or Folder object to compress
destinationFile  Object in Destination file for the archive
options  Longint in If fileOrFolder used: ZIP Without enclosing folder
Function result  Object in Status object

The ZIP Create archive command creates a compressed ZIP archive object and returns the status of the operation. 

You can pass either a fileOrFolder or a zipStructure object as first parameter.

  • fileOrFolder
    With this syntax, you simply pass the File or Folder object to compress. In this case, you can use the options parameter (see below). 
  • zipStructure
    With this syntax, you pass an object describing the ZIP archive object as parameter (if passed, the options parameter is ignored in this case). The following properties are available to define the structure:

    Property   Type Description
    compression  integer
    Constant Comment
    ZIP Compression none

    No compression.

    ZIP Compression standard

    Standard compression. (default)

    encryption  integer The encryption to use if a password is set:
     
    Constant Comment
    ZIP Encryption AES128

    AES encryption using 128-bit key.

    ZIP Encryption AES192

    AES encryption using 192-bit key.

    ZIP Encryption AES256

    AES encryption using 256-bit key. (default if password is set)

    ZIP Encryption none

    Data is not encrypted. (default if no password is set)

    password  text A password to use if encryption is required. 
    files  collection
    • a collection of File or Folder objects or
    • a collection of objects with the following properties:
      Property Type Description
      source object File or Folder
      destination text (optional) - Specify a relative filepath to change the organization of the contents of the archive
      option number (optional) - ZIP Ignore invisible files or 0 to compress all of the file
    callback  formula A callback formula that will receive the compression progress (0 - 100) in $1. 


In the destinationFile parameter, pass a File object describing the ZIP archive to create (name, location, etc.). It is advised to use the ".zip" extension if you want the ZIP archive to be processed automatically by any software. 

 

The optional options parameter allows you to compress only the contents of fileOrFolder (i.e., exclude the enclosing folder): by default, when you pass a Folder in the fileOrFolder parameter, ZIP Create archive will compress the folder and its contents, so that the decompressing operation will recreate a folder. If you want the decompressing operation to compress only the contents of the folder, pass the ZIP Without enclosing folder constant in the options parameter.

 

Once an archive is created, you can use the ZIP Read archive to access it.

The returned status object contains the following properties:

 

Property Type Description
statusText text Error message (if any):
  • Cannot open ZIP archive
  • Cannot create ZIP archive
  • Password is required for encryption
status longint Status code
success boolean True if archive created successfully, else false

To compress a File:

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

To compress a Folder without the folder itself:

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

To compress a ZIP archive structure with a password and progress bar:

 C_OBJECT($zip;$status;$destination)
 C_LONGINT(progID)
 
 $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(myFormulaCompressingMethod($1))
 
 progID:=Progress New //we use the 4D Progress component
 
 $status:=ZIP Create archive($zip;$destination)
 
 Progress QUIT(progID)

myFormulaCompressingMethod:

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

You want to pass a collection of folders and files to compress to the zipStructure object:

 C_OBJECT($zip;$destination;$err)
 $zip:=New object
 $zip.files:=New collection
 $zip.files.push(New object("source";Folder(fk desktop folder).file("Tests/text.txt")))
 $zip.files.push(New object("source";Folder(fk desktop folder).file("Tests/text2.txt")))
 $zip.files.push(New object("source";Folder(fk desktop folder).file("Images/image.png")))
 
 $destination:=Folder(fk desktop folder).file("file.zip")
 $err:=ZIP Create archive($zip;$destination)



See also 

About ZIP Archives
ZIP Read archive

 
PROPERTIES 

Product: 4D
Theme: File and Folder
Number: 1640

This command can be run in preemptive processes

 
PAGE CONTENTS 
 
HISTORY 

Created: 4D v18

 
ARTICLE USAGE

4D Language Reference ( 4D v19.5)
4D Language Reference ( 4D v19.6)
4D Language Reference ( 4D v19.7)
4D Language Reference ( 4D v19.8)