The ZipArchive
class allows one to zip and unzip files.
- On Windows, it depends on the Tatin package [DotNetZip] which in turn uses .NET
- On all other platform the
zip
package is used
The purpose of this was to not rely on .NET under Linux etc if one just wants to zip and unzip files and folders.
ZipArchive
runs under Dyalog 18.0 and later- Under Windows .NET is required , but that will always be available anyway
There are three shared methods available:
ZipFolder
UnzipTo
ListZipContents
The names should tell the whole story.
Note that when you pass a path like C:\Temp\Foo
to ZipFolder
and that folder contains:
readme.txt
sub/foo.dll
then the files in the ZIP carry only the names readme.txt
and sub/foo.dll
rather than the full path.
First you need to create an instance. The constructor requires the name of the ZIP file to be created (if it does not exists yet) or, in case it already exists, replaced or altered:
myZip←ZipArchive (,⊂'C:\Temp\MyZip')
By default if the zip file already exists it is overwritten.
The instance offers these methods:
Add
Delete
Dispose
ExtractTo
List
Add
as well as Delete
can process one or multiple filenames and folder names.
If you want to save these files:
foo/readme.txt
foo/sub/foo.dll
with an instance of ZipArchive
then there are two different scenarios:
When the folder foo
lives in your current directory then, assuming that your instance of ZipArchive
is named myZip
, you can just specify:
myZip.Add 'foo/readme.txt' 'f00/sub/foo.dll'
When the folder foo
lives in /tmp/MyStuff/
then you could use this:
myZip.Add '/tmp/MyStuff/foo/readme.txt'
myZip.Add '/tmp/MyStuff/foo/sub/foo.dll'
The obvious disadvantage is that the full path is preserved in the ZIP file, meaning that the files can only ever be unzipped into /tmp/MyStuff/
; if that does not exist, the operation will fail.
To get around this you can specify a parent folder as left argument, and specify on the right everything that should be preserved, foo/
in our case:
'/tmp/MyStuff/' myZip.Add 'foo/readme.txt'
'/tmp/MyStuff/' myZip.Add 'foo/sub/foo.dll'
Note that it does not matter whether the left argument carries a trailing separator or not, and that it does not matter whether the right argument(s) carry a leading separator or not: ZipArchive
will work that out.