CryptoSys API Library Manual

COMPR_Compress

Compress data using compression algorithm.

VBA/VB6 Syntax

Public Declare Function COMPR_Compress Lib "diCryptoSys.dll" (ByRef lpOutput As Byte, ByVal nOutBytes As Long, ByRef lpInput As Byte, ByVal nInputLen As Long, ByVal nOptions As Long) As Long

nRet = COMPR_Compress(lpOutput(0), nOutputLen, lpInput(0), nInputLen, nOptions)

C/C++ Syntax

long __stdcall COMPR_Compress(unsigned char *lpOutput, long nOutBytes, const unsigned char *lpInput, long nInputLen, long nOptions);

Parameters

lpOutput
[out] byte buffer to receive output data.
nOutBytes
[in] size of the output buffer in bytes.
lpInput
[in] input data to be compressed.
nInputLen
[in] length of input array in bytes.
nOptions
[in] option flags. Select one of:
API_COMPR_ZLIB (0) to use the zlib algorithm (default)
API_COMPR_ZSTD to use the Zstandard algorithm

Returns (VBA/C)

The number of bytes successfully copied into the output buffer or the required size in bytes. If an error occurs, it returns a negative error code.

VBA Wrapper Syntax

Public Function comprCompress(lpInput() As Byte, Optional nOptions As Long = 0) As Byte()

.NET Equivalent

Compr.Compress Method

Python Equivalent

static Compr.compress(data, alg=Alg.ZLIB)

Remarks

To determine the required size of the output buffer, call the function with nOutBytes set to zero (or lpOutput set to NULL).

VB6/VBA users: Note the '(0)' in lpOutput(0) and abInput(0).

Example (VBA core function)

Dim strPlain As String
Dim strBack As String
Dim abPlain() As Byte
Dim abCompressed() As Byte
Dim nCompLen As Long
Dim nUncompLen As Long
Dim nRet As Long

' COMPRESSSION (deflation)

' Set the plaintext message
strPlain = "hello, hello, hello. This is a 'hello world' message " & _
    "for the world, repeat, for the world."
' Convert to an array of bytes
abPlain = StrConv(strPlain, vbFromUnicode)
nUncompLen = UBound(abPlain) + 1
' Find required compressed length by calling with zero length value
nCompLen = COMPR_Compress(0, 0, abPlain(0), nUncompLen, 0)
ReDim abCompressed(nCompLen - 1)
' Now compress plaintext
Call COMPR_Compress(abCompressed(0), nCompLen, abPlain(0), nUncompLen, 0)
Debug.Print "Compressed " & nUncompLen & " bytes to " & nCompLen

' DECOMPRESSSION (inflation)

' Uncompress the compressed data
' Find the required length of uncompressed data
nUncompLen = COMPR_Uncompress(0, 0, abCompressed(0), nCompLen, 0)
Debug.Print "Required uncompressed length is " & nUncompLen & " bytes"
ReDim abPlain(nUncompLen - 1)
nRet = COMPR_Uncompress(abPlain(0), nUncompLen, abCompressed(0), nCompLen, 0)

' Convert back to a string
strBack = StrConv(abPlain, vbUnicode)
Debug.Print strBack
Compressed 90 bytes to 68
Required uncompressed length is 90 bytes
hello, hello, hello. This is a 'hello world' message for the world, repeat, for the world.

Example (VBA wrapper function)

Dim strPlain As String
Dim lpToCompress() As Byte
Dim lpCompressed() As Byte
Dim lpUncompressed() As Byte
strPlain = "hello, hello, hello. This is a 'hello world' message " & _
	"for the world, repeat, for the world."
lpToCompress = StrConv(strPlain, vbFromUnicode)

' Using default zlib algorithm
lpCompressed = comprCompress(lpToCompress)
Debug.Print "COMPRESSED(ZLIB)=" & cnvHexStrFromBytes(lpCompressed)
' 789CCB48CDC9C9D751C840A2F4144232328B15802851411D2CA2509E5F9493A2AE909B5A5C9C989EAA90965FA45092910A11D651284A2D484D2CD14115D6030086D11F4E
lpUncompressed = comprUncompress(lpCompressed)
Debug.Print "'" & StrConv(lpUncompressed, vbUnicode); "'"

' Using Zstandard algorithm
lpCompressed = comprCompress(lpToCompress, API_COMPR_ZSTD)
Debug.Print "COMPRESSED(ZSTD)=" & cnvHexStrFromBytes(lpCompressed)
' 28B52FFD205A1D0200540368656C6C6F2C202E20546869732069732061202720776F726C6427206D65737361676520666F72207468652C207265706561742C2E0400409E3AB1E1D97E71C508
lpUncompressed = comprUncompress(lpCompressed, API_COMPR_ZSTD)
Debug.Print "'" & StrConv(lpUncompressed, vbUnicode); "'"

Note that for this example with a short string, the zstd algorithm produces a longer result than zlib. However, in most cases, zstd produces shorter results.

See Also

COMPR_Uncompress

[Contents] [Index]

[PREV: CNV_ShortPathName...]   [Contents]   [Index]   
   [NEXT: COMPR_Uncompress...]

Copyright © 2001-24 D.I. Management Services Pty Ltd. All rights reserved. Generated 2024-01-07T07:42:00Z.