用vbs实现zip功能的脚本

所属分类: 脚本专栏 / vbs 阅读数: 623
收藏 0 赞 0 分享
压缩: 
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 
更多精彩内容其他人还在看

WINDOWS特有的消息常量标识符(vb,vbs常用)

这篇文章主要介绍了WINDOWS特有的消息常量标识符,vb,vbs中经常用到,需要的朋友可以参考一下
收藏 0 赞 0 分享

vbs 复制指定文件到指定目录下

这篇文章主要介绍了vbs 复制指定文件到指定目录下,需要的朋友可以参考下
收藏 0 赞 0 分享

Vbs备份指定文件到指定目录并且以日期重命名的实现代码

这篇文章主要介绍了Vbs备份指定文件到指定目录并且以日期重命名的实现代码,需要的朋友可以参考下
收藏 0 赞 0 分享

VBS递归创建多级目录文件夹的方法

这篇文章主要介绍了VBS递归创建多级目录文件夹的方法,主要使用的是vbs fso的GetParentFolderName与CreateFolder函数,需要的朋友可以参考下
收藏 0 赞 0 分享

VBS日期(时间)格式化函数代码

这篇文章主要介绍了VBS日期(时间)格式化函数代码,需要的朋友可以参考下
收藏 0 赞 0 分享

用vbs实现文本循环读取

因为测试中需要读取一批URL数据进行浏览,为了方便使用txt保存配置url,另外脚本之家特为大家补充了比较好的配置读取脚本,需要的朋友可以参考一下
收藏 0 赞 0 分享

磁盘IO利用率监控VBS脚本(windows)

这篇文章主要为大家分享监测windows主机IO利用率的脚本代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

监测windows主机网络接口利用率的vbs代码

这篇文章主要介绍了监测windows主机网络接口利用率的vbs代码,不用任何软件没有安全隐患,学习vbs的朋友可以参考一下
收藏 0 赞 0 分享

vbs定期监控 值个班,定期瞄一下

有些时候需要定期去执行一段程序,怎么办?这里提供一个方法,虽然有点绕,但效果还不错,需要的朋友可以参考下
收藏 0 赞 0 分享

VBS怎么获取指定目录下的文件列表

这篇文章主要介绍了VBS怎么获取指定目录下的文件列表,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多