用vbs实现zip功能的脚本
程序员文章站
2022-08-27 18:20:35
压缩: function fzip(ssourcefolder,stargetzipfile) 'this function&n...
压缩:
function fzip(ssourcefolder,stargetzipfile)
'this function will add all of the files in a source folder to a zip file
'using windows' native folder zip capability.
dim oshellapp, ofso, ierr, serrsource, serrdescription
set oshellapp = createobject("shell.application")
set ofso = createobject("scripting.filesystemobject")
'the source folder needs to have a \ on the end
if right(ssourcefolder,1) <> "\" then ssourcefolder = ssourcefolder & "\"
on error resume next
'if a target zip exists already, delete it
if ofso.fileexists(stargetzipfile) then ofso.deletefile stargetzipfile,true
ierr = err.number
serrsource = err.source
serrdescription = err.description
on error goto 0
if ierr <> 0 then
fzip = array(ierr,serrsource,serrdescription)
exit function
end if
on error resume next
'write the fileheader for a blank zipfile.
ofso.opentextfile(stargetzipfile, 2, true).write "pk" & chr(5) & chr(6) & string(18, chr(0))
ierr = err.number
serrsource = err.source
serrdescription = err.description
on error goto 0
if ierr <> 0 then
fzip = array(ierr,serrsource,serrdescription)
exit function
end if
on error resume next
'start copying files into the zip from the source folder.
oshellapp.namespace(stargetzipfile).copyhere oshellapp.namespace(ssourcefolder).items
ierr = err.number
serrsource = err.source
serrdescription = err.description
on error goto 0
if ierr <> 0 then
fzip = array(ierr,serrsource,serrdescription)
exit function
end if
'because the copying occurs in a separate process, the script will just continue. run a do...loop to prevent the function
'from exiting until the file is finished zipping.
do until oshellapp.namespace(stargetzipfile).items.count = oshellapp.namespace(ssourcefolder).items.count
wscript.sleep 1500'如果不成功,增加一下秒数
loop
fzip = array(0,"","")
end function
call fzip ("c:\vbs","c:\vbs.zip")
解压缩:
function funzip(szipfile,stargetfolder)
'create the shell.application object
dim oshellapp:set oshellapp = createobject("shell.application")
'create the file system object
dim ofso:set ofso = createobject("scripting.filesystemobject")
'create the target folder if it isn't already there
if not ofso.folderexists(stargetfolder) then ofso.createfolder stargetfolder
'extract the files from the zip into the folder
oshellapp.namespace(stargetfolder).copyhere oshellapp.namespace(szipfile).items
'this is a seperate process, so the script would continue even if the unzipping is not done
'to prevent this, we run a do...loop once a second checking to see if the number of files
'in the target folder equals the number of files in the zipfile. if so, we continue.
do
wscript.sleep 1000‘有时需要更改
loop while ofso.getfolder(stargetfolder).files.count < oshellapp.namespace(szipfile).items.count
end function
function fzip(ssourcefolder,stargetzipfile)
'this function will add all of the files in a source folder to a zip file
'using windows' native folder zip capability.
dim oshellapp, ofso, ierr, serrsource, serrdescription
set oshellapp = createobject("shell.application")
set ofso = createobject("scripting.filesystemobject")
'the source folder needs to have a \ on the end
if right(ssourcefolder,1) <> "\" then ssourcefolder = ssourcefolder & "\"
on error resume next
'if a target zip exists already, delete it
if ofso.fileexists(stargetzipfile) then ofso.deletefile stargetzipfile,true
ierr = err.number
serrsource = err.source
serrdescription = err.description
on error goto 0
if ierr <> 0 then
fzip = array(ierr,serrsource,serrdescription)
exit function
end if
on error resume next
'write the fileheader for a blank zipfile.
ofso.opentextfile(stargetzipfile, 2, true).write "pk" & chr(5) & chr(6) & string(18, chr(0))
ierr = err.number
serrsource = err.source
serrdescription = err.description
on error goto 0
if ierr <> 0 then
fzip = array(ierr,serrsource,serrdescription)
exit function
end if
on error resume next
'start copying files into the zip from the source folder.
oshellapp.namespace(stargetzipfile).copyhere oshellapp.namespace(ssourcefolder).items
ierr = err.number
serrsource = err.source
serrdescription = err.description
on error goto 0
if ierr <> 0 then
fzip = array(ierr,serrsource,serrdescription)
exit function
end if
'because the copying occurs in a separate process, the script will just continue. run a do...loop to prevent the function
'from exiting until the file is finished zipping.
do until oshellapp.namespace(stargetzipfile).items.count = oshellapp.namespace(ssourcefolder).items.count
wscript.sleep 1500'如果不成功,增加一下秒数
loop
fzip = array(0,"","")
end function
call fzip ("c:\vbs","c:\vbs.zip")
解压缩:
function funzip(szipfile,stargetfolder)
'create the shell.application object
dim oshellapp:set oshellapp = createobject("shell.application")
'create the file system object
dim ofso:set ofso = createobject("scripting.filesystemobject")
'create the target folder if it isn't already there
if not ofso.folderexists(stargetfolder) then ofso.createfolder stargetfolder
'extract the files from the zip into the folder
oshellapp.namespace(stargetfolder).copyhere oshellapp.namespace(szipfile).items
'this is a seperate process, so the script would continue even if the unzipping is not done
'to prevent this, we run a do...loop once a second checking to see if the number of files
'in the target folder equals the number of files in the zipfile. if so, we continue.
do
wscript.sleep 1000‘有时需要更改
loop while ofso.getfolder(stargetfolder).files.count < oshellapp.namespace(szipfile).items.count
end function
上一篇: 文本文件分割脚本(VBS)
下一篇: ASP.NET中画图形验证码的实现代码