home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / CODE_UPLOAD8755882000.psc / mUnzip.bas < prev    next >
Encoding:
BASIC Source File  |  2000-08-01  |  12.1 KB  |  402 lines

  1. Attribute VB_Name = "mUnzip"
  2. Option Explicit
  3.  
  4. ' ======================================================================================
  5. ' Name:     mUnzip
  6. ' Author:   Steve McMahon (steve@vbaccelerator.com)
  7. ' Date:     1 December 2000
  8. '
  9. ' Requires: Info-ZIP's Unzip32.DLL v5.40, renamed to vbuzip10.dll
  10. '           cUnzip.cls
  11. '
  12. ' Copyright ⌐ 2000 Steve McMahon for vbAccelerator
  13. ' --------------------------------------------------------------------------------------
  14. ' Visit vbAccelerator - advanced free source code for VB programmers
  15. ' http://vbaccelerator.com
  16. ' --------------------------------------------------------------------------------------
  17. '
  18. ' Part of the implementation of cUnzip.cls, a class which gives a
  19. ' simple interface to Info-ZIP's excellent, free unzipping library
  20. ' (Unzip32.DLL).
  21. '
  22. ' This sample uses decompression code by the Info-ZIP group.  The
  23. ' original Info-Zip sources are freely available from their website
  24. ' at
  25. '     http://www.cdrcom.com/pubs/infozip/
  26. '
  27. ' Please ensure you visit the site and read their free source licensing
  28. ' information and requirements before using their code in your own
  29. ' application.
  30. '
  31. ' ======================================================================================
  32.  
  33. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
  34.     lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
  35.  
  36. ' argv
  37. Private Type UNZIPnames
  38.     s(0 To 1023) As String
  39. End Type
  40.  
  41. ' Callback large "string" (sic)
  42. Private Type CBChar
  43.     ch(0 To 32800) As Byte
  44. End Type
  45.  
  46. ' Callback small "string" (sic)
  47. Private Type CBCh
  48.     ch(0 To 255) As Byte
  49. End Type
  50.  
  51. ' DCL structure
  52. Public Type DCLIST
  53.    ExtractOnlyNewer As Long      ' 1 to extract only newer
  54.    SpaceToUnderScore As Long     ' 1 to convert spaces to underscore
  55.    PromptToOverwrite As Long     ' 1 if overwriting prompts required
  56.    fQuiet As Long                ' 0 = all messages, 1 = few messages, 2 = no messages
  57.    ncflag As Long                ' write to stdout if 1
  58.    ntflag As Long                ' test zip file
  59.    nvflag As Long                ' verbose listing
  60.    nUflag As Long                ' "update" (extract only newer/new files)
  61.    nzflag As Long                ' display zip file comment
  62.    ndflag As Long                ' all args are files/dir to be extracted
  63.    noflag As Long                ' 1 if always overwrite files
  64.    naflag As Long                ' 1 to do end-of-line translation
  65.    nZIflag As Long               ' 1 to get zip info
  66.    C_flag As Long                ' 1 to be case insensitive
  67.    fPrivilege As Long            ' zip file name
  68.    lpszZipFN As String           ' directory to extract to.
  69.    lpszExtractDir As String
  70. End Type
  71.  
  72. Private Type USERFUNCTION
  73.    ' Callbacks:
  74.    lptrPrnt As Long           ' Pointer to application's print routine
  75.    lptrSound As Long          ' Pointer to application's sound routine.  NULL if app doesn't use sound
  76.    lptrReplace As Long        ' Pointer to application's replace routine.
  77.    lptrPassword As Long       ' Pointer to application's password routine.
  78.    lptrMessage As Long        ' Pointer to application's routine for
  79.                               ' displaying information about specific files in the archive
  80.                               ' used for listing the contents of the archive.
  81.    lptrService As Long        ' callback function designed to be used for allowing the
  82.                               ' app to process Windows messages, or cancelling the operation
  83.                               ' as well as giving option of progress.  If this function returns
  84.                               ' non-zero, it will terminate what it is doing.  It provides the app
  85.                               ' with the name of the archive member it has just processed, as well
  86.                               ' as the original size.
  87.                               
  88.    ' Values filled in after processing:
  89.    lTotalSizeComp As Long     ' Value to be filled in for the compressed total size, excluding
  90.                               ' the archive header and central directory list.
  91.    lTotalSize As Long         ' Total size of all files in the archive
  92.    lCompFactor As Long        ' Overall archive compression factor
  93.    lNumMembers As Long        ' Total number of files in the archive
  94.    cchComment As Integer      ' Flag indicating whether comment in archive.
  95. End Type
  96.  
  97. Public Type ZIPVERSIONTYPE
  98.    major As Byte
  99.    minor As Byte
  100.    patchlevel As Byte
  101.    not_used As Byte
  102. End Type
  103.  
  104. Public Type UZPVER
  105.     structlen As Long         ' Length of structure
  106.     flag As Long              ' 0 is beta, 1 uses zlib
  107.     betalevel As String * 10  ' e.g "g BETA"
  108.     date As String * 20       ' e.g. "4 Sep 95" (beta) or "4 September 1995"
  109.     zlib As String * 10       ' e.g. "1.0.5 or NULL"
  110.     Unzip As ZIPVERSIONTYPE
  111.     zipinfo As ZIPVERSIONTYPE
  112.     os2dll As ZIPVERSIONTYPE
  113.     windll As ZIPVERSIONTYPE
  114. End Type
  115.  
  116. Private Declare Function Wiz_SingleEntryUnzip Lib "vbuzip10.dll" _
  117.   (ByVal ifnc As Long, ByRef ifnv As UNZIPnames, _
  118.    ByVal xfnc As Long, ByRef xfnv As UNZIPnames, _
  119.    dcll As DCLIST, Userf As USERFUNCTION) As Long
  120. Public Declare Sub UzpVersion2 Lib "vbuzip10.dll" (uzpv As UZPVER)
  121.  
  122. ' Object for callbacks:
  123. Private m_cUnzip As cUnzip
  124. Private m_bCancel As Boolean
  125.  
  126. Private Function plAddressOf(ByVal lPtr As Long) As Long
  127.    ' VB Bug workaround fn
  128.    plAddressOf = lPtr
  129. End Function
  130.  
  131. Private Sub UnzipMessageCallBack( _
  132.       ByVal ucsize As Long, _
  133.       ByVal csiz As Long, _
  134.       ByVal cfactor As Integer, _
  135.       ByVal mo As Integer, _
  136.       ByVal dy As Integer, _
  137.       ByVal yr As Integer, _
  138.       ByVal hh As Integer, _
  139.       ByVal mm As Integer, _
  140.       ByVal c As Byte, _
  141.       ByRef fname As CBCh, _
  142.       ByRef meth As CBCh, _
  143.       ByVal crc As Long, _
  144.       ByVal fCrypt As Byte _
  145.    )
  146. Dim sFileName As String
  147. Dim sFolder As String
  148. Dim dDate As Date
  149. Dim sMethod As String
  150. Dim iPos As Long
  151.  
  152.    On Error Resume Next
  153.     
  154.    ' Add to unzip class:
  155.    With m_cUnzip
  156.       ' Parse:
  157.       sFileName = StrConv(fname.ch, vbUnicode)
  158.       ParseFileFolder sFileName, sFolder
  159.       dDate = DateSerial(yr, mo, hh)
  160.       dDate = dDate + TimeSerial(hh, mm, 0)
  161.       sMethod = StrConv(meth.ch, vbUnicode)
  162.       iPos = InStr(sMethod, vbNullChar)
  163.       If (iPos > 1) Then
  164.          sMethod = Left$(sMethod, iPos - 1)
  165.       End If
  166.     
  167.       Debug.Print fCrypt
  168.       .DirectoryListAddFile sFileName, sFolder, dDate, csiz, crc, ((fCrypt And 64) = 64), cfactor, sMethod
  169.    End With
  170.    
  171. End Sub
  172.  
  173. Private Function UnzipPrintCallback( _
  174.       ByRef fname As CBChar, _
  175.       ByVal x As Long _
  176.    ) As Long
  177. Dim iPos As Long
  178. Dim sFIle As String
  179.    On Error Resume Next
  180.    
  181.    ' Check we've got a message:
  182.    If x > 1 And x < 32000 Then
  183.       ' If so, then get the readable portion of it:
  184.       ReDim b(0 To x) As Byte
  185.       CopyMemory b(0), fname, x
  186.       ' Convert to VB string:
  187.       sFIle = StrConv(b, vbUnicode)
  188.       
  189.       ' Fix up backslashes:
  190.       ReplaceSection sFIle, "/", "\"
  191.       
  192.       ' Tell the caller about it
  193.       m_cUnzip.ProgressReport sFIle
  194.    End If
  195.    UnzipPrintCallback = 0
  196. End Function
  197.  
  198. Private Function UnzipPasswordCallBack( _
  199.       ByRef pwd As CBCh, _
  200.       ByVal x As Long, _
  201.       ByRef s2 As CBCh, _
  202.       ByRef Name As CBCh _
  203.    ) As Long
  204.  
  205. Dim bCancel As Boolean
  206. Dim sPassword As String
  207. Dim b() As Byte
  208. Dim lSize As Long
  209.  
  210. On Error Resume Next
  211.  
  212.    ' The default:
  213.    UnzipPasswordCallBack = 1
  214.     
  215.    If m_bCancel Then
  216.       Exit Function
  217.    End If
  218.    
  219.    ' Ask for password:
  220.    m_cUnzip.PasswordRequest sPassword, bCancel
  221.       
  222.    sPassword = Trim$(sPassword)
  223.    
  224.    ' Cancel out if no useful password:
  225.    If bCancel Or Len(sPassword) = 0 Then
  226.       m_bCancel = True
  227.       Exit Function
  228.    End If
  229.    
  230.    ' Put password into return parameter:
  231.    lSize = Len(sPassword)
  232.    If lSize > 254 Then
  233.       lSize = 254
  234.    End If
  235.    b = StrConv(sPassword, vbFromUnicode)
  236.    CopyMemory pwd.ch(0), b(0), lSize
  237.    
  238.    ' Ask UnZip to process it:
  239.    UnzipPasswordCallBack = 0
  240.        
  241. End Function
  242.  
  243. Private Function UnzipReplaceCallback(ByRef fname As CBChar) As Long
  244. Dim eResponse As EUZOverWriteResponse
  245. Dim iPos As Long
  246. Dim sFIle As String
  247.  
  248.    On Error Resume Next
  249.    eResponse = euzDoNotOverwrite
  250.    
  251.    ' Extract the filename:
  252.    sFIle = StrConv(fname.ch, vbUnicode)
  253.    iPos = InStr(sFIle, vbNullChar)
  254.    If (iPos > 1) Then
  255.       sFIle = Left$(sFIle, iPos - 1)
  256.    End If
  257.    
  258.    ' No backslashes:
  259.    ReplaceSection sFIle, "/", "\"
  260.    
  261.    ' Request the overwrite request:
  262.    m_cUnzip.OverwriteRequest sFIle, eResponse
  263.    
  264.    ' Return it to the zipping lib
  265.    UnzipReplaceCallback = eResponse
  266.    
  267. End Function
  268. Private Function UnZipServiceCallback(ByRef mname As CBChar, ByVal x As Long) As Long
  269. Dim iPos As Long
  270. Dim sInfo As String
  271. Dim bCancel As Boolean
  272.     
  273. '-- Always Put This In Callback Routines!
  274. On Error Resume Next
  275.     
  276.    ' Check we've got a message:
  277.    If x > 1 And x < 32000 Then
  278.       ' If so, then get the readable portion of it:
  279.       ReDim b(0 To x) As Byte
  280.       CopyMemory b(0), mname, x
  281.       ' Convert to VB string:
  282.       sInfo = StrConv(b, vbUnicode)
  283.       iPos = InStr(sInfo, vbNullChar)
  284.       If iPos > 0 Then
  285.          sInfo = Left$(sInfo, iPos - 1)
  286.       End If
  287.       ReplaceSection sInfo, "\", "/"
  288.       m_cUnzip.Service sInfo, bCancel
  289.       If bCancel Then
  290.          UnZipServiceCallback = 1
  291.       Else
  292.          UnZipServiceCallback = 0
  293.       End If
  294.    End If
  295.    
  296. End Function
  297.  
  298.  
  299.  
  300. Private Sub ParseFileFolder( _
  301.       ByRef sFileName As String, _
  302.       ByRef sFolder As String _
  303.    )
  304. Dim iPos As Long
  305. Dim iLastPos As Long
  306.  
  307.    iPos = InStr(sFileName, vbNullChar)
  308.    If (iPos <> 0) Then
  309.       sFileName = Left$(sFileName, iPos - 1)
  310.    End If
  311.    
  312.    iLastPos = ReplaceSection(sFileName, "/", "\")
  313.    
  314.    If (iLastPos > 1) Then
  315.       sFolder = Left$(sFileName, iLastPos - 2)
  316.       sFileName = Mid$(sFileName, iLastPos)
  317.    End If
  318.    
  319. End Sub
  320. Private Function ReplaceSection(ByRef sString As String, ByVal sToReplace As String, ByVal sReplaceWith As String) As Long
  321. Dim iPos As Long
  322. Dim iLastPos As Long
  323.    iLastPos = 1
  324.    Do
  325.       iPos = InStr(iLastPos, sString, "/")
  326.       If (iPos > 1) Then
  327.          Mid$(sString, iPos, 1) = "\"
  328.          iLastPos = iPos + 1
  329.       End If
  330.    Loop While Not (iPos = 0)
  331.    ReplaceSection = iLastPos
  332.  
  333. End Function
  334.  
  335. ' Main subroutine
  336. Public Function VBUnzip( _
  337.       cUnzipObject As cUnzip, _
  338.       tDCL As DCLIST, _
  339.       iIncCount As Long, _
  340.       sInc() As String, _
  341.       iExCount As Long, _
  342.       sExc() As String _
  343.    ) As Long
  344. Dim tUser As USERFUNCTION
  345. Dim lR As Long
  346. Dim tInc As UNZIPnames
  347. Dim tExc As UNZIPnames
  348. Dim i As Long
  349.  
  350. On Error GoTo ErrorHandler
  351.  
  352.    Set m_cUnzip = cUnzipObject
  353.    ' Set Callback addresses
  354.    tUser.lptrPrnt = plAddressOf(AddressOf UnzipPrintCallback)
  355.    tUser.lptrSound = 0& ' not supported
  356.    tUser.lptrReplace = plAddressOf(AddressOf UnzipReplaceCallback)
  357.    tUser.lptrPassword = plAddressOf(AddressOf UnzipPasswordCallBack)
  358.    tUser.lptrMessage = plAddressOf(AddressOf UnzipMessageCallBack)
  359.    tUser.lptrService = plAddressOf(AddressOf UnZipServiceCallback)
  360.         
  361.    ' Set files to include/exclude:
  362.    If (iIncCount > 0) Then
  363.       For i = 1 To iIncCount
  364.          tInc.s(i - 1) = sInc(i)
  365.       Next i
  366.       tInc.s(iIncCount) = vbNullChar
  367.    Else
  368.       tInc.s(0) = vbNullChar
  369.    End If
  370.    If (iExCount > 0) Then
  371.       For i = 1 To iExCount
  372.          tExc.s(i - 1) = sExc(i)
  373.       Next i
  374.       tExc.s(iExCount) = vbNullChar
  375.    Else
  376.       tExc.s(0) = vbNullChar
  377.    End If
  378.    m_bCancel = False
  379.    VBUnzip = Wiz_SingleEntryUnzip(iIncCount, tInc, iExCount, tExc, tDCL, tUser)
  380.     
  381.     'Debug.Print "--------------"
  382.     'Debug.Print MYUSER.cchComment
  383.     'Debug.Print MYUSER.TotalSizeComp
  384.     'Debug.Print MYUSER.TotalSize
  385.     'Debug.Print MYUSER.CompFactor
  386.     'Debug.Print MYUSER.NumMembers
  387.     'Debug.Print "--------------"
  388.  
  389.    Exit Function
  390.    
  391. ErrorHandler:
  392. Dim lErr As Long, sErr As Long
  393.    
  394.    VBUnzip = -1
  395.    Set m_cUnzip = Nothing
  396.  
  397.    Exit Function
  398.  
  399. End Function
  400.  
  401.  
  402.