OA_Zip.bas 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. Option Explicit
  2. Option Base 0
  3. Private Declare Sub Sleep Lib "kernel32" (ByVal dwMiliseconds As Long)
  4. Public Sub zipfile(zipPath As String, filePath As String)
  5. createzip zipPath
  6. addfile zipPath, filePath
  7. End Sub
  8. Public Sub zipfolder(zipPath As String, folderpath As String)
  9. Dim file As Variant
  10. createzip zipPath
  11. For Each file In CreateObject("Scripting.FileSystemObject").GetFolder(folderpath).Files
  12. addfile zipPath, CStr(file)
  13. Next file
  14. End Sub
  15. Private Sub addfile(zipPath As String, filePath As String)
  16. Dim sh As Shell32.Shell, fdr As Shell32.Folder, cntItems As Integer 'cnt = Count
  17. Set sh = CreateObject("Shell.Application")
  18. Set fdr = sh.NameSpace(zipPath)
  19. cntItems = fdr.Items.count
  20. fdr.CopyHere filePath, 4 + 16 + 1024
  21. Do
  22. Sleep 1000
  23. Loop Until cntItems < fdr.Items.count
  24. Set fdr = Nothing
  25. Set sh = Nothing
  26. End Sub
  27. Private Sub createzip(zipPath As String)
  28. Dim fso As Scripting.FileSystemObject
  29. Set fso = CreateObject("Scripting.FileSystemObject")
  30. If fso.FileExists(zipPath) Then
  31. fso.DeleteFile zipPath
  32. End If
  33. fso.CreateTextFile(zipPath).Write "PK" & Chr(5) & Chr(6) & String(18, Chr(0))
  34. Set fso = Nothing
  35. End Sub