Skip to main content

zipCreateArchive

zipCreateArchive ( fileToZip : 4D.File , destinationFile : 4D.File ) : object
zipCreateArchive ( folderToZip : 4D.Folder , destinationFile : 4D.File { , options : integer }) : object
zipCreateArchive ( zipStructure : object , destinationFile : 4D.File ) : object

ParameterTypeDescription
fileToZip4D.File->File or folder object to compress
folderToZip4D.Folder->File or folder object to compress
zipStructureobject->File or folder object to compress
destinationFile4D.File->Destination file for the archive
optionsinteger->folderToZip option: ZIP Without enclosing folder
Resultobject<-Status object

Description

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

You can pass a 4D.File, a 4D.Folder, or a zip structure object as first parameter:

  • fileToZip: You simply pass a 4D.File to compress.

  • folderToZip: You pass a 4D.Folder to compress. In this case, the options parameter allows you to compress only the contents of the folder (i.e., exclude the enclosing folder). By default, zipCreateArchive will compress the folder and its contents, so that the decompressing operation will recreate a folder. If you want the decompressing operation to restore only the contents of the folder, pass the ZIP Without enclosing folder constant in the options parameter.

  • zipStructure: You pass an object describing the ZIP archive object. The following properties are available to define the structure:

PropertyTypeDescription
compressioninteger
  • ZIP Compression standard: Deflate compression (default)
  • ZIP Compression LZMA: LZMA compression
  • ZIP Compression XZ: XZ compression
  • ZIP Compression none: No compression
  • levelintegerCompression level. Possible values: 1 to 10. A lower value will produce a larger file, while a higher value will produce a smaller file. Compression level has however an impact on performance. Default values if omitted:
  • ZIP Compression standard: 6
  • ZIP Compression LZMA: 4
  • ZIP Compression XZ: 4
  • encryptionintegerThe encryption to use if a password is set:
  • 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)
  • passwordstringA password to use if encryption is required.
    filesCollection
  • a collection of 4D.File or 4D.Folder objects or
  • a collection of objects with the following properties:
  • PropertyTypeDescription
    source4D.File or 4D.FolderFile or folder
    destinationstring(optional) - Specify a relative filepath to change the organization of the contents of the archive
    optionnumber(optional) - ZIP Ignore invisible files or 0 to compress all of the file
    callback4D.FunctionA callback formula that will receive the compression progress (0 - 100) in $1.

    In the destinationFile parameter, pass a 4D.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.

    Once an archive is created, you can use the zipReadArchive command to access it.

    Status object

    The returned status object contains the following properties:

    PropertyTypeDescription
    statusTextstringError message (if any):
  • Cannot open ZIP archive
  • Cannot create ZIP archive
  • Password is required for encryption
  • statusintegerStatus code
    successbooleanTrue if archive created successfully, else false

    Example 1

    To compress a 4D.File:

     var zfile, destination : 4D.File
    var status : object

    destination = file("/SOURCES/MyDocs/file.zip")
    zfile = file("/SOURCES/MyDocs/text.txt")

    status = zipCreateArchive(zfile,destination)

    Example 2

    To compress a 4D.Folder without the folder itself:

     var zfolder : 4D.Folder
    var destination : 4D.File
    var status : object

    destination = file("/SOURCES/MyDocs/Images.zip")
    zfolder = folder("/SOURCES/MyDocs/Images")

    status = zipCreateArchive(zfolder,destination,ZIP Without enclosing folder)

    Example 3

    To compress a ZIP archive structure with a password:

     var destination : 4D.File
    var zip,status : object

    destination = file("/SOURCES/MyDocs/Archive.zip")

    zip = newObject
    zip.files = folder("/SOURCES/MyDocs/Resources").folders()
    zip.password = "password"

    status = zipCreateArchive(zip,destination)

    Example 4

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

     var destination : 4D.File
    var zip,err : object
    zip = newObject
    zip.files = newCollection
    zip.files.push(newObject("source",file("/SOURCES/Tests/text.txt")))
    zip.files.push(newObject("source",file("/SOURCES/Tests/text2.txt")))
    zip.files.push(newObject("source",file("/SOURCES/Images/image.png")))

    destination = file("/SOURCES/file.zip")
    err = zipCreateArchive(zip,destination)

    Example 5

    You want to use an alternative compression algorithm with a high compression level:

    var destination : 4D.File
    var zip, err : object

    zip = newObject
    zip.files = newCollection
    zip.files.push(folder("/SOURCES/images"))
    zip.compression = ZIP Compression LZMA
    zip.level = 7 //default is 4

    destination = file("/SOURCES/images.zip")
    err = zipCreateArchive(zip, destination)