vba compression

Option Explicit

#If VBA7 Then
    Private Declare PtrSafe Function Compress& Lib "cabinet" (ByVal hCompressor&, ByVal pUncompressedData&, ByVal sizeUncompressedData&, ByVal pCompressedDataBuffer&, ByVal sizeCompressedBuffer&, bytesOut&)
    Private Declare PtrSafe Function Decompress& Lib "cabinet" (ByVal hCompressor&, ByVal pCompressedData&, ByVal sizeCompressedData&, ByVal pUncompressedDataBuffer&, ByVal sizeOfUncompressedBuffer&, bytesOut&)
    Private Declare PtrSafe Function CreateCompressor& Lib "cabinet" (ByVal CompressAlgorithm&, ByVal pAllocationRoutines&, hCompressor&)
    Private Declare PtrSafe Function CreateDecompressor& Lib "cabinet" (ByVal CompressAlgorithm&, ByVal pAllocationRoutines&, hDecompressor&)
    Private Declare PtrSafe Function CloseCompressor& Lib "cabinet" (ByVal hCompressor&)
    Private Declare PtrSafe Function CloseDecompressor& Lib "cabinet" (ByVal hDecompressor&)
#Else
    Private Declare Function Compress& Lib "cabinet" (ByVal hCompressor&, ByVal pUncompressedData&, ByVal sizeUncompressedData&, ByVal pCompressedDataBuffer&, ByVal sizeCompressedBuffer&, bytesOut&)
    Private Declare Function Decompress& Lib "cabinet" (ByVal hCompressor&, ByVal pCompressedData&, ByVal sizeCompressedData&, ByVal pUncompressedDataBuffer&, ByVal sizeOfUncompressedBuffer&, bytesOut&)
    Private Declare Function CreateCompressor& Lib "cabinet" (ByVal CompressAlgorithm&, ByVal pAllocationRoutines&, hCompressor&)
    Private Declare Function CreateDecompressor& Lib "cabinet" (ByVal CompressAlgorithm&, ByVal pAllocationRoutines&, hDecompressor&)
    Private Declare Function CloseCompressor& Lib "cabinet" (ByVal hCompressor&)
    Private Declare Function CloseDecompressor& Lib "cabinet" (ByVal hDecompressor&)
#End If


Function CompressString$(s$, Optional algorithm& = 5)
    Dim h&, max&, bytesOut&, b$
    If Len(s) Then
        If CreateCompressor(algorithm, 0&, h) Then
            max = LenB(s): b = Space$(max)
            If Compress(h, StrPtr(s), max, StrPtr(b), max, bytesOut) Then
                If bytesOut Then CompressString = Left$(b, bytesOut \ 2)
            End If
            CloseCompressor h
        End If
    End If
End Function

Function DecompressString$(s$, Optional algorithm& = 5)
    Dim h&, bytesOut&, b$
    If Len(s) Then
        If CreateDecompressor(algorithm, 0&, h) Then
            b = Space$(LenB(s) * 50)
            If Decompress(h, StrPtr(s), LenB(s), StrPtr(b), LenB(b), bytesOut) Then
                If bytesOut Then DecompressString = Left$(b, bytesOut \ 2)
            End If
            CloseDecompressor h
        End If
    End If
End Function
                              
'------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Dim GreenEggs$, Tiny$, Roundtrip$
                              
GreenEggs = "I do not like them in a box. I do not like them with a fox. I will not eat them in a house. I do not like them with a mouse. I do not like them here or there. I do not like them ANYWHERE!"
Tiny = CompressString(GreenEggs)
Roundtrip = DecompressString(Tiny)
                              
MsgBox Len(GreenEggs)    	'<--displays:  187
MsgBox Len(Tiny)    		'<--displays:  73
MsgBox Len(Roundtrip)    	'<--displays:  187

Debug.Print Roundtrip		'<--displays Dr. Seus's breakfast problem
                              
'Note: Tiny (the compressed string) can be written to disk and decompressed 
'      at a later date.  
                              
'Note: These functions use the Win32 Compression API, which is bundled with 
'Windows since Windows 8. These function will not work on Windows 7 and earlier.
                              
'Note: These functons default to using Algorithm #5: LZMS. The functions can 
'      optionally be directed to use the other supported MS API
'      compression algorithms:                
'                              MSZIP:       2
'                              XPRESS:      3
'                              XPRESS_HUFF: 4
'                              LZMS:        5

'Note: There is no Algorithm #1 included in the API.
                              
'Note: The default LZMS compression algorithm seems to compress the best.                              
                              
                              
'Reference:
'    https://docs.microsoft.com/en-us/windows/win32/api/_cmpapi/
                              

0
8

                                    Option Explicit

#If VBA7 Then
    Private Declare PtrSafe Function Compress&amp; Lib &quot;cabinet&quot; (ByVal hCompressor&amp;, ByVal pUncompressedData&amp;, ByVal sizeUncompressedData&amp;, ByVal pCompressedDataBuffer&amp;, ByVal sizeCompressedBuffer&amp;, bytesOut&amp;)
    Private Declare PtrSafe Function Decompress&amp; Lib &quot;cabinet&quot; (ByVal hCompressor&amp;, ByVal pCompressedData&amp;, ByVal sizeCompressedData&amp;, ByVal pUncompressedDataBuffer&amp;, ByVal sizeOfUncompressedBuffer&amp;, bytesOut&amp;)
    Private Declare PtrSafe Function CreateCompressor&amp; Lib &quot;cabinet&quot; (ByVal CompressAlgorithm&amp;, ByVal pAllocationRoutines&amp;, hCompressor&amp;)
    Private Declare PtrSafe Function CreateDecompressor&amp; Lib &quot;cabinet&quot; (ByVal CompressAlgorithm&amp;, ByVal pAllocationRoutines&amp;, hDecompressor&amp;)
    Private Declare PtrSafe Function CloseCompressor&amp; Lib &quot;cabinet&quot; (ByVal hCompressor&amp;)
    Private Declare PtrSafe Function CloseDecompressor&amp; Lib &quot;cabinet&quot; (ByVal hDecompressor&amp;)
#Else
    Private Declare Function Compress&amp; Lib &quot;cabinet&quot; (ByVal hCompressor&amp;, ByVal pUncompressedData&amp;, ByVal sizeUncompressedData&amp;, ByVal pCompressedDataBuffer&amp;, ByVal sizeCompressedBuffer&amp;, bytesOut&amp;)
    Private Declare Function Decompress&amp; Lib &quot;cabinet&quot; (ByVal hCompressor&amp;, ByVal pCompressedData&amp;, ByVal sizeCompressedData&amp;, ByVal pUncompressedDataBuffer&amp;, ByVal sizeOfUncompressedBuffer&amp;, bytesOut&amp;)
    Private Declare Function CreateCompressor&amp; Lib &quot;cabinet&quot; (ByVal CompressAlgorithm&amp;, ByVal pAllocationRoutines&amp;, hCompressor&amp;)
    Private Declare Function CreateDecompressor&amp; Lib &quot;cabinet&quot; (ByVal CompressAlgorithm&amp;, ByVal pAllocationRoutines&amp;, hDecompressor&amp;)
    Private Declare Function CloseCompressor&amp; Lib &quot;cabinet&quot; (ByVal hCompressor&amp;)
    Private Declare Function CloseDecompressor&amp; Lib &quot;cabinet&quot; (ByVal hDecompressor&amp;)
#End If


Function CompressString$(s$, Optional algorithm&amp; = 5)
    Dim h&amp;, max&amp;, bytesOut&amp;, b$
    If Len(s) Then
        If CreateCompressor(algorithm, 0&amp;, h) Then
            max = LenB(s): b = Space$(max)
            If Compress(h, StrPtr(s), max, StrPtr(b), max, bytesOut) Then
                If bytesOut Then CompressString = Left$(b, bytesOut \ 2)
            End If
            CloseCompressor h
        End If
    End If
End Function

Function DecompressString$(s$, Optional algorithm&amp; = 5)
    Dim h&amp;, bytesOut&amp;, b$
    If Len(s) Then
        If CreateDecompressor(algorithm, 0&amp;, h) Then
            b = Space$(LenB(s) * 50)
            If Decompress(h, StrPtr(s), LenB(s), StrPtr(b), LenB(b), bytesOut) Then
                If bytesOut Then DecompressString = Left$(b, bytesOut \ 2)
            End If
            CloseDecompressor h
        End If
    End If
End Function
                              
'------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Dim GreenEggs$, Tiny$, Roundtrip$
                              
GreenEggs = &quot;I do not like them in a box. I do not like them with a fox. I will not eat them in a house. I do not like them with a mouse. I do not like them here or there. I do not like them ANYWHERE!&quot;
Tiny = CompressString(GreenEggs)
Roundtrip = DecompressString(Tiny)
                              
MsgBox Len(GreenEggs)    	'&lt;--displays:  187
MsgBox Len(Tiny)    		'&lt;--displays:  73
MsgBox Len(Roundtrip)    	'&lt;--displays:  187

Debug.Print Roundtrip		'&lt;--displays Dr. Seus's breakfast problem
                              
'Note: Tiny (the compressed string) can be written to disk and decompressed 
'      at a later date.  
                              
'Note: These functions use the Win32 Compression API, which is bundled with 
'Windows since Windows 8. These function will not work on Windows 7 and earlier.
                              
'Note: These functons default to using Algorithm #5: LZMS. The functions can 
'      optionally be directed to use the other supported MS API
'      compression algorithms:                
'                              MSZIP:       2
'                              XPRESS:      3
'                              XPRESS_HUFF: 4
'                              LZMS:        5

'Note: There is no Algorithm #1 included in the API.
                              
'Note: The default LZMS compression algorithm seems to compress the best.                              
                              
                              
'Reference:
'    https://docs.microsoft.com/en-us/windows/win32/api/_cmpapi/
                              

0
0
3.78
9
B_archer 100 points

                                    Option Explicit

