Create CAB Cabinet File.;; CAB is the Microsoft Windows native compressed. UdfAskFileName ('Select files to include in Cabinet file', strFolder, 'All files|*. Nov 5, 2017 - In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
/ / Create CAB (MS Cabinet) File This dialog allows you to create CAB archive files. You can compress any files to CAB file and distribute it with your setup.exe. Use command in your installation script to unpack CAB files. Destination File Specifies a full path for the resulting CAB file or files. Type No limit - the installation will not be divided into volumes. Volume size (KB) - you have to specify volume size as kilobytes in the field below. Volume size (MB) - you have to specify volume size as megabytes in the field below.
Volume size Maximum volume size in Kilobytes or Megabytes according Type. Ignore wildcards You can specify the wildcards of excluded files (by semicolon).
These files will be ignored during the packing process. You can use '.' and '?'
.bak; temp.dat. Create CAB archive Source files (wildcards) or directories File, wildcard or folder Enter a filename or a folder on your computer (including path and drive designation), which should be included in the setup, or use the browsing buttons to select them. If you enter a directory, it will be packed together with all of its files and then re-created on the target computer. C: myapp. C: picture C: files myfiles.a??
Recursive Search Checked = enables recursive search in all subdirectories. Seek Files Only Checked = uses the specified wildcard only for seeking files.
Ease of use The goal is to read and create cabinet folders with no third-party tools or libraries that are not already present in the user's machine. As you will see, Windows Explorer on its own can help us read a cabinet file, but it cannot create such a file. However, Windows comes with a native command line utility, makecab.exe, which we will use to create a cabinet folder. There are many uses for cabinet folders. Previously, a saving in disk space was the primary purpose, indeed for creating installation disks and cd-roms, but today that often isn't very important; more the option for collecting a selection of files in one file may be more pertinent. In most cases it will be more convenient to use zip folders, as these can preserve subfolders and be modified after creation, should you later wish to – for example, one file inside a zip folder can be updated like a file in a normal folder. Once a cabinet file is created, that's it.
On the other hand, a cabinet folder can compress certain file types (like bitmap pictures and database files) much better than a zip folder can. And the fact, that the content of a cabinet folder is read-only, may in some cases be an advantage. However, for whatever reason or purpose you may have for using a cabinet folder (or a file as it physically is) that is controlled by VBA code in, say, Microsoft Excel or Microsoft Access, it should be easy to manage. Several external third-party tools and libraries are offered, and each may have certain advantages. An example is the utility from Microsoft, cabarc.exe, which can preserve subfolders in the cabinet folder, but for the general usage - reading cabinet folders (as Windows Explorer does) and creating them - you only need the two functions presented here: Cab and DeCab - nothing more, nothing less. Note: Zip files/folders can be managed in a similar way. If you are interested in this, please see: Cab and DeCab These two functions are included in the VBA API and does just as if you did it manually on screen using Windows Explorer.
They can do a little more, controlled by the parameters as explained below, and will also work with custom file extensions, should you have the need. Thus, a cabinet folder may be named, say, mycabfolder.bab.
This will prevent the file from being recognized by Windows Explorer as a cabinet folder. For large and/or many files, the well-known progress bar box will be displayed when copying from a cabinet folder. When creating a cabinet folder, in addition to the hourglass, a minimized command window runs, which can be restored if one wishes to follow the progress.
Cab Result = Cab(Path) This is the compress Path (a file or a folder's files). This would be similar to using Windows Explorer to right-clicked the file or folder and selected Send to cabinet folder (which you can't). Result holds 0 (zero) for success, or an error code.
For many practical purposes, you may wish to specify where the cabinet folder will be created and what name it should be given. Thus: Destination = 'D: Backups Data20171224.cab' Result = Cab(Path, Destination) Should you wish so, the extension can be different from cab, for example: Destination = 'D: Backups Data20171224.cak' Also you are probably aware, that if you in Windows Explorer tried to zip the same file or folder again, Windows Explorer will generate a 'versioned' zip file. Function Cab mimics this by extending filenames like: D: Backups Data20171224.cab D: Backups Data20171224 (2).cab D: Backups Data20171224 (3).cab and so on. If you don't want this, a third parameter is available, Overwrite. Thus, if the destination exists: Result = Cab(Path, True) ' Delete and recreate the zip folder. Result = Cab(Path, False) ' Create a versioned zip folder.
Further, a common style for a cabinet folder holding a single file is to replace the last character of the file extension with an underscore. To do so, set the parameter SingleFileExtension to True: Result = Cab(Path, True) ' Create cabinet file with single-file extension. If Path is not a single file, parameter SingleFileExtension will be ignored, and the standard cabinet extension will be used. Finally, compression can be set to standard or high (default). High compression can be very effective – often with a reduction of the compressed file size of about 30 - 50%, compared to standard compression.
A cabinet folder with standard compression will have a size similar to that obtained for a zip folder. A typical Microsoft Access database file could be compressed like this: Uncompressed 32.0 MB Compressed, low 5.8 MB Compressed, high 4.0 MB The penalty for using high compression is compression speed. It is about 50% slower. The decompression time it about the same, regardless of the compression used. DeCab Result = DeCab(Path) This decompress Path (a cabinet folder) is like if you, in Windows Explorer, double-click the cabinet folder and copied all the contained files to the parent folder.
Result holds 0 (zero) if success, or an error code. Often, you wish to specify where the cabinet folder will be decompressed and/or what name the folder should be given. If so, specify the Destination parameter: Destination = 'D: Restore Data' Result = DeCab(Path, Destination) By default, if decompressing to an existing folder, DeCab will overwrite the existing folder completely.
Setting parameter Overwrite to False will leave existing files, but will overwrite those with the same name as the files in the cabinet folder: Result = DeCab(Path, True) ' Delete and recreate the folder. Result = DeCab(Path, False) ' Leave existing files in thefolder. Warning Be careful and double-check before calling DeCab with Overwrite set to True (default). If Destination is specified wrongly, you may inadvertently erase most of a drive.
Code Both Cab and DeCab takes advantage of two standard Microsoft libraries found on any Windows machine: Microsoft Shell Controls And Automation Microsoft Scripting Runtime and both functions allow for early or late binding as you prefer, controlled by the constant EarlyBinding that you can adjust to your preference. There are many more details to take care of than one might expect, thus a lot of comments are outlined here to study for those that are interested. But don't feel obliged; the functions are ready to use as is.
Cab This is the full function: ' Compress a file or a folder to a cabinet file/folder. ' ' A single file will be compressed to a file with a '.' extension by default, ' optionally with a '.cab' extension. ' A folder will be compressed to a file with a '.cab' extension by default, ' optionally with a custom extension. ' ' Parameters: ' Path: ' Valid (UNC) path to the file or folder to compress. ' Destination: ' (Optional) Valid (UNC) path to a folder or to a file with a ' cabinet extension or other extension.
' Overwrite: ' (Optional) Overwrite (default) or leave an existing cabinet file. ' If False, the created cabinet file will be versioned: ' Example.cab, Example (2).cab, etc. ' If True, an existing cabinet file will first be deleted, then recreated. ' SingleFileExtension: ' (Optional) '.' style or (default) '.cab' file extension. ' If False, the created cabinet file extension will be 'cab'.
' If True and source file's extension's last character is not an underscore, ' the created cabinet file extension will be named as the source file, ' but with an underscore as the last character of the extension. ' In both cases, a specified Destination filename will override this setting.
' HighCompression: ' (Optional) Use standard compression or high compression. ' If False, use standard MSZIP compression. Faster, but larger file size. ' If True, use LZX compression. Slower, but smaller file size.
' ' Path and Destination can be relative paths. If so, the current path is used. ' ' If success, 0 is returned, and Destination holds the full path of the created cabinet file. ' If error, error code is returned, and Destination will be zero length string. ' ' Early binding requires references to: ' ' Shell: ' Microsoft Shell Controls And Automation ' ' Scripting.FileSystemObject: ' Microsoft Scripting Runtime ' ' 2017-10-31. Gustav Brock. Cactus Data ApS, CPH.
' Public Function Cab( ByVal Path As StringOptional ByRef Destination As StringOptional ByVal Overwrite As Boolean = TrueOptional ByVal SingleFileExtension As BooleanOptional ByVal HighCompression As Boolean = True) As Long #If EarlyBinding Then ' Microsoft Scripting Runtime. Dim FileSystemObject As Scripting.FileSystemObject ' Microsoft Shell Controls And Automation. Dim ShellApplication As Shell Set FileSystemObject = New Scripting.FileSystemObject Set ShellApplication = New Shell #Else Dim FileSystemObject As Object Dim ShellApplication As Object Set FileSystemObject = CreateObject('Scripting.FileSystemObject') Set ShellApplication = CreateObject('Shell.Application') #End If ' Extension of a cabinet file holding one or more files. Const CabExtensionName As String = 'cab' Const CabExtension As String = '.' & CabExtensionName ' Extension of a cabinet file holding one file only.
Const CabExtensionName1 As String = '.' ' Extension for a Disk Directive File for MakeCab.exe. Const DdfExtensionName As String = 'ddf' Const DdfExtension As String = '.'
& DdfExtensionName ' Custom error values. Const ErrorPathFile As Long = 75 Const ErrorOther As Long = -1 Const ErrorNone As Long = 0 ' Maximum (arbitrary) allowed count of created cabinet versions. Const MaxCabVersion As Integer = 1000 ' MakeCab directive constants. Const CompressionHigh As String = 'LZX' Const CompressionLow As String = 'MSZIP' Dim FileNames As Variant Dim CabPath As String Dim CabName As String Dim CabFile As String Dim CabBase As String Dim CabTemp As String Dim CabMono As Boolean Dim Extension As String Dim ExtensionName As String Dim Version As Integer Dim Item As Long Dim PathName As String Dim CurrentDirectory As String Dim TempDirectory As String Dim Result As Long If FileSystemObject.FileExists(Path) Then ' The source is an existing file. CabMono = True CabName = FileSystemObject.GetFileName(Path) If SingleFileExtension = True Then ExtensionName = FileSystemObject.GetExtensionName(Path) ' Check if the file already has a cabinet-style extension. If Right(ExtensionName, 1) = Right(CabExtensionName1, 1) Then ' Remove extension. ExtensionName = ' ' Add cabinet extension later.
Else ' Apply cabinet-style extension. Mid(CabName, Len(CabName)) = Right(CabExtensionName1, 1) ExtensionName = FileSystemObject.GetExtensionName(CabName) End If End If If ExtensionName = ' Then CabName = FileSystemObject.GetBaseName(Path) & CabExtension ExtensionName = FileSystemObject.GetExtensionName(CabName) End If CabPath = FileSystemObject.GetFile(Path).ParentFolder Extension = '.' & ExtensionName ElseIf FileSystemObject.FolderExists(Path) Then ' The source is an existing folder. CabName = FileSystemObject.GetBaseName(Path) & CabExtension CabPath = FileSystemObject.GetFolder(Path).ParentFolder Extension = CabExtension Else ' The source does not exist. End If If CabName = ' Then ' Nothing to compress. Destination = ' Else If Destination ' Then If FileSystemObject.GetExtensionName(Destination) = ' Then ' Destination is a folder.
If FileSystemObject.FolderExists(Destination) Then CabPath = Destination Else ' No folder for the cabinet file. Destination = ' End If Else ' Destination is a single compressed file.
![]()
CabName = FileSystemObject.GetFileName(Destination) If CabName = Destination Then ' No path given. Use CabPath as is. Else ' Use path of Destination. CabPath = FileSystemObject.GetParentFolderName(Destination) End If End If Else ' Use (and return) the already found folder of the source. Destination = CabPath End If End If If Destination ' Then CabFile = FileSystemObject.BuildPath(CabPath, CabName) If FileSystemObject.FileExists(CabFile) Then If Overwrite = True Then ' Delete an existing file. FileSystemObject.DeleteFile CabFile, True ' At this point either the file is deleted or an error is raised. Else CabBase = FileSystemObject.GetBaseName(CabFile) ' Modify name of the cabinet file to be created to preserve an existing file: ' 'Example.cab' - 'Example (2).cab', etc.
Version = Version + 1 Do Version = Version + 1 CabFile = FileSystemObject.BuildPath(CabPath, CabBase & Format(Version, ' (0 )') & Extension) Loop Until FileSystemObject.FileExists(CabFile) = False Or Version MaxCabVersion If Version MaxCabVersion Then ' Give up. Err.Raise ErrorPathFile, 'Cab Create', 'File could not be created.' End If CabName = FileSystemObject.GetFileName(CabFile) End If End If ' Get list of files to compress. FileNames = FolderFileNames(Path) ' Prepare a temporary ddf file to control makecab.exe. CabTemp = FileSystemObject.BuildPath(CabPath, FileSystemObject.GetBaseName(FileSystemObject.GetTempName) & DdfExtension) ' Resolve relative paths. CabTemp = FileSystemObject.GetAbsolutePathName(CabTemp) Path = FileSystemObject.GetAbsolutePathName(Path) ' Build the directive file.
Comments are closed.
|
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |