Attachment 'sfZip.v1.0.txt'

Download

   1 :Namespace sfZip
   2 
   3     (⎕IO ⎕ML ⎕WX)←1 3 3
   4 
   5     ∇ r←{nsOrKey}AplToZipFile fileName;case;dataStream;item;key;ns;sfDir;val;zipArchive;⎕USING
   6     ⍝ Function to Save and Retrieve to disk a Namespace of only characters.
   7     ⍝ The characters will be zipped using the Syncfusion ZipArchive library.
   8 
   9     ⍝ fileName = Fully qualified file name
  10     ⍝ nsOrKey  = Namespace to save under fileName
  11     ⍝          = Key of retrieved Namespace saved under fileName
  12     ⍝          = if empty returns the full Namespace saved under fileName
  13 
  14     ⍝ r ← 1 'text' or r ← 1 Namespace  ⍝ If successfull
  15     ⍝ r ← 0 (error description)        ⍝ If failure
  16      
  17       ⎕USING←0⍴⊂''  ⍝ To Speed up ⎕NC
  18      
  19     ⍝ Check for 'nsOrKey' and select the case:
  20       :If 0=⎕NC'nsOrKey'
  21         ⍝ nsOrKey does not exist. Read the fileName and return a NameSpace.
  22           case←'ReadFile'
  23      
  24       :ElseIf 9=⎕NC'nsOrKey'
  25         ⍝ nsOrKey is a NameSpace. Save it under FileName.
  26           ns←nsOrKey ⋄ case←'SaveNS'
  27      
  28       :ElseIf ' '=↑1↑0⍴nsOrKey
  29         ⍝ nsOrKey is character(s). Read the fileName and return only this Key.
  30           key←nsOrKey ⋄ case←'ReturnKey'
  31      
  32       :Else
  33           r←0 'AplToZipFile Error: Left Argument Must be a NameSpace or a Character Vector(Key)'
  34           →0
  35      
  36       :End
  37      
  38     ⍝ Required for all the cases.
  39       sfDir←'Syncfusion/4.5/' ⍝ Location of the Syncfusion librairies
  40       ⎕USING←('Syncfusion.Compression.Zip,',sfDir,'Syncfusion.Compression.Base.dll')'System.IO,mscorlib.dll' 'System,mscorlib.dll'
  41       zipArchive←⎕NEW ZipArchive
  42       zipArchive.DefaultCompressionLevel←zipArchive.DefaultCompressionLevel.BestSpeed ⍝ AboveNormal  BelowNormal  Best  BestSpeed  NoCompression  Normal
  43      
  44       :Select case
  45       :Case 'ReadFile'
  46         ⍝ Read the file and return a namespace
  47           :Trap 0
  48               zipArchive.Open(⊂,fileName)
  49               ns←⎕NS''
  50      
  51               :For key :In zipArchive.Items.ItemName
  52                   val←'UTF-8'⎕UCS zipArchive.Item[⊂,key].DataStream.ToArray
  53                   ⍎'ns.',(¯4↓key),'←val'
  54               :End
  55      
  56               zipArchive.Close ⋄ zipArchive.Dispose ⋄ zipArchive←⎕NULL
  57               r←1 ns
  58      
  59           :Else
  60               →ERROR
  61           :EndTrap
  62      
  63       :Case 'ReturnKey'
  64         ⍝ Return the value of only one Key.
  65           :Trap 0
  66               zipArchive.Open(⊂,fileName)
  67      
  68               :If (⊂key,'.txt')∊zipArchive.Items.ItemName
  69                 ⍝ key is valid.
  70                   val←'UTF-8'⎕UCS zipArchive.Item[⊂,key,'.txt'].DataStream.ToArray
  71                   zipArchive.Close ⋄ zipArchive.Dispose ⋄ zipArchive←⎕NULL
  72                   r←1 val
  73      
  74               :Else
  75                 ⍝ key does not exist.
  76                   r←0 'AplToZipFile: Invalid Key: ',∊key
  77                   zipArchive.Close ⋄ zipArchive.Dispose ⋄ zipArchive←⎕NULL
  78      
  79               :End
  80           :Else
  81               →ERROR
  82           :EndTrap
  83      
  84       :Case 'SaveNS'
  85         ⍝ Save the Namespace to the file name.
  86           :Trap 0
  87               :For key :In ns.⎕NL-2
  88                   val←ns.⍎key
  89                   dataStream←⎕NEW MemoryStream(⊂'UTF-8'⎕UCS val)
  90                   dataStream.Position←Convert.ToInt64 0   ⍝ Starting Position Set to 0
  91                   item←⎕NEW ZipArchiveItem(zipArchive(key,'.txt')dataStream 0 FileAttributes.Archive)
  92                   {}zipArchive.AddItem(item)
  93               :End
  94      
  95               zipArchive.Save(⊂,fileName)
  96               zipArchive.Close ⋄ zipArchive.Dispose ⋄ zipArchive←⎕NULL
  97               r←1
  98      
  99           :Else
 100               →ERROR
 101           :EndTrap
 102       :EndSelect
 103      
 104       →0
 105      
 106      ERROR:
 107      
 108     ⍝ Show the Error:
 109       :If 90=⎕EN
 110           r←0('AplToFile EXCEPTION: ',⎕EXCEPTION.Message)
 111       :Else
 112           r←0((1⊃⎕DM),': ',{(~(∧\' '=⍵)∨(⌽∧\⌽' '=⍵))/⍵}(2⊃⎕DM))
 113       :EndIf
 114      
 115      ⍝ Try to close zipArchive in case the error happened before closing it:
 116       :Trap 0
 117           zipArchive.Close ⋄ zipArchive.Dispose ⋄ zipArchive←⎕NULL
 118       :EndTrap
 119 
 120 
 121     ∇ r←ZipFile fileName;sfDir;zipArchive;⎕USING
 122     ⍝ Compress a file on disk using Syncfusion ZipArchive library.
 123     ⍝ The file will be saved at the same location with a .zip extension.
 124 
 125     ⍝ fileName = fully qualified file name with extension.
 126 
 127     ⍝ r ← 1                      ⍝ if successfull
 128     ⍝ r ← 0 (error description)  ⍝ if failure
 129      
 130     ⍝ Set ⎕USING for the Syncfusion library.
 131       sfDir←'Syncfusion/4.5/' ⍝ Location of the Syncfusion librairies
 132       ⎕USING←'Syncfusion.Compression.Zip,',sfDir,'Syncfusion.Compression.Base.dll'
 133      
 134       :Trap 0
 135         ⍝ Get a ZipArchive object
 136           zipArchive←⎕NEW ZipArchive
 137           zipArchive.DefaultCompressionLevel←zipArchive.DefaultCompressionLevel.Best
 138      
 139         ⍝ Add the file to the ZipArchive object
 140           {}zipArchive.AddFile(⊂,fileName)
 141      
 142         ⍝ Change the fileName extension to zip
 143           fileName←({⌽(a⍳'.')↓a←⌽⍵}fileName),'.zip'
 144      
 145         ⍝ Save the zipped file
 146           zipArchive.Save(⊂,fileName)
 147      
 148         ⍝ Clean-up
 149           zipArchive.Close ⋄ zipArchive.Dispose ⋄ zipArchive←⎕NULL
 150      
 151           r←1
 152      
 153       :Else
 154         ⍝ Show the Error:
 155           :If 90=⎕EN
 156               r←0('ZipFile EXCEPTION: ',⎕EXCEPTION.Message)
 157           :Else
 158               r←0((1⊃⎕DM),': ',{(~(∧\' '=⍵)∨(⌽∧\⌽' '=⍵))/⍵}(2⊃⎕DM))
 159           :EndIf
 160      
 161         ⍝ Try to close zipArchive in case the error happened before closing it:
 162           :Trap 0
 163               zipArchive.Close ⋄ zipArchive.Dispose ⋄ zipArchive←⎕NULL
 164           :EndTrap
 165      
 166       :EndTrap
 167 
 168     
 169     ∇ r←UnZipFile fileName;sfDir;zipArchive;⎕USING;outString
 170     ⍝ Decompress a file compressed with ZipFile.
 171 
 172     ⍝ fileName = fully qualified file name with extension.
 173 
 174     ⍝ r ← 1 (unZip file content) ⍝ if successfull
 175     ⍝ r ← 0 (error description)  ⍝ if failure
 176      
 177     ⍝ Set ⎕USING for the Syncfusion dll.
 178       sfDir←'Syncfusion/4.5/' ⍝ Location of the Syncfusion librairies
 179       ⎕USING←'Syncfusion.Compression.Zip,',sfDir,'Syncfusion.Compression.Base.dll'
 180      
 181       :Trap 0
 182         ⍝ Get a ZipArchive object
 183           zipArchive←⎕NEW ZipArchive
 184      
 185         ⍝ Open the file with the ZipArchive object
 186           zipArchive.Open(⊂fileName)
 187      
 188         ⍝ Decompress the FileStream and return result.
 189           outString←⎕UCS zipArchive.Item[0].DataStream.ToArray
 190           r←1 outString
 191      
 192       :Else
 193         ⍝ Show the Error:
 194           :If 90=⎕EN
 195               r←0('UnZipFile EXCEPTION: ',⎕EXCEPTION.Message)
 196           :Else
 197               r←0((1⊃⎕DM),': ',{(~(∧\' '=⍵)∨(⌽∧\⌽' '=⍵))/⍵}(2⊃⎕DM))
 198           :EndIf
 199      
 200         ⍝ Try to close zipArchive in case the error happened before closing it:
 201           :Trap 0
 202               zipArchive.Close ⋄ zipArchive.Dispose ⋄ zipArchive←⎕NULL
 203           :EndTrap
 204      
 205       :EndTrap
 206 
 207 
 208     ∇ zipString←ZipText string;dataStream;item;outStream;sfDir;zipArchive;⎕USING
 209     ⍝ Compress a vector of character using Syncfusion ZipArchive library.
 210      
 211     ⍝ Set ⎕USING for the Syncfusion dll.
 212       sfDir←'Syncfusion/4.5/' ⍝ Location of the Syncfusion librairies
 213       ⎕USING←('Syncfusion.Compression.Zip,',sfDir,'Syncfusion.Compression.Base.dll')'System.IO,mscorlib.dll' 'System,mscorlib.dll'
 214      
 215     ⍝ Convert the APL character vector to a MemoryStream.
 216       dataStream←⎕NEW MemoryStream(⊂'UTF-8'⎕UCS string)
 217       dataStream.Position←Convert.ToInt64 0   ⍝ Set starting position to 0
 218      
 219     ⍝ Get a ZipArchive object
 220       zipArchive←⎕NEW ZipArchive
 221       zipArchive.DefaultCompressionLevel←zipArchive.DefaultCompressionLevel.Best
 222      
 223     ⍝ Add a ZipArchiveItem to ZipArchive
 224       item←⎕NEW ZipArchiveItem(zipArchive'dir'dataStream 0 FileAttributes.Archive)
 225       {}zipArchive.AddItem(item)
 226      
 227     ⍝ Return the Compress character vector.
 228       outStream←⎕NEW MemoryStream
 229       zipArchive.Save(outStream 0)
 230       zipString←⎕UCS outStream.ToArray
 231      
 232     ⍝ Clean-up.
 233       dataStream.Close ⋄ dataStream.Dispose ⋄ dataStream←⎕NULL
 234       outStream.Close ⋄ outStream.Dispose ⋄ outStream←⎕NULL
 235       zipArchive.Close ⋄ zipArchive.Dispose ⋄ zipArchive←⎕NULL
 236 
 237     
 238     
 239     ∇ string←UnZipText zipString;dataStream;sfDir;zipArchive;⎕USING
 240     ⍝ Decompress a string obtained from ZipCompressText
 241      
 242     ⍝ Set ⎕USING for the Syncfusion dll.
 243       sfDir←'Syncfusion/4.5/' ⍝ Location of the Syncfusion librairies
 244       ⎕USING←('Syncfusion.Compression.Zip,',sfDir,'Syncfusion.Compression.Base.dll')'System.IO,mscorlib.dll'
 245      
 246     ⍝ Save the compressed string to a MemoryStream
 247       dataStream←⎕NEW MemoryStream(⊂⎕UCS zipString)
 248      
 249     ⍝ Decompress the MemoryStream and return result.
 250       zipArchive←⎕NEW ZipArchive
 251       zipArchive.Open(dataStream 0)
 252       string←'UTF-8'⎕UCS zipArchive.Item[0].DataStream.ToArray
 253      
 254     ⍝ Clean-up.
 255       dataStream.Close ⋄ dataStream.Dispose ⋄ dataStream←⎕NULL
 256       zipArchive.Close ⋄ zipArchive.Dispose ⋄ zipArchive←⎕NULL
 257 
 258 
 259 :EndNamespace

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2015-01-27 14:26:37, 9.7 KB) [[attachment:sfZip.v1.0.txt]]
  • [get | view] (2015-09-18 19:35:59, 13.6 KB) [[attachment:sfZip.v1.1.txt]]
 All files | Selected Files: delete move to page

You are not allowed to attach a file to this page.