- Public Function CompressData(ByRef vxbInput() As Byte, ByRef vxbOutput() As Byte, Optional vnStart As Long = 0, Optional vnMaxSize As Long = 0, Optional veCompressionLevel As ZLIB_CompressionLevelConstants = Z_DEFAULT_COMPRESSION) As Boolean
- Dim tStream As zStream
- Dim rc As Long
- Dim xbCopy() As Byte
- With tStream
- '# On initialise les parametres de la structure stream
- If deflateInit(tStream, veCompressionLevel, ZLIB_Version, Len(tStream)) = 0 Then
- CompressData = True
- '# Les données sont a prendre dans le tableau en entrée
- CopyMemory rc, ByVal ArrPtr(vxbInput), 4
- If rc Then
- CopyMemory .avail_in, ByVal rc + 16, 4
- .avail_in = .avail_in - vnStart
- End If
- If .avail_in > 0 And vnStart < .avail_in Then
- '# Doit-on prendre tout le tableau ?
- If vnMaxSize <> 0 And vnMaxSize < .avail_in Then
- .avail_in = vnMaxSize
- End If
- .next_in = VarPtr(vxbInput(vnStart))
-
- '# On regarde ou sont les données du tableau de sortie.
- '# Pas de VarPtr ici car ce tableau peut etre vide, ca évite un On Error ^^
- CopyMemory rc, ByVal ArrPtr(vxbOutput), 4
- If rc Then
- CopyMemory rc, ByVal rc + 12, 4
-
- If rc + vnStart = .next_in Then
- '# Le tableau d'entrée et le tableau de sortie pointent au même endroit...
- '# ca ne va "pas le faire" ...
- xbCopy = vxbInput
- .next_in = VarPtr(xbCopy(vnStart))
- ElseIf vnStart Then
- '# On recopie le début du tableau
- ReDim vxbOutput(vnStart - 1)
- CopyMemory vxbOutput(0), vxbInput(0), vnStart - 1
- End If
- Else
- vxbOutput = vxbInput
- End If
-
- .avail_out = .avail_in + 12
- '# On agrandit le tableau de sortie
- ReDim Preserve vxbOutput(.total_out - 1 + .avail_out + vnStart)
- '# Les nouvelles données décompressées seront placées à la suite, dans le tableau...
- .next_out = VarPtr(vxbOutput(vnStart + .total_out))
-
- '# Lance la décompression a proprement parler
- CompressData = deflate(tStream, 4) = 1
-
- If .total_out Or vnStart Then
- ReDim Preserve vxbOutput(.total_out + vnStart - 1)
- Else
- Erase vxbOutput
- End If
- End If
-
- '# Fin de l'utilisation de ZLib
- deflateEnd tStream
- End If
- End With
- End Function
Public Function CompressData(ByRef vxbInput() As Byte, ByRef vxbOutput() As Byte, Optional vnStart As Long = 0, Optional vnMaxSize As Long = 0, Optional veCompressionLevel As ZLIB_CompressionLevelConstants = Z_DEFAULT_COMPRESSION) As Boolean
Dim tStream As zStream
Dim rc As Long
Dim xbCopy() As Byte
With tStream
'# On initialise les parametres de la structure stream
If deflateInit(tStream, veCompressionLevel, ZLIB_Version, Len(tStream)) = 0 Then
CompressData = True
'# Les données sont a prendre dans le tableau en entrée
CopyMemory rc, ByVal ArrPtr(vxbInput), 4
If rc Then
CopyMemory .avail_in, ByVal rc + 16, 4
.avail_in = .avail_in - vnStart
End If
If .avail_in > 0 And vnStart < .avail_in Then
'# Doit-on prendre tout le tableau ?
If vnMaxSize <> 0 And vnMaxSize < .avail_in Then
.avail_in = vnMaxSize
End If
.next_in = VarPtr(vxbInput(vnStart))
'# On regarde ou sont les données du tableau de sortie.
'# Pas de VarPtr ici car ce tableau peut etre vide, ca évite un On Error ^^
CopyMemory rc, ByVal ArrPtr(vxbOutput), 4
If rc Then
CopyMemory rc, ByVal rc + 12, 4
If rc + vnStart = .next_in Then
'# Le tableau d'entrée et le tableau de sortie pointent au même endroit...
'# ca ne va "pas le faire" ...
xbCopy = vxbInput
.next_in = VarPtr(xbCopy(vnStart))
ElseIf vnStart Then
'# On recopie le début du tableau
ReDim vxbOutput(vnStart - 1)
CopyMemory vxbOutput(0), vxbInput(0), vnStart - 1
End If
Else
vxbOutput = vxbInput
End If
.avail_out = .avail_in + 12
'# On agrandit le tableau de sortie
ReDim Preserve vxbOutput(.total_out - 1 + .avail_out + vnStart)
'# Les nouvelles données décompressées seront placées à la suite, dans le tableau...
.next_out = VarPtr(vxbOutput(vnStart + .total_out))
'# Lance la décompression a proprement parler
CompressData = deflate(tStream, 4) = 1
If .total_out Or vnStart Then
ReDim Preserve vxbOutput(.total_out + vnStart - 1)
Else
Erase vxbOutput
End If
End If
'# Fin de l'utilisation de ZLib
deflateEnd tStream
End If
End With
End Function