Zipping and Unzipping files in UFT/QTP using Dotnetfactory

Hello friends,

In this post we will discuss about how to zip and unzip files during automated scripts execution within UFT. We will use .Net factory object, and the below code will required .net framework 4.5 or higher installed on the machine.

Zipping Folder’s

For zipping files copy and paste the below code in UFT and call the function, the first argument is the path of the folder to be zipped and second argument is the final zip file path with name.

Public Function CreateZipFile(ByVal SourceFolderPath, ByVal TargetZipFileName)
        Dim OZipObject
        Set ODirectory = DotNetFactory.CreateInstance("System.IO.Directory","System")            
        If ODirectory.Exists(SourceFolderPath) Then
            Set OZipObject = DotNetFactory.CreateInstance("System.IO.Compression.ZipFile","System.IO.Compression.FileSystem")                
            OZipObject.CreateFromDirectory SourceFolderPath, TargetZipFileName    
          End IF
End Function

First, we will use the directory object to check, if the folder to be zipped exists or not.

Set ODirectory = DotNetFactory.CreateInstance("System.IO.Directory","System")		
If ODirectory.Exists(SourceFolderPath) Then
'code here
End If

Second, we will use the ZipFile Object to create the zip file.
OZipObject.CreateFromDirectory SourceFolderPath, TargetZipFileName

Unzipping File’s

For unzipping files, copy and paste the following code

Public Function UnZipFile(ByVal SourceFilePath, ByVal TargetExtractFolderName)
    Dim OZipObject
    Set OFile = DotNetFactory.CreateInstance("System.IO.File","System")            
    If OFile.Exists(SourceFilePath) Then
        Set OZipObject = DotNetFactory.CreateInstance("System.IO.Compression.ZipFile","System.IO.Compression.FileSystem")                
        OZipObject.ExtractToDirectory SourceFilePath, TargetExtractFolderName    
      End IF
End Function

While unzipping file, we will check if the zip file exists and then, we will use the ExtractToDirectory function to unzip the files to the desired folder.
Below is the complete code

StartFolder = "C:\temp\TESTATTACHMENTS"
ZipFilename = "C:\temp\TESTATTACHMENTS.Zip"
UnzipFolder = "C:\temp\UnzipTestAttachments"

CreateZipFile StartFolder, ZipFilename

UnZipFile ZipFilename, UnzipFolder

Public Function CreateZipFile(ByVal SourceFolderPath, ByVal TargetZipFileName)
        Dim OZipObject
        Set ODirectory = DotNetFactory.CreateInstance("System.IO.Directory","System")            
        If ODirectory.Exists(SourceFolderPath) Then
            Set OZipObject = DotNetFactory.CreateInstance("System.IO.Compression.ZipFile","System.IO.Compression.FileSystem")                
            OZipObject.CreateFromDirectory SourceFolderPath, TargetZipFileName    
          End IF
End Function

Public Function UnZipFile(ByVal SourceFilePath, ByVal TargetExtractFolderName)
    Dim OZipObject
    Set OFile = DotNetFactory.CreateInstance("System.IO.File","System")            
    If OFile.Exists(SourceFilePath) Then
        Set OZipObject = DotNetFactory.CreateInstance("System.IO.Compression.ZipFile","System.IO.Compression.FileSystem")                
        OZipObject.ExtractToDirectory SourceFilePath, TargetExtractFolderName    
      End IF
End Function

Let me know, if you need any help, I will try to provide answers.

Thanks,
Sumeet Singh Kushwah

4 responses to “Zipping and Unzipping files in UFT/QTP using Dotnetfactory

  1. Great stuff! This doesn’t work when files already exist in the destination path though. Is there a “ExtractToDirectoryandOverwrite” equivalent? ExtractToFile doesn’t seem to do the trick.

    Like

  2. i am getting error while executing the script which zip the folder
    External object System.IO.Directory::Exists has thrown the following exception:
    Object reference not set to an instance of an object.
    The error appear at line with if condition
    If ODirectory.Exists(SourceFolderPath) Then

    Like

Leave a comment