Module: ZipFile in Category General VBA/VB6 : Encryption and Compression from Total Visual SourceBook

Create compressed zip files, list the files in a zip file, and extract/unzip all files or individual files from zip files with file overwrite options using VB6 and VBA in 32 and 64-bit.

Code references the Microsoft Shell Object without requiring it to be added as a library reference to your project.

Procedure Name Type Description
(Declarations) Declarations Declarations and private variables for the modZipFile module.
ZipFileCreate Procedure Create a zip file from a file; automatically overwrites the zip file if it exists.
ZipFileAdd Procedure Add a file or files to an existing zip file.
CreateZipFileHeader Procedure Create a new, empty zip file.
UnzipOneFile Procedure Extract (unzip) a single file from a zip file and place it into a folder. It does not create the subfolders specified in the zip file.
UnzipFileMask Procedure Extract (unzip) all the file names matching a mask from a zip file and place them into a folder. It does not create the subfolders specified in the zip file.
UnzipAllFilesSimple Procedure Extract (unzip) all the files from a zip file into its own folder. If the zip file includes subfolders, the subfolders are created. If the destination files exist, the user is prompted to overwrite them.
UnzipAllFiles Procedure Extract (unzip) all the files from a zip file into a folder. If the zip file includes subfolders, the subfolders are created.
UnzipAllFilesOverwrite Procedure Extract (unzip) all the files from a zip file into a folder and overwrite any existing file. If the zip file includes subfolders, the subfolders are created.
GetFileName Procedure Get the name and extension of a file name by removing the drive and folder path. This procedure is also in the modFileDisk module.
DeleteFile Procedure Delete the named file, handling errors if the file does not exist (same as KillFile but provided with a similar name). This procedure is also in the modFileDisk module.
GetFilePath Procedure Get the folder path part of a string. This procedure is also in the modFileDisk module.
IsFile Procedure Determine if a file name exists. This also returns TRUE if the file is a folder with trailing slash containing at least one file, but use IsFolder instead to check if a directory name exists. This procedure is also in the modFileDisk module.
ZippedFilesToArray Procedure Populate an array with a list of files in a zip file.
ZippedFilesToString Procedure Get a delimited string listing the files contained in a zip file.
ZipFolderRetrieve Procedure Part of a recursive routine to update an array of file/folder names listing the files contained in a folder of a zip file.
' Example of modZipFile
'
' To use this example, create a new module and paste this code into it.
' Then run the procedure by putting the cursor in the procedure and pressing:
'    F5 to run it, or
'    F8 to step through it line-by-line (see the Debug menu for more options)

Private Sub Example_modZipFile()
  ' Comments: Example of using the modZipFile module to create, open, and extract compressed zip files

  Const cstrSamplePath As String = "C:\Total Visual SourceBook 2013\Samples\"
  Const cstrUnzipFolder As String = cstrSamplePath & "unzip\"

  Const cstrZipFile As String = cstrSamplePath & "Test.Zip"
  Const cstrFilesToZip As String = cstrSamplePath & "sample.txt;" & cstrSamplePath & "Test.txt"
  Const cstrFilesToAdd As String = cstrSamplePath & "sample.mdb"
  Const cstrMask As String = "*.txt"

  Dim astrFiles() As String
  Dim intFile As Integer
  Dim intFiles As Integer
  Dim intAdd As Integer

  ' Create a zip file with the specified files
  Debug.Print "Creating zip file..."
  intFiles = ZipFileCreate(cstrZipFile, cstrFilesToZip, ";")
  If intFiles > 0 Then
    Debug.Print "Zip file created: " & cstrZipFile & " with " & intFiles & " files"
  Else
    Debug.Print "Zip file could not be created."
  End If

  If intFiles > 0 Then
    ' Add file to the zip file that was created
    intAdd = ZipFileAdd(cstrZipFile, cstrFilesToAdd)
    If intAdd > 0 Then
      Debug.Print "File added to zip file."

      ' Open the zip file with Windows Explorer
      Shell "Explorer.exe " & cstrZipFile
    Else
      Debug.Print "File could not be added to zip file."
    End If
  End If

  ' Get the list of files in the zip file
  intFiles = ZippedFilesToArray(cstrZipFile, False, astrFiles)
  Debug.Print intFiles & " files in the zip file " & cstrZipFile & ":"
  For intFile = 0 To UBound(astrFiles())
    Debug.Print intFile + 1, astrFiles(intFile)
  Next intFile

  ' Show the list of files in the zip file
  Debug.Print "Delimited list of zipped files: " & ZippedFilesToString(cstrZipFile, False, ",")

  ' Unzip (extract) one file from the compressed file
  Debug.Print "Unzipping a single file from the zip file and overwriting it if already exists..."
  If UnzipOneFile(cstrZipFile, "Test.txt", True, cstrUnzipFolder) Then
    Debug.Print "One file from zip file unzipped to: " & cstrUnzipFolder
  Else
    Debug.Print "File could not be extracted from the zip file."
  End If

  ' Unzip (extract) files matching a mask from a compressed file
  Debug.Print "Unzipping files that match a mask from the zip file and overwriting existing files..."
  intFiles = UnzipFileMask(cstrZipFile, cstrMask, True, cstrUnzipFolder)
  Debug.Print intFiles & " files matching mask were extracted from the zip file to: " & cstrUnzipFolder

  ' Unzip/extract all the files from a compress file
  Debug.Print "Unzipping all files from the zip file and overwrite existing files..."
  If UnzipAllFilesOverwrite(cstrZipFile, cstrUnzipFolder) Then
    Debug.Print "All files were extracted from the zip file to: " & cstrUnzipFolder
  Else
    Debug.Print "Files could not be extracted from the zip file."
  End If

  If MsgBox("Do you want to see the user prompts to overwrite existing files?", vbQuestion) = vbYes Then
    Call UnzipAllFiles(cstrZipFile, cstrUnzipFolder)
  End If

  ' Open Windows Explorer to the folder where the files are extracted
  Shell "Explorer.exe " & cstrUnzipFolder

End Sub

Total Visual SourceBook The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.

Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!

Additional Resources

Total Visual SourceBook CD and Printed Manual

Microsoft Access/ Office 2016, 2013, 2010, and 2007 Version
is Shipping!

New features in Total Visual SourceBook for Access, Office and VB6

Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!


View all FMS products for Microsoft Access All Our Microsoft Access Products

Reviews

Reader Choice Award for MS Access Source Code Library
Reader Choice

"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."

Van T. Dinh, Microsoft MVP

SourceBook Info

Additional Info

Question

 

 

Free Product Catalog from FMS