Cabinet.dll provides a collection of functions to compress/extract files and manage CAB files. I'd like to use it with VBA. There are few examples online using these features in VBA.
I want to add individual files to an existing CAB. That might be possible with the fx Compress or FCIAddFile. But I haven't found any VBA examples with FCIAddFile.
I attempted the following declarations. Inline comments describe the errors:
Enum CompressionType
MSZIP = 2
XPRESS = 3
XPRESS_HUFF = 4
LZMS = 5
End Enum
Private Declare PtrSafe Function CreateCompressor& Lib "cabinet" (ByVal CompressAlgorithm&, ByVal pAllocationRoutines&, hCompressor&)
Private Declare PtrSafe Function Compress& Lib "cabinet" (ByVal hCompressor&, ByVal pUncompressedData As String, ByVal sizeUncompressedData&, ByVal pCompressedDataBuffer As String, ByVal sizeCompressedBuffer&, bytesOut&)
Private Declare PtrSafe Function CreateDecompressor& Lib "cabinet" (ByVal CompressAlgorithm&, ByVal pAllocationRoutines&, hDecompressor&)
Private Declare PtrSafe Function Decompress& Lib "cabinet" (ByVal hCompressor&, ByVal pCompressedData&, ByVal sizeCompressedData&, ByVal pUncompressedDataBuffer&, ByVal sizeOfUncompressedBuffer&, bytesOut&)
Private Declare PtrSafe Function CloseCompressor& Lib "cabinet" (ByVal hCompressor&)
Private Declare PtrSafe Function CloseDecompressor& Lib "cabinet" (ByVal hDecompressor&)
Private Declare PtrSafe Function FCIAddFile& Lib "cabinet" (ByVal hfci&, ByVal pszSourceFile&, ByVal pszFileName&, ByVal fExecute&, ByVal pfnfcignc&, ByVal pfnfcis&, ByVal pfnfcigoi&, ByVal typeCompress&)
Function CompressString$(s$, Optional algorithm& = 5)
Dim h&, max&, bytesOut&, b$
CreateCompressor algorithm, 0&, h
max = LenB(s): b = Space$(max)
FCIAddFile h, "C:\Data\Network info.txt", "Network info.txt", False, 0&, 0&, 0&, XPRESS ' <-- TYPE MISMATCH ERROR
Compress h, StrPtr(s), max, StrPtr(b), max, bytesOut
If bytesOut Then CompressString = Left$(b, bytesOut \ 2)
CloseCompressor h ' <-- EXCEL CRASHES IF THIS LINE IS ALLOWED TO EXECUTE
End Function
Borrowed from https://newbedev.com/excel-vba-compression-code-example
I don't understand why the constants appear to be different than the CreateCompressor function.
CreateCompressor:
MSZIP = 2
XPRESS = 3
XPRESS_HUFF = 4
LZMS = 5
tcompTYPE_NONE: 0x0000
tcompTYPE_MSZIP: 0x0001