Zip and Unzip MDB files using VB6

Posted on 2006-04-26
Last Modified: 2012-06-21
Like it says on the tin!

I have a vb6 app which creates an Access MDB file of about 250kb. This file will unltimately be emailed using a slow connection. When zipped, this file is only around 15kb in size. Is there a way I can use Windows compressed folders to zip the file programmaticaly.

I need to use a standard windows API and not a third party dll or control.

Later I have another application that will process this mdb file. It would be good if this app could unzip the file and process the mdb automatically with out user interaction.
Question by:RussellMartin
    LVL 15

    Expert Comment

    LVL 2

    Author Comment

    CQUINN, this link requires WinZIP.

    I want to tap into Windows Compressed Zip Folders if possible. This will allow my application to not rely on third party software.

    Is there an API call or something available for Xp or Windows 2k?
    LVL 27

    Accepted Solution


    Public Function vbZIP(ByVal sSource As String, ByVal sDest As String) As Boolean
       Dim lAttr As Long, lInitialLength As Long
       lAttr = GetFileAttributes(sSource)
       If lAttr = -1 Then Exit Function
       Dim sZipHeader As String
    '   sZipHeader = Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, Chr(0))
       sZipHeader = "PK" & Chr(5) & Chr(6) & String(18, Chr(0))
       If Dir(sDest) <> "" Then Kill sDest
       Dim nFile As Integer
       nFile = FreeFile
       Open sDest For Output As #nFile
          Print #nFile, sZipHeader
       Close #nFile
       lInitialLength = FileLen(sDest)
       Dim oShell, oSource
       Set oShell = CreateObject("Shell.Application")
      'Copy the file(s) to the compressed folder
       If (lAttr And vbDirectory) = vbDirectory Then
          Set oSource = oShell.NameSpace(CVar(sSource)).Items
          Set oSource = oShell.NameSpace(CVar(GetParentFolder(sSource))).ParseName(GetFileTitle(sSource))
       End If
       If Not oSource Is Nothing Then
          oShell.NameSpace(CVar(sDest)).CopyHere oSource, &H615
          GoTo ErrZip
       End If
       Do While Dir(sDest) = ""
          Sleep 100
       On Error Resume Next
       Do While FileLen(sDest) = lInitialLength
          Sleep 100
      'wait for lock to release
       On Error Resume Next
    'Attempt to open the file, this causes an Err 70,
    'Permission Denied when the file is already open
          nFile = FreeFile
          Open sDest For Append As #nFile
          Close #nFile
          If Err = 0 Then Exit Do
          Sleep 100
       vbZIP = True
       Set oSource = Nothing
       Set oShell = Nothing
    End Function

    Call vbZIP("c:\data.mdb", "c:\")
    LVL 27

    Expert Comment

    Oops, forgot API:
    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    Public Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now