VBA压缩与解压缩源码

2017-09-08 22:42:00
网络摘录
转贴
134


VBA本身没有压缩和解压缩的函数,但可调用zip.dll  unzip.dll 或 winrar.exe 命令行方式来实现对文件的压缩与解压缩


1、批量解压缩(一次性解压指定文件夹中所有rar文件)
Sub UnRarFile()   '解压缩程序
  Dim Rarexe As String
  Dim RAR As String
  Dim Myadd As String
  Dim FileString As String
  Dim Result As Long
    Rarexe = "C:\program files\winrar\winrar.exe" 'rar程序路径
    myRAR = "D:\工资表\*.rar"  '需要解压缩的rar文件,用通配符可以解压所有文件
    Myadd = "D:\工资表"     ' 解压后的文件存放路径
    FileString = Rarexe & " X " & myRAR & " " & Myadd 'rar程序的X命令,用来解压缩文件的字符串
    Result = Shell(FileString, vbHide) '执行解压缩
End Sub

运行效果: 把D盘的工资表文件夹中的所有压缩文件一次性解压。

2、批量压缩文件(一次性压缩指定文件夹中所有xls文件)
   
Sub RarFile()   '压缩程序
  Dim Rarexe As String
  Dim myRAR As String
  Dim Myfile As String
  Dim FileString As String
  Dim Result As Long
    Rarexe = "C:\program files\winrar\winrar.exe" 'rar程序路径
    myRAR = "D:\工资表\工资表.rar"  '压缩后的文件名
    Myfile = "D:\工资表\*.xls"    ' 指定要压缩的文件
    FileString = Rarexe & " A " & myRAR & " " & Myfile 'rar程序的A命令压缩文件的字符串
    Result = Shell(FileString, vbHide) '执行压缩
End Sub

___________________________________________________________________________
Set oba = CreateObject("Wscript.shell")
'[压缩]
oba.Run "winrar a c:\test.rar c:\*.txt",0,True
'[解压缩]
oba.Run "winrar x -o+ C:\test.rar *.txt C:\test",0,True
Set oba = Nothing

分享