#If VBA7 Then
    Private Declare PtrSafe Function Compress&amp; Lib &quot;cabinet&quot; (ByVal hCompressor&amp;, ByVal pUncompressedData&amp;, ByVal sizeUncompressedData&amp;, ByVal pCompressedDataBuffer&amp;, ByVal sizeCompressedBuffer&amp;, bytesOut&amp;)
    Private Declare PtrSafe Function Decompress&amp; Lib &quot;cabinet&quot; (ByVal hCompressor&amp;, ByVal pCompressedData&amp;, ByVal sizeCompressedData&amp;, ByVal pUncompressedDataBuffer&amp;, ByVal sizeOfUncompressedBuffer&amp;, bytesOut&amp;)
    Private Declare PtrSafe Function CreateCompressor&amp; Lib &quot;cabinet&quot; (ByVal CompressAlgorithm&amp;, ByVal pAllocationRoutines&amp;, hCompressor&amp;)
    Private Declare PtrSafe Function CreateDecompressor&amp; Lib &quot;cabinet&quot; (ByVal CompressAlgorithm&amp;, ByVal pAllocationRoutines&amp;, hDecompressor&amp;)
    Private Declare PtrSafe Function CloseCompressor&amp; Lib &quot;cabinet&quot; (ByVal hCompressor&amp;)
    Private Declare PtrSafe Function CloseDecompressor&amp; Lib &quot;cabinet&quot; (ByVal hDecompressor&amp;)
#Else
    Private Declare Function Compress&amp; Lib &quot;cabinet&quot; (ByVal hCompressor&amp;, ByVal pUncompressedData&amp;, ByVal sizeUncompressedData&amp;, ByVal pCompressedDataBuffer&amp;, ByVal sizeCompressedBuffer&amp;, bytesOut&amp;)
    Private Declare Function Decompress&amp; Lib &quot;cabinet&quot; (ByVal hCompressor&amp;, ByVal pCompressedData&amp;, ByVal sizeCompressedData&amp;, ByVal pUncompressedDataBuffer&amp;, ByVal sizeOfUncompressedBuffer&amp;, bytesOut&amp;)
    Private Declare Function CreateCompressor&amp; Lib &quot;cabinet&quot; (ByVal CompressAlgorithm&amp;, ByVal pAllocationRoutines&amp;, hCompressor&amp;)
    Private Declare Function CreateDecompressor&amp; Lib &quot;cabinet&quot; (ByVal CompressAlgorithm&amp;, ByVal pAllocationRoutines&amp;, hDecompressor&amp;)
    Private Declare Function CloseCompressor&amp; Lib &quot;cabinet&quot; (ByVal hCompressor&amp;)
    Private Declare Function CloseDecompressor&amp; Lib &quot;cabinet&quot; (ByVal hDecompressor&amp;)
#End If


Function CompressString$(s$, Optional algorithm&amp; = 5)
    Dim h&amp;, max&amp;, bytesOut&amp;, b$
    If Len(s) Then
        If CreateCompressor(algorithm, 0&amp;, h) Then
            max = LenB(s): b = Space$(max)
            If Compress(h, StrPtr(s), max, StrPtr(b), max, bytesOut) Then
                If bytesOut Then CompressString = Left$(b, bytesOut \ 2)
            End If
            CloseCompressor h
        End If
    End If
End Function

Function DecompressString$(s$, Optional algorithm&amp; = 5)
    Dim h&amp;, bytesOut&amp;, b$
    If Len(s) Then
        If CreateDecompressor(algorithm, 0&amp;, h) Then
            b = Space$(LenB(s) * 50)
            If Decompress(h, StrPtr(s), LenB(s), StrPtr(b), LenB(b), bytesOut) Then
                If bytesOut Then DecompressString = Left$(b, bytesOut \ 2)
            End If
            CloseDecompressor h
        End If
    End If
End Function
                              
'------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Dim GreenEggs$, Tiny$, Roundtrip$
                              
GreenEggs = &quot;I do not like them in a box. I do not like them with a fox. I will not eat them in a house. I do not like them with a mouse. I do not like them here or there. I do not like them ANYWHERE!&quot;
Tiny = CompressString(GreenEggs)
Roundtrip = DecompressString(Tiny)
                              
MsgBox Len(GreenEggs)    	'&lt;--displays:  187
MsgBox Len(Tiny)    		'&lt;--displays:   73
MsgBox Len(Roundtrip)    	'&lt;--displays:  187

Debug.Print Roundtrip		'&lt;--displays Dr. Seus's breakfast problem
                              
'Note: Tiny (the compressed string) can be written to disk and decompressed 
'      at a later date.  
                              
'Note: These functions use the Win32 Compression API, which is bundled with 
'Windows since Windows 8. These function will not work on Windows 7 and earlier.
                              
'Note: These functons default to using Algorithm #5: LZMS. The functions can 
'      optionally be directed to use the other supported MS API
'      compression algorithms:                
'                              MSZIP:       2
'                              XPRESS:      3
'                              XPRESS_HUFF: 4
'                              LZMS:        5

'Note: There is no Algorithm #1 included in the API.
                              
'Note: The default LZMS compression algorithm seems to compress the best.                              
                              
                              
'Reference:
'    https://docs.microsoft.com/en-us/windows/win32/api/_cmpapi/
                              

3.78 (9 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source