KNOWLEDGEBASE
Log In    |    Knowledgebase    |    4D Home
Tech Tip: Create Finder-equivalent zip archives from within 4D
PRODUCT: 4D | VERSION: 12 | PLATFORM: Mac
Published On: November 3, 2011

Since early in the release of Mac OS-X, the Mac OS-X Finder has included a "Compress" function. This function is found in the Finder's File menu and in the contextual menu when you control-click on an item in the Finder. It is fast and it doesn't require you to install any third-party software. From within 4D you can get the same functionality of the "Compress" function in the Finder using a Mac OS-X UNIX command and LAUNCH EXTERNAL PROCESS.

The solution is the ditto command. With LAUNCH EXTERNAL PROCESS and ditto with the following options, you can create an archive just like the Finder's "Compress" function:

       ditto -c -k --keepParent --rsrc [source] [destination]


where "source" is the file or directory you want to archive and "destination" is the path, including file name, you want to give the new archive file. For example, to create an archive of a directory named "SourceCode" to a file named "mySrcCode.zip," the 4D code would that shown below:

C_TEXT($Command_T)
$Command_T:="ditto -c -k --keepParent --rsrc \"/Dev_4D/SourceCode\" \"/Dev_4D/mySrcCode.zip\""
SET ENVIRONMENT VARIABLE("_4D_OPTION_BLOCKING_EXTERNAL_PROCESS";"false")
LAUNCH EXTERNAL PROCESS($Command_T)


To extract the files from an zip archive is just as easy:

C_TEXT($Command_T)
$Command_T:="ditto -x -k \"/Dev_4D/mySrcCode.zip\" \"/Dev_4D/SourceCode\""
SET ENVIRONMENT VARIABLE("_4D_OPTION_BLOCKING_EXTERNAL_PROCESS";"false")
LAUNCH EXTERNAL PROCESS($Command_T)


The capabilities of the UNIX ditto command far exceed what have been presented so far. The below chart summaries what it can do, which includes coping as well as archiving:



Though the chart says it cannot "copy" a single file, in tests for this Tech Tip is will archive a single file.

Full documentation for the ditto command, including all command options, can be found at the included link.