Android Question Compress Bytes or String

Serge Nova

Member
Hello everyone, I need to compress the bytes either
1. Compress the bytes directly, either
2. By converting the bytes into text and then compressing this text.
I tried this code but the text obtained by encoding64 is not compressing.

Example:
Dim org As String
    org =  su.EncodeBase64(File_To_Bytes(File.DirRootExternal, "aaaa.png"))
    
    Log($"OrgLength: ${org.Length}"$)
    '
    ' Compression
    '
    Dim compressed() As Byte = GZip.compress(org)
    Log($"CompressedBytesLength: ${compressed.Length}"$)
    Dim base64 As String = su.EncodeBase64(compressed)
    Log($"CompressedBytes converted to base64 Length: ${base64.Length}"$)

    Log($"CompressedBytes converted to base64: ${base64}"$)

    
    'Decompression
    
    Dim decompressedbytes() As Byte = su.DecodeBase64(base64)
    Log($"decompressedbytesLength: ${decompressedbytes.Length}"$)
    Dim bc As ByteConverter
    Dim uncompressed As String = bc.StringFromBytes(decompressedbytes,"UTF8")
    Log($"uncompressedLength: ${uncompressed.Length}"$)
    
    Log($"Decompressed String = ${uncompressed}"$)

Thanks in advance
 

Brian Dean

Well-Known Member
Licensed User
Longtime User
Why are you encoding in Base64 first? That increases the byte count by one third. Why do you not simply use GZip compression directly? And why worry about converting the bytes into text? Just compress the data using GZip.
 
Upvote 0

Serge Nova

Member
Why are you encoding in Base64 first? That increases the byte count by one third. Why do you not simply use GZip compression directly? And why worry about converting the bytes into text? Just compress the data using GZip.
Gzip only compresses strings, that's why I get the bytes from the image first, then I get the string by base64 encoding. I don't see how to directly compress a file (bytes) with GZip without encoding it first
 
Upvote 0

Serge Nova

Member
Why are you encoding in Base64 first? That increases the byte count by one third. Why do you not simply use GZip compression directly? And why worry about converting the bytes into text? Just compress the data using GZip.
If you have an example please post it.
Best regard
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
If you have an example please post it.
If you compress something (an image, pdf) ou get compressed bytes. Why you need it to convert it to a STRING? To be able you use a String you have to base64-Encode the compressed bytes.

The STRING (base64) is one third longer than just the bytes.

What you want to do with the compressed bytes? Why it is important to get a STRING?

Library LibArchiverPlus
 
Last edited:
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
Gzip only compresses strings ...
What you might have heard is that zipping is usually much more effective when compressing text than when compressing images, and some images will not zip at all well. That is because zipping works by finding repeated patterns in the data. Language text contains many such patterns and compresses well. Some images with areas of block colour, like cartoons or logos, might zip well too, but landscapes with cloudy skies usually will not. That is why JPEG, rather than zipping, is more common for images.
 
Upvote 0

Serge Nova

Member
If you compress something (an image, pdf) ou get compressed bytes. Why you need it to convert it to a STRING? To be able you use a String you have to base64-Encode the compressed bytes.
So far I can't compress bytes, when I saw the CompressStrings library which only offers me string compression, I thought that since I can't compress bytes then I can convert bytes to string then compress them with GZipStrings (from CompressStrings)

What you want to do with the compressed bytes? Why it is important to get a STRING?
I want to save the files in my database as text, by directly converting the bytes of a file into text by encoding64 sometimes I get several million characters (depending on the size of this file), it is why I want:
1. Either compress the bytes (of a file) then convert them into text by the base64 encoding (regardless of the slight increase in characters due to the base64 encoding) before storing them in my database;
2. Either convert the bytes into text then compress these texts before storing them in my database
 
Upvote 0

Serge Nova

Member
What you might have heard is that zipping is usually much more effective when compressing text than when compressing images, and some images will not zip at all well. That is because zipping works by finding repeated patterns in the data. Language text contains many such patterns and compresses well. Some images with areas of block colour, like cartoons or logos, might zip well too, but landscapes with cloudy skies usually will not. That is why JPEG, rather than zipping, is more common for images.
Ok, now I understand why texts obtained from base 64 encoding do not compress.
Thank you very much for the explanation Brian Dean
 
Upvote 0
Top