:Namespace sfZip (⎕IO ⎕ML ⎕WX)←1 3 3 ∇ r←{nsOrKey}AplToZipFile fileName;case;dataStream;item;key;ns;sfDir;val;zipArchive;⎕USING ⍝ Function to Save and Retrieve to disk a Namespace of only characters. ⍝ The characters will be zipped using the Syncfusion ZipArchive library. ⍝ ⍝ fileName = Fully qualified file name ⍝ nsOrKey = Namespace to save under fileName ⍝ = Key of retrieved Namespace saved under fileName ⍝ = if empty returns the full Namespace saved under fileName ⍝ ⍝ r ← 1 'text' or r ← 1 Namespace ⍝ If successfull ⍝ r ← 0 (error description) ⍝ If failure ⎕USING←0⍴⊂'' ⍝ To Speed up ⎕NC ⍝ Check for 'nsOrKey' and select the case: :If 0=⎕NC'nsOrKey' ⍝ nsOrKey does not exist. Read the fileName and return a NameSpace. case←'ReadFile' :ElseIf 9=⎕NC'nsOrKey' ⍝ nsOrKey is a NameSpace. Save it under FileName. ns←nsOrKey ⋄ case←'SaveNS' :ElseIf ' '=↑1↑0⍴nsOrKey ⍝ nsOrKey is character(s). Read the fileName and return only this Key. key←nsOrKey ⋄ case←'ReturnKey' :Else r←0 'AplToZipFile Error: Left Argument Must be a NameSpace or a Character Vector(Key)' →0 :End ⍝ Required for all the cases. sfDir←'Syncfusion/4.5/' ⍝ Location of the Syncfusion librairies ⎕USING←('Syncfusion.Compression.Zip,',sfDir,'Syncfusion.Compression.Base.dll')'System.IO,mscorlib.dll' 'System,mscorlib.dll' zipArchive←⎕NEW ZipArchive zipArchive.DefaultCompressionLevel←zipArchive.DefaultCompressionLevel.BestSpeed ⍝ AboveNormal BelowNormal Best BestSpeed NoCompression Normal :Select case :Case 'ReadFile' ⍝ Read the file and return a namespace :Trap 0 zipArchive.Open(⊂,fileName) ns←⎕NS'' :For key :In zipArchive.Items.ItemName val←'UTF-8'⎕UCS zipArchive.Item[⊂,key].DataStream.ToArray ⍎'ns.',(¯4↓key),'←val' :End zipArchive.Close ⋄ zipArchive.Dispose ⋄ zipArchive←⎕NULL r←1 ns :Else →ERROR :EndTrap :Case 'ReturnKey' ⍝ Return the value of only one Key. :Trap 0 zipArchive.Open(⊂,fileName) :If (⊂key,'.txt')∊zipArchive.Items.ItemName ⍝ key is valid. val←'UTF-8'⎕UCS zipArchive.Item[⊂,key,'.txt'].DataStream.ToArray zipArchive.Close ⋄ zipArchive.Dispose ⋄ zipArchive←⎕NULL r←1 val :Else ⍝ key does not exist. r←0 'AplToZipFile: Invalid Key: ',∊key zipArchive.Close ⋄ zipArchive.Dispose ⋄ zipArchive←⎕NULL :End :Else →ERROR :EndTrap :Case 'SaveNS' ⍝ Save the Namespace to the file name. :Trap 0 :For key :In ns.⎕NL-2 val←ns.⍎key dataStream←⎕NEW MemoryStream(⊂'UTF-8'⎕UCS val) dataStream.Position←Convert.ToInt64 0 ⍝ Starting Position Set to 0 item←⎕NEW ZipArchiveItem(zipArchive(key,'.txt')dataStream 0 FileAttributes.Archive) {}zipArchive.AddItem(item) :End zipArchive.Save(⊂,fileName) zipArchive.Close ⋄ zipArchive.Dispose ⋄ zipArchive←⎕NULL r←1 :Else →ERROR :EndTrap :EndSelect →0 ERROR: ⍝ Show the Error: :If 90=⎕EN r←0('AplToFile EXCEPTION: ',⎕EXCEPTION.Message) :Else r←0((1⊃⎕DM),': ',{(~(∧\' '=⍵)∨(⌽∧\⌽' '=⍵))/⍵}(2⊃⎕DM)) :EndIf ⍝ Try to close zipArchive in case the error happened before closing it: :Trap 0 zipArchive.Close ⋄ zipArchive.Dispose ⋄ zipArchive←⎕NULL :EndTrap ∇ ∇ r←ZipFile fileName;sfDir;zipArchive;⎕USING ⍝ Compress a file on disk using Syncfusion ZipArchive library. ⍝ The file will be saved at the same location with a .zip extension. ⍝ ⍝ fileName = fully qualified file name with extension. ⍝ ⍝ r ← 1 ⍝ if successfull ⍝ r ← 0 (error description) ⍝ if failure ⍝ Set ⎕USING for the Syncfusion library. sfDir←'Syncfusion/4.5/' ⍝ Location of the Syncfusion librairies ⎕USING←'Syncfusion.Compression.Zip,',sfDir,'Syncfusion.Compression.Base.dll' :Trap 0 ⍝ Get a ZipArchive object zipArchive←⎕NEW ZipArchive zipArchive.DefaultCompressionLevel←zipArchive.DefaultCompressionLevel.Best ⍝ Add the file to the ZipArchive object {}zipArchive.AddFile(⊂,fileName) ⍝ Change the fileName extension to zip fileName←({⌽(a⍳'.')↓a←⌽⍵}fileName),'.zip' ⍝ Save the zipped file zipArchive.Save(⊂,fileName) ⍝ Clean-up zipArchive.Close ⋄ zipArchive.Dispose ⋄ zipArchive←⎕NULL r←1 :Else ⍝ Show the Error: :If 90=⎕EN r←0('ZipFile EXCEPTION: ',⎕EXCEPTION.Message) :Else r←0((1⊃⎕DM),': ',{(~(∧\' '=⍵)∨(⌽∧\⌽' '=⍵))/⍵}(2⊃⎕DM)) :EndIf ⍝ Try to close zipArchive in case the error happened before closing it: :Trap 0 zipArchive.Close ⋄ zipArchive.Dispose ⋄ zipArchive←⎕NULL :EndTrap :EndTrap ∇ ∇ r←UnZipFile fileName;sfDir;zipArchive;⎕USING;outString ⍝ Decompress a file compressed with ZipFile. ⍝ ⍝ fileName = fully qualified file name with extension. ⍝ ⍝ r ← 1 (unZip file content) ⍝ if successfull ⍝ r ← 0 (error description) ⍝ if failure ⍝ Set ⎕USING for the Syncfusion dll. sfDir←'Syncfusion/4.5/' ⍝ Location of the Syncfusion librairies ⎕USING←'Syncfusion.Compression.Zip,',sfDir,'Syncfusion.Compression.Base.dll' :Trap 0 ⍝ Get a ZipArchive object zipArchive←⎕NEW ZipArchive ⍝ Open the file with the ZipArchive object zipArchive.Open(⊂fileName) ⍝ Decompress the FileStream and return result. outString←⎕UCS zipArchive.Item[0].DataStream.ToArray r←1 outString :Else ⍝ Show the Error: :If 90=⎕EN r←0('UnZipFile EXCEPTION: ',⎕EXCEPTION.Message) :Else r←0((1⊃⎕DM),': ',{(~(∧\' '=⍵)∨(⌽∧\⌽' '=⍵))/⍵}(2⊃⎕DM)) :EndIf ⍝ Try to close zipArchive in case the error happened before closing it: :Trap 0 zipArchive.Close ⋄ zipArchive.Dispose ⋄ zipArchive←⎕NULL :EndTrap :EndTrap ∇ ∇ zipString←ZipText string;dataStream;item;outStream;sfDir;zipArchive;⎕USING ⍝ Compress a vector of character using Syncfusion ZipArchive library. ⍝ Set ⎕USING for the Syncfusion dll. sfDir←'Syncfusion/4.5/' ⍝ Location of the Syncfusion librairies ⎕USING←('Syncfusion.Compression.Zip,',sfDir,'Syncfusion.Compression.Base.dll')'System.IO,mscorlib.dll' 'System,mscorlib.dll' ⍝ Convert the APL character vector to a MemoryStream. dataStream←⎕NEW MemoryStream(⊂'UTF-8'⎕UCS string) dataStream.Position←Convert.ToInt64 0 ⍝ Set starting position to 0 ⍝ Get a ZipArchive object zipArchive←⎕NEW ZipArchive zipArchive.DefaultCompressionLevel←zipArchive.DefaultCompressionLevel.Best ⍝ Add a ZipArchiveItem to ZipArchive item←⎕NEW ZipArchiveItem(zipArchive'dir'dataStream 0 FileAttributes.Archive) {}zipArchive.AddItem(item) ⍝ Return the Compress character vector. outStream←⎕NEW MemoryStream zipArchive.Save(outStream 0) zipString←⎕UCS outStream.ToArray ⍝ Clean-up. dataStream.Close ⋄ dataStream.Dispose ⋄ dataStream←⎕NULL outStream.Close ⋄ outStream.Dispose ⋄ outStream←⎕NULL zipArchive.Close ⋄ zipArchive.Dispose ⋄ zipArchive←⎕NULL ∇ ∇ string←UnZipText zipString;dataStream;sfDir;zipArchive;⎕USING ⍝ Decompress a string obtained from ZipCompressText ⍝ Set ⎕USING for the Syncfusion dll. sfDir←'Syncfusion/4.5/' ⍝ Location of the Syncfusion librairies ⎕USING←('Syncfusion.Compression.Zip,',sfDir,'Syncfusion.Compression.Base.dll')'System.IO,mscorlib.dll' ⍝ Save the compressed string to a MemoryStream dataStream←⎕NEW MemoryStream(⊂⎕UCS zipString) ⍝ Decompress the MemoryStream and return result. zipArchive←⎕NEW ZipArchive zipArchive.Open(dataStream 0) string←'UTF-8'⎕UCS zipArchive.Item[0].DataStream.ToArray ⍝ Clean-up. dataStream.Close ⋄ dataStream.Dispose ⋄ dataStream←⎕NULL zipArchive.Close ⋄ zipArchive.Dispose ⋄ zipArchive←⎕NULL ∇ :EndNamespace