ZIP Create archive
ZIP Create archive ( fileToZip : 4D.File ; destinationFile : 4D.File ) : Object
ZIP Create archive ( folderToZip : 4D.Folder ; destinationFile : 4D.File { ; options : Integer }) : Object
ZIP Create archive ( zipStructure : Object ; destinationFile : 4D.File ) : Object
Parameter | Type | Description | |
---|---|---|---|
fileToZip | 4D.File | → | File or Folder object to compress |
folderToZip | 4D.Folder | → | File or Folder object to compress |
zipStructure | Object | → | File or Folder object to compress |
destinationFile | 4D.File | → | Destination file for the archive |
options | Integer | → | folderToZip option: ZIP Without enclosing folder |
Result | Object | ← | Status object |
History
Release | Changes |
---|---|
19 R3 | Added ZIP Compression LZMA , ZIP Compression xy , .level property |
18 | Added |
Description
The ZIP Create archive
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,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 restore only the contents of the folder, pass theZIP 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:
Property | Type | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
compression | Integer | ZIP Compression standard : Deflate compression (default)ZIP Compression LZMA : LZMA compressionZIP Compression XZ : XZ compressionZIP Compression none : No compression | ||||||||||||
level | Integer | Compression 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 : 6ZIP Compression LZMA : 4ZIP Compression XZ : 4 | ||||||||||||
encryption | Integer | The 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) | ||||||||||||
password | Text | A password to use if encryption is required. | ||||||||||||
files | Collection | 4D.File or 4D.Folder objects or
| ||||||||||||
callback | 4D.Function | A 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 ZIP Read archive command to access it.
Status object
The returned status object contains the following properties:
Property | Type | Description |
---|---|---|
statusText | Text | Error message (if any): |
status | Integer | Status code |
success | Boolean | True if archive created successfully, else false |
Example 1
To compress a 4D.File
:
var $file; $destination : 4D.File
var $status : Object
$destination:=Folder(fk desktop folder).file("MyDocs/file.zip")
$file:=Folder(fk desktop folder).file("MyDocs/text.txt")
$status:=ZIP Create archive($file;$destination)
Example 2
To compress a 4D.Folder
without the folder itself:
var $folder : 4D.Folder
var $destination : 4D.File
var $status : Object
$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)
Example 3
To compress a ZIP archive structure with a password and progress bar:
var $destination : 4D.File
var $zip;$status : Object
var progID : Integer
$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
:
var $1 : Integer
Progress SET PROGRESS(progID;Num($1/100))
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:=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)
Example 5
You want to use an alternative compression algorithm with a high compression level:
var $destination : 4D.File
var $zip; $err : Object
$zip:=New object
$zip.files:=New collection
$zip.files.push(Folder(fk desktop folder).folder("images"))
$zip.compression:=ZIP Compression LZMA
$zip.level:=7 //default is 4
$destination:=Folder(fk desktop folder).file("images.zip")
$err:=ZIP Create archive($zip; $destination)
See also
ZipArchive Class
ZipFile Class
ZipFolder Class
ZIP Read archive
Properties
Command number | 1640 |
Thread safe | ✓ |