By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,066 Members | 1,625 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,066 IT Pros & Developers. It's quick & easy.

Compression namespace for Array

P: n/a
Fla
Hi!
I would like to use Compression namespace for Array, i.e. use .NET
native Compression for compress a String, or an Array of Integer whose
elements are returned values of AscW for each char of the String.
I tried with the code available @
http://msdn2.microsoft.com/en-us/lib...am(VS.80).aspx
but with no success 'cause I obtained ms.lenght buffer.length.

This is my source code for CompressData:

Private Sub CompressData()
Dim BUFFERSIZE As Integer = DataArray.Length *
Marshal.SizeOf(DataArray(0))
Dim buffer(BUFFERSIZE - 1) As Byte

'Dump integer Array DataArray in Byte Array buffer
Dim IndexValue As Integer = 0
For Index As Integer = 0 To BUFFERSIZE - 1
If ((Index << 31) = 0) Then ' if Even
buffer(Index) = (DataArray(IndexValue) >8)
Else
buffer(Index) = (DataArray(IndexValue) And &HFF)
IndexValue += 1
End If

Next

Dim ms As New MemoryStream()
' Use the newly created memory stream for the compressed data.
Dim compressedzipStream As New GZipStream(ms,
CompressionMode.Compress, True)
compressedzipStream.Write(buffer, 0, buffer.Length)
' Close the stream.
compressedzipStream.Close()
Dim BufferOut() As Byte = ms.ToArray()

End Sub

I've got the same problem also for DecompressData with the added
difficulty of using chunk of Bytes, as reported in the previous MDSN
article.

What I'm doing wrong? Is there any work-around, any trick to use
Compression not only for stream file but also for Array of Objects? Do
you have any suggestion?
Are there any commercial libraries, with no redistribuition license,
doing this?

Thanks

Nov 12 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Why not just encode the string so that you get an array of bytes?
Fla wrote:
Hi!
I would like to use Compression namespace for Array, i.e. use .NET
native Compression for compress a String, or an Array of Integer whose
elements are returned values of AscW for each char of the String.
I tried with the code available @
http://msdn2.microsoft.com/en-us/lib...am(VS.80).aspx
but with no success 'cause I obtained ms.lenght buffer.length.

This is my source code for CompressData:

Private Sub CompressData()
Dim BUFFERSIZE As Integer = DataArray.Length *
Marshal.SizeOf(DataArray(0))
Dim buffer(BUFFERSIZE - 1) As Byte

'Dump integer Array DataArray in Byte Array buffer
Dim IndexValue As Integer = 0
For Index As Integer = 0 To BUFFERSIZE - 1
If ((Index << 31) = 0) Then ' if Even
buffer(Index) = (DataArray(IndexValue) >8)
Else
buffer(Index) = (DataArray(IndexValue) And &HFF)
IndexValue += 1
End If

Next

Dim ms As New MemoryStream()
' Use the newly created memory stream for the compressed data.
Dim compressedzipStream As New GZipStream(ms,
CompressionMode.Compress, True)
compressedzipStream.Write(buffer, 0, buffer.Length)
' Close the stream.
compressedzipStream.Close()
Dim BufferOut() As Byte = ms.ToArray()

End Sub

I've got the same problem also for DecompressData with the added
difficulty of using chunk of Bytes, as reported in the previous MDSN
article.

What I'm doing wrong? Is there any work-around, any trick to use
Compression not only for stream file but also for Array of Objects? Do
you have any suggestion?
Are there any commercial libraries, with no redistribuition license,
doing this?

Thanks
Nov 12 '06 #2

P: n/a
Fla
Hi!
Did you mean System.Text.Encoding.ASCII.GetBytes as the following code
lines?
Dim Buffer() As Byte = System.Text.Encoding.ASCII.GetBytes(StringChar)

with

For Index As Integer = 0 To 299
StringChar &= ChrW(Index + 255)
Next

Thanks

Göran Andersson ha scritto:
Why not just encode the string so that you get an array of bytes?
Fla wrote:
Hi!
[...]
Nov 12 '06 #3

P: n/a
Yes. Except that you would need to use a dirrerent encoding to handle
characters that is not in the ASCII character set, like UTF8.

