WORKING WITH QTP

TestEveryThinG

Create ZiP file VB QTP / UnZip file VB QTP

Posted by rajivkumarnandvani on May 13, 2009

Function WindowsUnZip(sUnzipFileName, sUnzipDestination)
Set oUnzipFSO = CreateObject(“Scripting.FileSystemObject”)
If Not oUnzipFSO.FolderExists(sUnzipDestination) Then
oUnzipFSO.CreateFolder(sUnzipDestination)
End If
With CreateObject(“Shell.Application”)
.NameSpace(sUnzipDestination).Copyhere .NameSpace(sUnzipFileName).Items
End With

Set oUnzipFSO = Nothing
End Function
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)))

‘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

‘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

Sub NewZip(sNewZip)
Set oNewZipFSO = CreateObject(“Scripting.FileSystemObject”)
Set oNewZipFile = oNewZipFSO.CreateTextFile(sNewZip)

oNewZipFile.Write Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, 0)

oNewZipFile.Close
Set oNewZipFSO = Nothing

Wscript.Sleep(500)
End Sub

WindowsZip “c:\rajiv.htm”, “c:\rajiv.zip”

Advertisements

15 Responses to “Create ZiP file VB QTP / UnZip file VB QTP”

  1. tweetme said

    When I use this function in my QTP code I get the following error,Do I need to do changes in order to run in QTP

    The test run cannot continue due to a syntax error.

    Syntax error

    Line (1): “Function WindowsUnZip(sUnzipFileName, sUnzipDestination)”.

  2. hi,
    While doing code copy paste from blog, make sure about single quote and double quote. just change in qtp using replace.because when you copy from blog page(web page) they are not exactly the same..

  3. anil said

    rajiv i used your code and it worked perfectly, thanks for a nice forum like this
    regards-anil

  4. Thanks man 🙂

  5. Bharat said

    Greta job done man.

  6. Thanks 🙂

  7. mahesh said

    Will it replace the folder if it already exists in the destination location?

  8. No it will not replace. you can check if file exist then delete using FSO(File system object) DeleteFile method
    Thanks

  9. praveen said

    How to unzip ‘sample.txt.z’ file using VB script or QTP ?

  10. Hi praveen,
    Thanks for reading the blog. for unzip the file you have to use the unzip function mentioned in post.
    Function WindowsUnZip(sUnzipFileName, sUnzipDestination)

    WindowsUnZip(“c:\sample.txt.z”,”c:\”)

    best of luck
    Thanks

  11. rk said

    its reaally good work dude 🙂 compression function is awesome 🙂

  12. Thanks Bro. 🙂

  13. Dana said

    Hi,
    when i tried to run the zip function QTP says it doesn’t recognized the Wscript object. the error i got was during run time:
    “Object required: ‘Wscript’

    Line (378): “Wscript.Sleep(500)”.”

    do you knew anything about it?

    thanks,

  14. Hi dana,

    it seems Wscript object is not created
    set wshshell = wscript.CreateObject(“wscript.shell”)
    wscript.sleep 2000

  15. Rahul said

    Rajiv, What if I want to zip up a database file located on a server or workstation?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: