Saturday, December 17, 2011

Using the DotNetZip Library with Pervasive EZScript

We are often required to work with compressed files in our Pervasive Data Integrator processes. We receive files that are compressed, we use file compression for archiving and we provide files to our clients that we compress prior to transmitting them.

The DotNetZip Library is great free file-compression library that you can use with your own applications or script. Used with Pervasive's EZScript, you can add zipping and unzipping functionality to any Data Integrator process.

To use the library I had to first install .Net Framework 3.5.1 Feature on our Windows Server 2008 R2 that hosts Pervasive Data Integrator. Next, I downloaded the DotNetZipUtils-v1.9.msi to install the runtime DLL. Finally, I used this excellent PowerShell script written by "Fred" that I lifted from here to install the Ionic.Zip.dll into the Global Assembly Cache.

I've included a couple of functions below to get you started with using file compression in EZScript.

ZIP function:

Function ZipFile(fileName, zipFileName, password)
 Dim oZipFile As Object
 Dim fileNames(), i

 ReDim fileNames(UBound(Split(fileName, ";")))
 fileNames = Split(fileName, ";")

 Set oZipFile = CreateObject("Ionic.Zip.ZipFile")
 '"using AES256 encryption...") 
 'oZipFile.Encryption = 3  

 ' same password all items
 If Len(password) <> 0 Then
  oZipFile.Password = password
 End If

 For i = 0 To UBound(fileNames)
  'oZipFile.AddItem(fileName)  
  oZipFile.AddItem_2(Trim(fileNames(i)), "") 
 Next

 oZipFile.Name = zipFileName
 LogMessage("INFO", "Zipping file: " & zipFilename)
 oZipFile.Save()
 LogMessage("INFO", "File zipped: " & zipFilename)

 oZipFile.Dispose()
 Set oZipFile = Nothing

End Function

Unzip function:

' ExtractExistingFileAction: 0= Throw; 1= OverwriteSilently; 2=DoNotOverwrite 
Function UnZipFile(zipFileName, password, unZipDirectory, ExtractExistingFileAction)
 Dim oZipFile As Object
 
 Set oZipFile = CreateObject("Ionic.Zip.ZipFile")
 '"using AES256 encryption...") 
 'oZipFile.Encryption = 3  

 oZipFile.Initialize(zipFileName)

 ' same password all items
 If Len(password) <> 0 Then
  oZipFile.Password = password
 End If

 oZipFile.ExtractAll_2(unZipDirectory, ExtractExistingFileAction)

 LogMessage("INFO", "Files extracted to " & unZipDirectory)

 oZipFile.Dispose()
 Set oZipFile = Nothing

End Function

And an example of how to call the functions:

Option Explicit

Dim zipFileName, password

' an example of zipping multiple files
ZipFile("\\fileshare\myFolder\myAccessFile.mdb;\\fileshare\myFolder\myTextFile.txt", _
 "\\fileshare\myFolder\myFile.zip", "Super Secret Password")

Dim unZipDirectory, overwriteAction
zipFileName = "\\fileshare\myFolder\myZippedFile.zip"
password = ""
unZipDirectory = "\\fileshare\myFolder\tmpZIP\"
' overwriteAction: 0=throw error; 1=overwrite silently; 2=do not overwrite
overwriteAction = 1

UnZipFile(zipFileName, password, unZipDirectory, overwriteAction)

Hopefully these examples can help you get started with an easy-to-use file compression (and decompression) in your Pervasive Data Integrator processes.

No comments:

Post a Comment