VBZipBas.bas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. Attribute VB_Name = "VBZipBas"
  2. Option Explicit
  3. '---------------------------------------------------------------
  4. '-- Please Do Not Remove These Comments!!!
  5. '---------------------------------------------------------------
  6. '-- Sample VB 5 code to drive zip32.dll
  7. '-- Contributed to the Info-ZIP project by Mike Le Voi
  8. '--
  9. '-- Contact me at: mlevoi@modemss.brisnet.org.au
  10. '--
  11. '-- Visit my home page at: http://modemss.brisnet.org.au/~mlevoi
  12. '--
  13. '-- Use this code at your own risk. Nothing implied or warranted
  14. '-- to work on your machine :-)
  15. '---------------------------------------------------------------
  16. '--
  17. '-- The Source Code Is Freely Available From Info-ZIP At:
  18. '-- http://www.cdrom.com/pub/infozip/infozip.html
  19. '--
  20. '-- A Very Special Thanks To Mr. Mike Le Voi
  21. '-- And Mr. Mike White Of The Info-ZIP
  22. '-- For Letting Me Use And Modify His Orginal
  23. '-- Visual Basic 5.0 Code! Thank You Mike Le Voi.
  24. '---------------------------------------------------------------
  25. '--
  26. '-- Contributed To The Info-ZIP Project By Raymond L. King
  27. '-- Modified June 21, 1998
  28. '-- By Raymond L. King
  29. '-- Custom Software Designers
  30. '--
  31. '-- Contact Me At: king@ntplx.net
  32. '-- ICQ 434355
  33. '-- Or Visit Our Home Page At: http://www.ntplx.net/~king
  34. '--
  35. '---------------------------------------------------------------
  36. '
  37. ' This is the original example with some small changes. Only
  38. ' use with the original Zip32.dll (compiled from Zip 2.31 or
  39. ' later). Do not use this VB example with Zip32z64.dll
  40. ' (compiled from Zip 3.0). To check the version of a dll,
  41. ' right click on the file and check properties.
  42. '
  43. ' 6/24/2008 Ed Gordon
  44. '---------------------------------------------------------------
  45. ' Usage notes:
  46. '
  47. ' This code uses Zip32.dll. You DO NOT need to register the
  48. ' DLL to use it. You also DO NOT need to reference it in your
  49. ' VB project. You DO have to copy the DLL to your SYSTEM
  50. ' directory, your VB project directory, or place it in a directory
  51. ' on your command PATH.
  52. '
  53. ' A bug has been found in the Zip32.dll when called from VB. If
  54. ' you try to pass any values other than NULL in the ZPOPT strings
  55. ' Date, szRootDir, or szTempDir they get converted from the
  56. ' VB internal wide character format to temporary byte strings by
  57. ' the calling interface as they are supposed to. However when
  58. ' ZpSetOptions returns the passed strings are deallocated unless the
  59. ' VB debugger prevents it by a break between ZpSetOptions and
  60. ' ZpArchive. When Zip32.dll uses these pointers later it
  61. ' can result in unpredictable behavior. A kluge is available
  62. ' for Zip32.dll, just replacing api.c in Zip 2.3, but better to just
  63. ' use the new Zip32z64.dll where these bugs are fixed. However,
  64. ' the kluge has been added to Zip 2.31 and later and these are
  65. ' now stable. To determine the version of the dll you have
  66. ' right click on it, select the Version tab, and verify the
  67. ' Product Version is at least 2.31.
  68. '
  69. ' Another bug is where -R is used with some other options and can
  70. ' crash the dll. This is a bug in how zip processes the command
  71. ' line and should be mostly fixed in Zip 2.31. If you run into
  72. ' problems try using -r instead for recursion. The bug is fixed
  73. ' in Zip 3.0 but note that Zip 3.0 creates dll zip32z64.dll and
  74. ' it is not compatible with older VB including this example. See
  75. ' the new VB example code included with Zip 3.0 for calling
  76. ' interface changes.
  77. '
  78. ' Note that Zip32 is probably not thread safe. It may be made
  79. ' thread safe in a later version, but for now only one thread in
  80. ' one program should use the DLL at a time. Unlike Zip, UnZip is
  81. ' probably thread safe, but an exception to this has been
  82. ' found. See the UnZip documentation for the latest on this.
  83. '
  84. ' All code in this VB project is provided under the Info-Zip license.
  85. '
  86. ' If you have any questions please contact Info-Zip at
  87. ' http://www.info-zip.org.
  88. '
  89. ' 4/29/2004 EG (Updated 3/1/2005, 6/24/2008 EG)
  90. '
  91. '---------------------------------------------------------------
  92. '-- C Style argv
  93. '-- Holds The Zip Archive Filenames
  94. ' Max for this just over 8000 as each pointer takes up 4 bytes and
  95. ' VB only allows 32 kB of local variables and that includes function
  96. ' parameters. - 3/19/2004 EG
  97. '
  98. Public Type ZIPnames
  99. zFiles(0 To 99) As String
  100. End Type
  101. '-- Call Back "String"
  102. Public Type ZipCBChar
  103. ch(4096) As Byte
  104. End Type
  105. '-- ZPOPT Is Used To Set The Options In The ZIP32.DLL
  106. Public Type ZPOPT
  107. Date As String ' US Date (8 Bytes Long) "12/31/98"?
  108. szRootDir As String ' Root Directory Pathname (Up To 256 Bytes Long)
  109. szTempDir As String ' Temp Directory Pathname (Up To 256 Bytes Long)
  110. fTemp As Long ' 1 If Temp dir Wanted, Else 0
  111. fSuffix As Long ' Include Suffixes (Not Yet Implemented!)
  112. fEncrypt As Long ' 1 If Encryption Wanted, Else 0
  113. fSystem As Long ' 1 To Include System/Hidden Files, Else 0
  114. fVolume As Long ' 1 If Storing Volume Label, Else 0
  115. fExtra As Long ' 1 If Excluding Extra Attributes, Else 0
  116. fNoDirEntries As Long ' 1 If Ignoring Directory Entries, Else 0
  117. fExcludeDate As Long ' 1 If Excluding Files Earlier Than Specified Date, Else 0
  118. fIncludeDate As Long ' 1 If Including Files Earlier Than Specified Date, Else 0
  119. fVerbose As Long ' 1 If Full Messages Wanted, Else 0
  120. fQuiet As Long ' 1 If Minimum Messages Wanted, Else 0
  121. fCRLF_LF As Long ' 1 If Translate CR/LF To LF, Else 0
  122. fLF_CRLF As Long ' 1 If Translate LF To CR/LF, Else 0
  123. fJunkDir As Long ' 1 If Junking Directory Names, Else 0
  124. fGrow As Long ' 1 If Allow Appending To Zip File, Else 0
  125. fForce As Long ' 1 If Making Entries Using DOS File Names, Else 0
  126. fMove As Long ' 1 If Deleting Files Added Or Updated, Else 0
  127. fDeleteEntries As Long ' 1 If Files Passed Have To Be Deleted, Else 0
  128. fUpdate As Long ' 1 If Updating Zip File-Overwrite Only If Newer, Else 0
  129. fFreshen As Long ' 1 If Freshing Zip File-Overwrite Only, Else 0
  130. fJunkSFX As Long ' 1 If Junking SFX Prefix, Else 0
  131. fLatestTime As Long ' 1 If Setting Zip File Time To Time Of Latest File In Archive, Else 0
  132. fComment As Long ' 1 If Putting Comment In Zip File, Else 0
  133. fOffsets As Long ' 1 If Updating Archive Offsets For SFX Files, Else 0
  134. fPrivilege As Long ' 1 If Not Saving Privileges, Else 0
  135. fEncryption As Long ' Read Only Property!!!
  136. fRecurse As Long ' 1 (-r), 2 (-R) If Recursing Into Sub-Directories, Else 0
  137. fRepair As Long ' 1 = Fix Archive, 2 = Try Harder To Fix, Else 0
  138. flevel As Byte ' Compression Level - 0 = Stored 6 = Default 9 = Max
  139. End Type
  140. '-- This Structure Is Used For The ZIP32.DLL Function Callbacks
  141. Public Type ZIPUSERFUNCTIONS
  142. ZDLLPrnt As Long ' Callback ZIP32.DLL Print Function
  143. ZDLLCOMMENT As Long ' Callback ZIP32.DLL Comment Function
  144. ZDLLPASSWORD As Long ' Callback ZIP32.DLL Password Function
  145. ZDLLSERVICE As Long ' Callback ZIP32.DLL Service Function
  146. End Type
  147. '-- Local Declarations
  148. Public ZOPT As ZPOPT
  149. Public ZUSER As ZIPUSERFUNCTIONS
  150. '-- This Assumes ZIP32.DLL Is In Your \Windows\System Directory!
  151. '-- (alternatively, a copy of ZIP32.DLL needs to be located in the program
  152. '-- directory or in some other directory listed in PATH.)
  153. Private Declare Function ZpInit Lib "zip32.dll" _
  154. (ByRef Zipfun As ZIPUSERFUNCTIONS) As Long '-- Set Zip Callbacks
  155. Private Declare Function ZpSetOptions Lib "zip32.dll" _
  156. (ByRef Opts As ZPOPT) As Long '-- Set Zip Options
  157. Private Declare Function ZpGetOptions Lib "zip32.dll" _
  158. () As ZPOPT '-- Used To Check Encryption Flag Only
  159. Private Declare Function ZpArchive Lib "zip32.dll" _
  160. (ByVal argc As Long, ByVal funame As String, _
  161. ByRef argv As ZIPnames) As Long '-- Real Zipping Action
  162. '-------------------------------------------------------
  163. '-- Public Variables For Setting The ZPOPT Structure...
  164. '-- (WARNING!!!) You Must Set The Options That You
  165. '-- Want The ZIP32.DLL To Do!
  166. '-- Before Calling VBZip32!
  167. '--
  168. '-- NOTE: See The Above ZPOPT Structure Or The VBZip32
  169. '-- Function, For The Meaning Of These Variables
  170. '-- And How To Use And Set Them!!!
  171. '-- These Parameters Must Be Set Before The Actual Call
  172. '-- To The VBZip32 Function!
  173. '-------------------------------------------------------
  174. Public zDate As String
  175. Public zRootDir As String
  176. Public zTempDir As String
  177. Public zSuffix As Integer
  178. Public zEncrypt As Integer
  179. Public zSystem As Integer
  180. Public zVolume As Integer
  181. Public zExtra As Integer
  182. Public zNoDirEntries As Integer
  183. Public zExcludeDate As Integer
  184. Public zIncludeDate As Integer
  185. Public zVerbose As Integer
  186. Public zQuiet As Integer
  187. Public zCRLF_LF As Integer
  188. Public zLF_CRLF As Integer
  189. Public zJunkDir As Integer
  190. Public zRecurse As Integer
  191. Public zGrow As Integer
  192. Public zForce As Integer
  193. Public zMove As Integer
  194. Public zDelEntries As Integer
  195. Public zUpdate As Integer
  196. Public zFreshen As Integer
  197. Public zJunkSFX As Integer
  198. Public zLatestTime As Integer
  199. Public zComment As Integer
  200. Public zOffsets As Integer
  201. Public zPrivilege As Integer
  202. Public zEncryption As Integer
  203. Public zRepair As Integer
  204. Public zLevel As Integer
  205. '-- Public Program Variables
  206. Public zArgc As Integer ' Number Of Files To Zip Up
  207. Public zZipFileName As String ' The Zip File Name ie: Myzip.zip
  208. Public zZipFileNames As ZIPnames ' File Names To Zip Up
  209. Public zZipInfo As String ' Holds The Zip File Information
  210. '-- Public Constants
  211. '-- For Zip & UnZip Error Codes!
  212. Public Const ZE_OK = 0 ' Success (No Error)
  213. Public Const ZE_EOF = 2 ' Unexpected End Of Zip File Error
  214. Public Const ZE_FORM = 3 ' Zip File Structure Error
  215. Public Const ZE_MEM = 4 ' Out Of Memory Error
  216. Public Const ZE_LOGIC = 5 ' Internal Logic Error
  217. Public Const ZE_BIG = 6 ' Entry Too Large To Split Error
  218. Public Const ZE_NOTE = 7 ' Invalid Comment Format Error
  219. Public Const ZE_TEST = 8 ' Zip Test (-T) Failed Or Out Of Memory Error
  220. Public Const ZE_ABORT = 9 ' User Interrupted Or Termination Error
  221. Public Const ZE_TEMP = 10 ' Error Using A Temp File
  222. Public Const ZE_READ = 11 ' Read Or Seek Error
  223. Public Const ZE_NONE = 12 ' Nothing To Do Error
  224. Public Const ZE_NAME = 13 ' Missing Or Empty Zip File Error
  225. Public Const ZE_WRITE = 14 ' Error Writing To A File
  226. Public Const ZE_CREAT = 15 ' Could't Open To Write Error
  227. Public Const ZE_PARMS = 16 ' Bad Command Line Argument Error
  228. Public Const ZE_OPEN = 18 ' Could Not Open A Specified File To Read Error
  229. '-- These Functions Are For The ZIP32.DLL
  230. '--
  231. '-- Puts A Function Pointer In A Structure
  232. '-- For Use With Callbacks...
  233. Public Function FnPtr(ByVal lp As Long) As Long
  234. FnPtr = lp
  235. End Function
  236. '-- Callback For ZIP32.DLL - DLL Print Function
  237. Public Function ZDLLPrnt(ByRef fname As ZipCBChar, ByVal x As Long) As Long
  238. Dim s0 As String
  239. Dim xx As Long
  240. '-- Always Put This In Callback Routines!
  241. On Error Resume Next
  242. s0 = ""
  243. '-- Get Zip32.DLL Message For processing
  244. For xx = 0 To x
  245. If fname.ch(xx) = 0 Then
  246. Exit For
  247. Else
  248. s0 = s0 + Chr(fname.ch(xx))
  249. End If
  250. Next
  251. '----------------------------------------------
  252. '-- This Is Where The DLL Passes Back Messages
  253. '-- To You! You Can Change The Message Printing
  254. '-- Below Here!
  255. '----------------------------------------------
  256. '-- Display Zip File Information
  257. '-- zZipInfo = zZipInfo & s0
  258. Form1.Print s0;
  259. DoEvents
  260. ZDLLPrnt = 0
  261. End Function
  262. '-- Callback For ZIP32.DLL - DLL Service Function
  263. Public Function ZDLLServ(ByRef mname As ZipCBChar, ByVal x As Long) As Long
  264. ' x is the size of the file
  265. Dim s0 As String
  266. Dim xx As Long
  267. '-- Always Put This In Callback Routines!
  268. On Error Resume Next
  269. s0 = ""
  270. '-- Get Zip32.DLL Message For processing
  271. For xx = 0 To 4096
  272. If mname.ch(xx) = 0 Then
  273. Exit For
  274. Else
  275. s0 = s0 + Chr(mname.ch(xx))
  276. End If
  277. Next
  278. ' Form1.Print "-- " & s0 & " - " & x & " bytes"
  279. ' This is called for each zip entry.
  280. ' mname is usually the null terminated file name and x the file size.
  281. ' s0 has trimmed file name as VB string.
  282. ' At this point, s0 contains the message passed from the DLL
  283. ' It is up to the developer to code something useful here :)
  284. ZDLLServ = 0 ' Setting this to 1 will abort the zip!
  285. End Function
  286. '-- Callback For ZIP32.DLL - DLL Password Function
  287. Public Function ZDLLPass(ByRef p As ZipCBChar, _
  288. ByVal n As Long, ByRef m As ZipCBChar, _
  289. ByRef Name As ZipCBChar) As Integer
  290. Dim prompt As String
  291. Dim xx As Integer
  292. Dim szpassword As String
  293. '-- Always Put This In Callback Routines!
  294. On Error Resume Next
  295. ZDLLPass = 1
  296. '-- If There Is A Password Have The User Enter It!
  297. '-- This Can Be Changed
  298. szpassword = InputBox("Please Enter The Password!")
  299. '-- The User Did Not Enter A Password So Exit The Function
  300. If szpassword = "" Then Exit Function
  301. '-- User Entered A Password So Proccess It
  302. For xx = 0 To 255
  303. If m.ch(xx) = 0 Then
  304. Exit For
  305. Else
  306. prompt = prompt & Chr(m.ch(xx))
  307. End If
  308. Next
  309. For xx = 0 To n - 1
  310. p.ch(xx) = 0
  311. Next
  312. For xx = 0 To Len(szpassword) - 1
  313. p.ch(xx) = Asc(Mid(szpassword, xx + 1, 1))
  314. Next
  315. p.ch(xx) = Chr(0) ' Put Null Terminator For C
  316. ZDLLPass = 0
  317. End Function
  318. '-- Callback For ZIP32.DLL - DLL Comment Function
  319. Public Function ZDLLComm(ByRef s1 As ZipCBChar) As Integer
  320. Dim xx%, szcomment$
  321. '-- Always Put This In Callback Routines!
  322. On Error Resume Next
  323. ZDLLComm = 1
  324. szcomment = InputBox("Enter the comment")
  325. If szcomment = "" Then Exit Function
  326. For xx = 0 To Len(szcomment) - 1
  327. s1.ch(xx) = Asc(Mid$(szcomment, xx + 1, 1))
  328. Next xx
  329. s1.ch(xx) = Chr(0) ' Put null terminator for C
  330. End Function
  331. '-- Main ZIP32.DLL Subroutine.
  332. '-- This Is Where It All Happens!!!
  333. '--
  334. '-- (WARNING!) Do Not Change This Function!!!
  335. '--
  336. Public Function VBZip32() As Long
  337. Dim retcode As Long
  338. On Error Resume Next '-- Nothing Will Go Wrong :-)
  339. retcode = 0
  340. '-- Set Address Of ZIP32.DLL Callback Functions
  341. '-- (WARNING!) Do Not Change!!!
  342. ZUSER.ZDLLPrnt = FnPtr(AddressOf ZDLLPrnt)
  343. ZUSER.ZDLLPASSWORD = FnPtr(AddressOf ZDLLPass)
  344. ZUSER.ZDLLCOMMENT = FnPtr(AddressOf ZDLLComm)
  345. ZUSER.ZDLLSERVICE = FnPtr(AddressOf ZDLLServ)
  346. '-- Set ZIP32.DLL Callbacks
  347. retcode = ZpInit(ZUSER)
  348. If retcode = 0 Then
  349. MsgBox "Zip32.dll did not initialize. Is it in the current directory " & _
  350. "or on the command path?", vbOKOnly, "VB Zip"
  351. Exit Function
  352. End If
  353. '-- Setup ZIP32 Options
  354. '-- (WARNING!) Do Not Change!
  355. ZOPT.Date = zDate ' "12/31/79"? US Date?
  356. ZOPT.szRootDir = zRootDir ' Root Directory Pathname
  357. ZOPT.szTempDir = zTempDir ' Temp Directory Pathname
  358. ZOPT.fSuffix = zSuffix ' Include Suffixes (Not Yet Implemented)
  359. ZOPT.fEncrypt = zEncrypt ' 1 If Encryption Wanted
  360. ZOPT.fSystem = zSystem ' 1 To Include System/Hidden Files
  361. ZOPT.fVolume = zVolume ' 1 If Storing Volume Label
  362. ZOPT.fExtra = zExtra ' 1 If Including Extra Attributes
  363. ZOPT.fNoDirEntries = zNoDirEntries ' 1 If Ignoring Directory Entries
  364. ZOPT.fExcludeDate = zExcludeDate ' 1 If Excluding Files Earlier Than A Specified Date
  365. ZOPT.fIncludeDate = zIncludeDate ' 1 If Including Files Earlier Than A Specified Date
  366. ZOPT.fVerbose = zVerbose ' 1 If Full Messages Wanted
  367. ZOPT.fQuiet = zQuiet ' 1 If Minimum Messages Wanted
  368. ZOPT.fCRLF_LF = zCRLF_LF ' 1 If Translate CR/LF To LF
  369. ZOPT.fLF_CRLF = zLF_CRLF ' 1 If Translate LF To CR/LF
  370. ZOPT.fJunkDir = zJunkDir ' 1 If Junking Directory Names
  371. ZOPT.fGrow = zGrow ' 1 If Allow Appending To Zip File
  372. ZOPT.fForce = zForce ' 1 If Making Entries Using DOS Names
  373. ZOPT.fMove = zMove ' 1 If Deleting Files Added Or Updated
  374. ZOPT.fDeleteEntries = zDelEntries ' 1 If Files Passed Have To Be Deleted
  375. ZOPT.fUpdate = zUpdate ' 1 If Updating Zip File-Overwrite Only If Newer
  376. ZOPT.fFreshen = zFreshen ' 1 If Freshening Zip File-Overwrite Only
  377. ZOPT.fJunkSFX = zJunkSFX ' 1 If Junking SFX Prefix
  378. ZOPT.fLatestTime = zLatestTime ' 1 If Setting Zip File Time To Time Of Latest File In Archive
  379. ZOPT.fComment = zComment ' 1 If Putting Comment In Zip File
  380. ZOPT.fOffsets = zOffsets ' 1 If Updating Archive Offsets For SFX Files
  381. ZOPT.fPrivilege = zPrivilege ' 1 If Not Saving Privelages
  382. ZOPT.fEncryption = zEncryption ' Read Only Property!
  383. ZOPT.fRecurse = zRecurse ' 1 or 2 If Recursing Into Subdirectories
  384. ZOPT.fRepair = zRepair ' 1 = Fix Archive, 2 = Try Harder To Fix
  385. ZOPT.flevel = zLevel ' Compression Level - (0 To 9) Should Be 0!!!
  386. '-- Set ZIP32.DLL Options
  387. retcode = ZpSetOptions(ZOPT)
  388. '-- Go Zip It Them Up!
  389. retcode = ZpArchive(zArgc, zZipFileName, zZipFileNames)
  390. '-- Return The Function Code
  391. VBZip32 = retcode
  392. End Function