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”
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)”.
rajivkumarnandvani said
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..
anil said
rajiv i used your code and it worked perfectly, thanks for a nice forum like this
regards-anil
rajivkumarnandvani said
Thanks man 🙂
Bharat said
Greta job done man.
rajivkumarnandvani said
Thanks 🙂
mahesh said
Will it replace the folder if it already exists in the destination location?
rajivkumarnandvani said
No it will not replace. you can check if file exist then delete using FSO(File system object) DeleteFile method
Thanks
praveen said
How to unzip ‘sample.txt.z’ file using VB script or QTP ?
rajivkumarnandvani said
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
rk said
its reaally good work dude 🙂 compression function is awesome 🙂
rajivkumarnandvani said
Thanks Bro. 🙂
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,
rajivkumarnandvani said
Hi dana,
it seems Wscript object is not created
set wshshell = wscript.CreateObject(“wscript.shell”)
wscript.sleep 2000
Rahul said
Rajiv, What if I want to zip up a database file located on a server or workstation?