Thursday, January 19, 2012

ZIP & UNZIP files in QTP

'=======================================================
'ZIP:
'=======================================================
Function WindowsZip(sFile, sZipFile)
     Set oZipShell = CreateObject(“WScript.Shell”)
     Set oZipFSO = CreateObject(“Scripting.FileSystemObject”)

     If Not oZipFSO.FileExists(sZipFile) Then
       NewZip(sZipFile)
     End If

     Set oZipApp = CreateObject(“Shell.Application”)
     sZipFileCount = oZipApp.NameSpace(sZipFile).items.Count
     aFileName = Split(sFile, “\”)
     sFileName = (aFileName(Ubound(aFileName)))
    REM  listfiles

     sDupe = False

     For Each sFileNameInZip In oZipApp.NameSpace(sZipFile).items
        If LCase(sFileName) = LCase(sFileNameInZip) Then
         sDupe = True
         Exit For
       End If
    Next

     If Not sDupe Then
     oZipApp.NameSpace(sZipFile).Copyhere sFile
     REM  Keep script waiting until Compressing is done
     On Error Resume Next
      sLoop = 0
      Do Until sZipFileCount < oZipApp.NameSpace(sZipFile).Items.Count
         Wscript.Sleep(100)
         sLoop = sLoop + 1
       Loop
    On Error GoTo 0
     End If
  End Function
'=======================================================
'UnZip:
'=======================================================
Function Unzip(strFilePath, strFileName, blnOverWriteFiles, blnDeleteZipFileAfterExtract, out_ExtractToFolder)
' This routine unzips a file. NOTE: The files are extracted to a folder '
' in the same location using the name of the file minus the extension.  '
' EX. C:\Test.zip will be extracted to C:\Test ' 'strFile (String) = Full path and filename of the file to be unzipped. '
  Dim arrFile
  Dim FileInsideZip, FilesInsideZip
  Dim fso, strFile, pathToZipFile, extractTo
  Dim objShell

  Unzip = ""
  strFile = strFilePath & "\" & strFileName
  arrFile = Split(strFile, ".")
  Set fso = CreateObject("Scripting.FileSystemObject")
  If Not fnVerifyFileExists(strFilePath, strFileName) Then
   Reporter.ReportEvent micFail, "Unzip file : " & strFilePath & "\" & strFileName, "File does not exist"
   Exit Function
  End If
  fnCreateFolderStructure arrFile(0)    'Build the folder tree
 ' fso.CreateFolder(arrFile(0) & "\ ")
  pathToZipFile= arrFile(0) & ".zip"
  extractTo= arrFile(0) '& "\"
  Set objShell = CreateObject("Shell.Application")
  Set filesInzip=objShell.NameSpace(pathToZipFile).items

  'Verify that if files present in the zip file is already present in the folder. If yes, then delete those.

   For each itm in objShell.NameSpace(pathToZipFile).items
    FileInsideZip = itm.Name
      If  FilesInsideZip = "" Then
       FilesInsideZip = FileInsideZip
      Else
       FilesInsideZip = FilesInsideZip & ";" & FileInsideZip
      End If
    If blnOverWriteFiles = True Then
     If fnVerifyFileExists (extractTo, FileInsideZip) = True Then
      fnDeleteFile ExtractTo, FileInsideZip
     End If
    End If
   Next
  objShell.NameSpace(extractTo).CopyHere filesInzip

  If blnDeleteZipFile = True Then
   fso.DeleteFile pathToZipFile, True
  End If
  Reporter.ReportEvent micDone, "Unzip File : " &  strFilePath & "\" & strFileName, "Unzipped location : " & extractTo & vbcrlf & vbcrlf & "Files Unzipped : " & vbcrlf & FilesInsideZip
  Unzip = FilesInsideZip
  out_ExtractToFolder = extractTo
  Set fso = Nothing
  Set objShell = Nothing
End Function 'Unzip
'=======================================================
'Verify File Exists in the Specified Directory
Function fnVerifyFileExists(FilePath, FileName)
   Dim fso
   Set fso = CreateObject("Scripting.FileSystemObject")
   fnVerifyFileExists = fso.FileExists(FilePath & "\" & FileName)
   Set fso = Nothing
End Function
'=======================================================
'Delete the specified File
Function fnDeleteFile(strFilePath, strFileName)
   Dim fso
   Set fso = CreateObject("Scripting.FileSystemObject")
   If  fso.FileExists(strFilePath & "\" & strFileName) Then
    fso.DeleteFile strFilePath & "\" & strFileName
 End If
 Set fso = Nothing
End Function
'=======================================================
'Creates the entire folder structure
Function fnCreateFolderStructure(FolPath)
   Dim fso, arrFolderPath
   Dim fIndex,i, CurrentFolderPath
   Set fso = CreateObject("Scripting.FileSystemObject")
   arrFolderPath = Split(FolPath, "\")
 For fIndex = 0 to Ubound(arrFolderPath)     '0 will always have Drive letter
  CurrentFolderPath = 0
  For i = 0 to fIndex
   If i = 0 Then
    CurrentFolderPath = arrFolderPath(i)
   Else
    CurrentFolderPath  = CurrentFolderPath & "\" & arrFolderPath(i)
     End If
  Next

  If Not fso.FolderExists(CurrentFolderPath)  Then
   fso.CreateFolder CurrentFolderPath
  End If

   Next
   Set fso = Nothing
End Function

2 comments:

  1. NewZip(sZipFile)
    u haven't define function then how is it suppose to work..

    ReplyDelete
  2. Hi.. Thanks for your comments. Actually, I thought to put a function definition alone here and the user can call the function according to their need.

    ReplyDelete