home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / CODE_UPLOAD155232262001.psc / RichsoftVBZip.ctl < prev    next >
Encoding:
Text File  |  2001-02-27  |  15.4 KB  |  500 lines

  1. VERSION 5.00
  2. Begin VB.UserControl RichsoftVBZip 
  3.    BackStyle       =   0  'Transparent
  4.    CanGetFocus     =   0   'False
  5.    ClientHeight    =   975
  6.    ClientLeft      =   0
  7.    ClientTop       =   0
  8.    ClientWidth     =   975
  9.    HasDC           =   0   'False
  10.    InvisibleAtRuntime=   -1  'True
  11.    ScaleHeight     =   975
  12.    ScaleWidth      =   975
  13.    ToolboxBitmap   =   "RichsoftVBZip.ctx":0000
  14.    Begin VB.Frame fraBorder 
  15.       Height          =   855
  16.       Left            =   120
  17.       TabIndex        =   0
  18.       Top             =   0
  19.       Width           =   735
  20.       Begin VB.Image imgZip 
  21.          Height          =   480
  22.          Left            =   120
  23.          Picture         =   "RichsoftVBZip.ctx":0312
  24.          Top             =   240
  25.          Width           =   480
  26.       End
  27.    End
  28. End
  29. Attribute VB_Name = "RichsoftVBZip"
  30. Attribute VB_GlobalNameSpace = False
  31. Attribute VB_Creatable = True
  32. Attribute VB_PredeclaredId = False
  33. Attribute VB_Exposed = True
  34. '==============================================================================
  35. 'Richsoft Computing 2001
  36. 'Richard Southey
  37. 'This code is e-mailware, if you use it please e-mail me and tell me about
  38. 'your program.
  39. '
  40. 'For latest information about this and other projects please visit my website:
  41. 'www.richsoftcomputing.btinternet.co.uk
  42. '
  43. 'If you would like to make any comments/suggestions then please e-mail them to
  44. 'richsoftcomputing@btinternet.co.uk
  45. '==============================================================================
  46.  
  47. 'Set up the private atrributes
  48. Private ZipFilename As String
  49. Private CompLevel As ZipLevel
  50. Private DOS83Format As Boolean
  51. Private Recurse As Boolean
  52.  
  53. 'Set up the file collection
  54. Private Archive As Collection
  55.  
  56. 'Events
  57. Event OnArchiveUpdate()
  58. Event OnZipProgress(ByVal Percentage As Integer, ByVal Filename As String)
  59. Event OnZipComplete(ByVal Successful As Long)
  60. Event OnUnzipProgress(ByVal Percentage As Integer, ByVal Filename As String)
  61. Event OnUnzipComplete(ByVal Successful As Long)
  62. Event OnDeleteProgress(ByVal Percentage As Integer, ByVal Filename As String)
  63. Event OnDeleteComplete(ByVal Successful As Long)
  64.  
  65. 'Actions
  66. Public Enum ZipAction
  67.     zipDefault = 1
  68.     zipFreshen = 2
  69.     zipUpdate = 3
  70. End Enum
  71.  
  72. 'Compression Level
  73. Public Enum ZipLevel
  74.     zipStore = 0
  75.     zipLevel1 = 1
  76.     zipSuperFast = 2
  77.     zipFast = 3
  78.     zipLevel4 = 4
  79.     zipNormal = 5
  80.     zipLevel6 = 6
  81.     zipLevel7 = 7
  82.     zipLevel8 = 8
  83.     zipMax = 9
  84. End Enum
  85.  
  86. Public Function ConvertBytesToString(Bytes As Long) As String
  87.     'Turns a number representing the number of bytes
  88.     'into a string, bytes, KB, MB
  89.     
  90.     Select Case (Bytes / 1024)
  91.         Case Is < 0.2
  92.             ConvertBytesToString = Format(Bytes, "###,##0") & " bytes"
  93.         
  94.         Case Is < 512
  95.             ConvertBytesToString = Format(Bytes / 1024, "###,##0.0") + "KB"
  96.             
  97.         Case Else
  98.             ConvertBytesToString = Format(Bytes / (1024 ^ 2), "###,##0.0") + "MB"
  99.             
  100.     End Select
  101.         
  102. End Function
  103. Private Function FindFiles(Files As Collection, ByVal Recurse As Boolean)
  104.     'Finds all the files matching the specification
  105.     
  106.     '*******************************************
  107.     'RECURSIVE FOLDER SEARCH NOT YET IMPLEMENTED
  108.     '*******************************************
  109.     Dim Result As New Collection
  110.     Dim i As Long
  111.     For i = 1 To Files.Count
  112.         Debug.Print Files(i)
  113.         'Parse the file specification to find the path
  114.         Path = ParsePath(Files(i))
  115.         'Find the files matching the pattern
  116.         r = Dir$(Files(i), Attributes)
  117.         Do Until r = ""
  118.             'Add the file to the new file list collection
  119.             Result.Add Path & r
  120.             'Move on to next file, if one exists
  121.             r = Dir$()
  122.         Loop
  123.     Next i
  124.     
  125.     Set FindFiles = Result
  126.     
  127. End Function
  128.  
  129. Public Function Add(Files As Collection, ByVal Action As ZipAction, ByVal StorePathInfo As Boolean, ByVal RecurseSubFolders As Boolean, ByVal UseDOS83 As Boolean, ByVal CompressionLevel As ZipLevel) As Long
  130.     'Adds the specified files to the archive
  131.     Dim ArchiveFilename As String
  132.     ArchiveFilename = ZipFilename
  133.     Dim i As Long
  134.     Dim Result As Long
  135.     Dim FilesToAdd As Collection
  136.     
  137.     'Check to see if there are any files in the archive
  138.     'if not delete the file so there are not error messages
  139.     
  140.     If GetEntryNum = 0 Then
  141.         If Dir$(ArchiveFilename) <> "" Then
  142.             Kill ArchiveFilename
  143.         End If
  144.     End If
  145.     
  146.     'Find all the files to add, recursing folders if selected
  147.     Set FilesToAdd = FindFiles(Files, RecurseSubFolders)
  148.     
  149.     'Loop through the files adding them to the archive
  150.     For i = 1 To FilesToAdd.Count
  151.         Debug.Print "Trying to Add " & FilesToAdd(i)
  152.         RaiseEvent OnZipProgress((100 * (i / (FilesToAdd.Count))), ParseFilename(FilesToAdd(i)))
  153.         DoEvents
  154.         If AddFile(ArchiveFilename, FilesToAdd(i), StorePathInfo, UseDOS83, Action, CompressionLevel) Then
  155.             'File successfully extracted
  156.             Result = Result + 1
  157.         Else
  158.             'File did not extract for some reason
  159.         End If
  160.     Next i
  161.     
  162.     RaiseEvent OnZipComplete(Result)
  163.     'If any file was added update the archive
  164.     If Result > 0 Then
  165.         Read
  166.         RaiseEvent OnArchiveUpdate
  167.     End If
  168. End Function
  169.  
  170.  
  171. Public Function Delete(Files As Collection) As Long
  172.     'Deletes the files specified in the collection
  173.     'Returns the number of files deleted
  174.     Dim FilesToDelete As Collection
  175.     Dim ArchiveFilename As String
  176.     ArchiveFilename = ZipFilename
  177.     Dim i As Long
  178.     Dim Result As Long
  179.     
  180.     'First find the files which match the patterns
  181.     'specified in the collection
  182.     Set FilesToDelete = SelectFiles(Files)
  183.     
  184.     'Extract each file in turn
  185.     For i = 1 To FilesToDelete.Count
  186.         Debug.Print "Trying to Delete " & FilesToDelete(i)
  187.         RaiseEvent OnDeleteProgress((100 * (i / (FilesToDelete.Count))), ParseFilename(FilesToDelete(i)))
  188.         DoEvents
  189.         'Check to see if we are deleting the last entry
  190.         'if so just delete the archive
  191.         If (GetEntryNum - Result) > 1 Then
  192.             If DeleteFile(ArchiveFilename, FilesToDelete(i)) Then
  193.                 'File successfully extracted
  194.                 Result = Result + 1
  195.             Else
  196.                 'File did not extract for some reason
  197.             End If
  198.         Else
  199.             Kill ArchiveFilename
  200.             Result = Result + 1
  201.         End If
  202.     Next i
  203.     
  204.     RaiseEvent OnDeleteComplete(Result)
  205.     'If any file was deleted update the archive
  206.     If Result > 0 Then
  207.         Read
  208.         RaiseEvent OnArchiveUpdate
  209.     End If
  210.     
  211.     Delete = Result
  212.     
  213. End Function
  214.  
  215. Public Function Extract(Files As Collection, ByVal Action As ZipAction, ByVal UsePathInfo As Boolean, ByVal Overwrite As Boolean, ByVal Path As String) As Long
  216.     'Extracts the files specified in the collection
  217.     'Returns the number of files extracted
  218.     Dim FilesToExtract As Collection
  219.     Dim ArchiveFilename As String
  220.     ArchiveFilename = ZipFilename
  221.     Dim i As Long
  222.     Dim Result As Long
  223.     
  224.     'First find the files which match the patterns
  225.     'specified in the collection
  226.     Set FilesToExtract = SelectFiles(Files)
  227.     
  228.     'Check to if there is anything to do
  229.     'if there is create the output path if it does not exist
  230.     
  231.     '************
  232.     'TO IMPLEMENT
  233.     '************
  234.     
  235.     'Extract each file in turn
  236.     For i = 1 To FilesToExtract.Count
  237.         Debug.Print "Trying to Extract " & FilesToExtract(i) & " to " & Path
  238.         RaiseEvent OnUnzipProgress((100 * (i / (FilesToExtract.Count))), ParseFilename(FilesToExtract(i)))
  239.         DoEvents
  240.         If ExtractFile(ArchiveFilename, CStr(FilesToExtract(i)), Path, UsePathInfo, Overwrite, Action) Then
  241.             'File successfully extracted
  242.             Result = Result + 1
  243.         Else
  244.             'File did not extract for some reason
  245.         End If
  246.     Next i
  247.     
  248.     RaiseEvent OnUnzipComplete(Result)
  249.     Extract = Result
  250.     
  251. End Function
  252.  
  253. Public Property Get Filename() As String
  254.     Filename = ZipFilename
  255. End Property
  256.  
  257. Public Property Let Filename(ByVal New_Filename As String)
  258.     Dim r As Long
  259.     Dim i As Long
  260.     'Called when the filename is updated
  261.     ZipFilename = New_Filename
  262.     PropertyChanged "Filename"
  263.     'Read in the contents of the file
  264.     r = Read
  265.     'Raise the update event
  266.     RaiseEvent OnArchiveUpdate
  267. End Property
  268.  
  269. Public Function GetEntry(ByVal Index As Long) As ZipFileEntry
  270.     Set GetEntry = Archive(Index)
  271. End Function
  272. Public Function GetEntryNum() As Long
  273.     GetEntryNum = Archive.Count
  274. End Function
  275.  
  276. Private Function SelectFiles(Files As Collection) As Collection
  277.     'Selects files from a wildcard specification
  278.     'Wildcards only corrispond to the filename and not the path
  279.     Dim i As Long
  280.     Dim j As Long
  281.     Dim Result As New Collection
  282.     'Loop through the collection looking at each entry
  283.     For i = 1 To Files.Count
  284.         'Loop through the files in the archive checking the pattern
  285.         For j = 1 To GetEntryNum()
  286.             'Check the pattern, ignoring case
  287.             If LCase$(ParseFilename(GetEntry(j).Filename)) Like LCase$(Files(i)) Then
  288.                 'Its a match so add it to the new collection
  289.                 Result.Add GetEntry(j).Filename
  290.             End If
  291.         Next j
  292.     Next i
  293.     Set SelectFiles = Result
  294. End Function
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323. Public Function ParsePath(Path As String) As String
  324.     'Takes a full file specification and returns the path
  325.     For A = Len(Path) To 1 Step -1
  326.         If Mid$(Path, A, 1) = "\" Or Mid$(Path, A, 1) = "/" Then
  327.             'Add the correct path separator for the input
  328.             If Mid$(Path, A, 1) = "\" Then
  329.                 ParsePath = LCase$(Left$(Path, A - 1) & "\")
  330.             Else
  331.                 ParsePath = LCase$(Left$(Path, A - 1) & "/")
  332.             End If
  333.             Exit Function
  334.         End If
  335.     Next A
  336. End Function
  337.  
  338. Public Function ParseFilename(ByVal Path As String) As String
  339.     'Takes a full file specification and returns the path
  340.     For A = Len(Path) To 1 Step -1
  341.         If Mid$(Path, A, 1) = "\" Or Mid$(Path, A, 1) = "/" Then
  342.             ParseFilename = Mid$(Path, A + 1)
  343.             Exit Function
  344.         End If
  345.     Next A
  346.     ParseFilename = Path
  347. End Function
  348.  
  349. Private Sub UserControl_Initialize()
  350.     'Create a new Archive Collection
  351.     Set Archive = New Collection
  352. End Sub
  353.  
  354. Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
  355.     'Get properties out of storage
  356.     ZipFilename = PropBag.ReadProperty("Filename", "")
  357. End Sub
  358.  
  359.  
  360. Private Sub UserControl_Resize()
  361.     UserControl.Size 975, 975
  362. End Sub
  363.  
  364. Public Sub About()
  365.     'Show the about box
  366.     frmAbout.Show 1
  367. End Sub
  368.  
  369. Public Function Read() As Long
  370.     'Reads the archive and places each file into a collection
  371.     Dim Sig As Long
  372.     Dim ZipStream As Integer
  373.     Dim Res As Long
  374.     Dim zFile As ZipFile
  375.     Dim Name As String
  376.     Dim i As Integer
  377.     
  378.     'If the filename is empty return a empty file list
  379.     If ZipFilename = "" Then
  380.         Read = 0
  381.         'Remove any files still in the list
  382.         For i = Archive.Count To 1 Step -1
  383.             Archive.Remove i
  384.         Next i
  385.         Exit Function
  386.     End If
  387.     
  388.     'Clears the collection
  389.     'begin
  390.     'Archive.Clear;
  391.     For i = Archive.Count To 1 Step -1
  392.         Archive.Remove i
  393.     Next i
  394.     
  395.     'Opens the archive for binary access
  396.     ZipStream = FreeFile
  397.     Open ZipFilename For Binary As ZipStream
  398.     'Loop through archive
  399.     Do While True
  400.         Get ZipStream, , Sig
  401.         'See if the file header has been found
  402.               If Sig = LocalFileHeaderSig Then
  403.                     'Read each part of the file header
  404.                     Get ZipStream, , zFile.Version
  405.                     Get ZipStream, , zFile.Flag
  406.                     Get ZipStream, , zFile.CompressionMethod
  407.                     Get ZipStream, , zFile.Time
  408.                     Get ZipStream, , zFile.Date
  409.                     Get ZipStream, , zFile.CRC32
  410.                     Get ZipStream, , zFile.CompressedSize
  411.                     Get ZipStream, , zFile.UncompressedSize
  412.                     Get ZipStream, , zFile.FileNameLength
  413.                     Get ZipStream, , zFile.ExtraFieldLength
  414.                     'Get the filename
  415.                     'Set up a empty string so the right number of
  416.                     'bytes is read
  417.                     Name = String$(zFile.FileNameLength, " ")
  418.                     Get ZipStream, , Name
  419.                     zFile.Filename = Mid$(Name, 1, zFile.FileNameLength)
  420.                     'Move on through the archive
  421.                     'Skipping extra space, and compressed data
  422.                     Seek ZipStream, (Seek(ZipStream) + zFile.ExtraFieldLength)
  423.                     Seek ZipStream, (Seek(ZipStream) + zFile.CompressedSize)
  424.                     'Add the fileinfo to the collection
  425.                     AddEntry zFile
  426.               Else
  427.               Debug.Print Sig
  428.                 If Sig = CentralFileHeaderSig Or Sig = 0 Then
  429.                     'All the filenames have been found so
  430.                     'exit the loop
  431.                     Exit Do
  432.                 'End
  433.                 Else
  434.                 If Sig = EndCentralDirSig Then
  435.                     'Exit the loop
  436.                     Exit Do
  437.                 End If
  438.                 End If
  439.             End If
  440.         Loop
  441.         'Close the archive
  442.         Close ZipStream
  443.         'Return the number of files in the archive
  444.         Read = Archive.Count
  445.  
  446.     'Fire the update event
  447.     RaiseEvent OnArchiveUpdate
  448. End Function
  449.  
  450. Private Sub AddEntry(zFile As ZipFile)
  451.     Dim xFile As New ZipFileEntry
  452.     'Adds a file from the archive into the collection
  453.     '**It does not add entry that are just folders**
  454.     If ParseFilename(zFile.Filename) <> "" Then
  455.         xFile.Version = zFile.Version
  456.         xFile.Flag = zFile.Flag
  457.         xFile.CompressionMethod = zFile.CompressionMethod
  458.         xFile.CRC32 = zFile.CRC32
  459.         xFile.FileDateTime = GetDateTime(zFile.Date, zFile.Time)
  460.         xFile.CompressedSize = zFile.CompressedSize
  461.         xFile.UncompressedSize = zFile.UncompressedSize
  462.         xFile.FileNameLength = zFile.FileNameLength
  463.         xFile.Filename = zFile.Filename
  464.         xFile.ExtraFieldLength = zFile.ExtraFieldLength
  465.     End If
  466.     Archive.Add xFile
  467. End Sub
  468.  
  469. Private Function GetDateTime(ZipDate As Integer, ZipTime As Integer) As Date
  470.     'Converts the file date/time dos stamp from the archive
  471.     'in to a normal date/time string
  472.     
  473.     Dim r As Long
  474.     Dim FTime As FileTime
  475.     Dim Sys As SYSTEMTIME
  476.     Dim ZipDateStr As String
  477.     Dim ZipTimeStr As String
  478.     
  479.     'Convert the dos stamp into a file time
  480.     r = DosDateTimeToFileTime(CLng(ZipDate), CLng(ZipTime), FTime)
  481.     'Convert the file time into a standard time
  482.     r = FileTimeToSystemTime(FTime, Sys)
  483.  
  484.     ZipDateStr = Sys.wDay & "/" & Sys.wMonth & "/" & Sys.wYear
  485.     ZipTimeStr = Sys.wHour & ":" & Sys.wMinute & ":" & Sys.wSecond
  486.  
  487.     GetDateTime = ZipDateStr & " " & ZipTimeStr
  488. End Function
  489. Private Sub UserControl_Terminate()
  490.     'Clean up the Archive
  491.     Set Archive = Nothing
  492. End Sub
  493.  
  494.  
  495. Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
  496.     'Put properties into storage
  497.     PropBag.WriteProperty "Filename", ZipFilename, ""
  498. End Sub
  499.  
  500.