Fla wrote:
Hi!
Did you mean System.Text.Encoding.ASCII.GetBytes as the following code
lines?
Dim Buffer() As Byte = System.Text.Encoding.ASCII.GetBytes(StringChar)

with

For Index As Integer = 0 To 299
StringChar &= ChrW(Index + 255)
Next

Thanks

Göran Andersson ha scritto:
>Why not just encode the string so that you get an array of bytes?
Fla wrote:
>>Hi!
[...]
Nov 14 '06 #4

P: n/a
Fla
Göran Andersson ha scritto:
Yes. Except that you would need to use a dirrerent encoding to handle
characters that is not in the ASCII character set, like UTF8.
I can compress and decompress Array of bytes but I can't do this if I
dump the interrmediate compress byte() in a string. I'm using
encoding.unicode.getstring to dump the intermediate compress byte() on
a string and I'm using encoding.unicode.getbytes to get the array of
bytes to be uncompressed.
I can't get the result 'cause Deflate.Read(..) returns zero; I noticed
that when I try to dump the compressed byte array on a string, I get
less char as I forecast: CompressedBuffer.length = 119 while
CompressedString.Length = 57 using
Dim CompressedString As String =
Encoding.Unicode.GetString(CompressedBuffer).
I expected CompressedString.Length = CeilingUpper(119/2) = 60.
I'm using unicode compression in order to get a String with Char in
Unicode coding 'cause I need 2 bytes per char and storing a number 0 to
65535 in each char. Then I dump the this Unicode coded string on a
field of a db.

Any ideas? How could I dump the compressed array of bytes on an
intermediate string?
>
Fla wrote:
Hi!
Did you mean System.Text.Encoding.ASCII.GetBytes as the following code
lines?
Dim Buffer() As Byte = System.Text.Encoding.ASCII.GetBytes(StringChar)

with

For Index As Integer = 0 To 299
StringChar &= ChrW(Index + 255)
Next

Thanks

Göran Andersson ha scritto:
Why not just encode the string so that you get an array of bytes?
Fla wrote:
Hi!
[...]
Nov 15 '06 #5

P: n/a
Have you checked out System.String.Intern? I saved about 30% using this in
a
geocoder with millions of street names.

No compression required..
Nov 15 '06 #6

P: n/a
If you have some binary data in an array, you can't just treat it as if
it was an encoded string, because then you will lose some data when it's
decoded.

If you want a string representation of binary data, you can use the
Convert.ToBase64String and Convert.FromBase64String methods to convert
to and from a base 64 string.

Fla wrote:
Göran Andersson ha scritto:
>Yes. Except that you would need to use a dirrerent encoding to handle
characters that is not in the ASCII character set, like UTF8.

I can compress and decompress Array of bytes but I can't do this if I
dump the interrmediate compress byte() in a string. I'm using
encoding.unicode.getstring to dump the intermediate compress byte() on
a string and I'm using encoding.unicode.getbytes to get the array of
bytes to be uncompressed.
I can't get the result 'cause Deflate.Read(..) returns zero; I noticed
that when I try to dump the compressed byte array on a string, I get
less char as I forecast: CompressedBuffer.length = 119 while
CompressedString.Length = 57 using
Dim CompressedString As String =
Encoding.Unicode.GetString(CompressedBuffer).
I expected CompressedString.Length = CeilingUpper(119/2) = 60.
I'm using unicode compression in order to get a String with Char in
Unicode coding 'cause I need 2 bytes per char and storing a number 0 to
65535 in each char. Then I dump the this Unicode coded string on a
field of a db.

Any ideas? How could I dump the compressed array of bytes on an
intermediate string?
>Fla wrote:
>>Hi!
Did you mean System.Text.Encoding.ASCII.GetBytes as the following code
lines?
Dim Buffer() As Byte = System.Text.Encoding.ASCII.GetBytes(StringChar)

with

For Index As Integer = 0 To 299
StringChar &= ChrW(Index + 255)
Next

Thanks

Göran Andersson ha scritto:

Why not just encode the string so that you get an array of bytes?
Fla wrote:
Hi!
[...]
Nov 16 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.