Skip to content

Zipping and unzipping with .NET on Windows and zip/unzip on other platforms

License

Notifications You must be signed in to change notification settings

aplteam/ZipArchive

Repository files navigation

ZipArchive

Overview

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.

Requirements

  • ZipArchive runs under Dyalog 18.0 and later
  • Under Windows .NET is required , but that will always be available anyway

Using shared methods

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.

Using instance methods

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            

Processing one or more files

Add as well as Delete can process one or multiple filenames and folder names.

Relative versus absolute paths with instances

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:

Relative to the current directory

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'

Use an absolute path and preserve it

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.

Specifying a parent folder

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.

About

Zipping and unzipping with .NET on Windows and zip/unzip on other platforms

Resources

License

Stars

Watchers

Forks