B4J Question Zip a byte array to a byte array and unzip a byte array to another a byte array

hatzisn

Well-Known Member
Licensed User
Longtime User
Hello,

I am trying to zip and unzip a byte array to another byte array. The first Sub throws an exception in zis.RunMethod("write",...) method and the second returns the same array length it reads. I cannot figure out as I am not completely java literate what is wrong... Can anyone help? Thanks...

B4X:
Public Sub ZipByteArray(ba() As Byte) As Byte()
    
    Dim otp1 As OutputStream
    otp1.InitializeToBytesArray(0)
    
    Dim zis As JavaObject
    zis.InitializeNewInstance("java.util.zip.ZipOutputStream", Array(otp1))
    zis.RunMethod("write", Array(ba, 0, ba.Length))
    
    Dim bRe() As Byte
    bRe = otp1.ToBytesArray
    
    Return bRe


End Sub

Public Sub UnzipByteArray(ba() As Byte) As Byte()
    

    Dim inp1 As InputStream
    inp1.InitializeFromBytesArray(ba, 0, ba.Length)
    Log(inp1.BytesAvailable)


    Dim zis As JavaObject
    zis.InitializeNewInstance("java.util.zip.ZipInputStream", Array(inp1))
    
    Dim inp2 As InputStream = zis
    
    Return Bit.InputStreamToBytes(inp2)
    
'    InputStream Is = new ByteArrayInputStream(ba);
'    InputStream zis = new ZipInputStream(Is);
End Sub
 

drgottjr

Expert
Licensed User
Longtime User
here is one way to do it with inline java. get rid of your current b4a code.

B4X:
    Dim bytearray() As Byte = "some string to zip".GetBytes("UTF-8")
    Log(bytearray.Length)
    Dim compress() As Byte = jo.RunMethod("compress", Array(bytearray, False))
    Log(compress.Length)
    Log(bc.HexFromBytes(compress))
    Log("now trying to decompress...")
    Dim uncompressed() As Byte = jo.RunMethod("decompress", Array( compress,False ))
    Log(bc.StringFromBytes(uncompressed,"UTF8"))


#if Java
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

  public static byte[] compress(byte[] input, boolean GZIPFormat) throws IOException {
      
    Deflater compressor = new Deflater(Deflater.DEFAULT_COMPRESSION, GZIPFormat);
    compressor.setInput(input);
    compressor.finish();
    ByteArrayOutputStream bao = new ByteArrayOutputStream();
    byte[] readBuffer = new byte[1024];
    int readCount = 0;
    while (!compressor.finished()) {
      readCount = compressor.deflate(readBuffer);
      if (readCount > 0) {
        bao.write(readBuffer, 0, readCount);
      }
    }
    compressor.end();
    return bao.toByteArray();
  }
 
  public static byte[] decompress(byte[] input, boolean GZIPFormat)
      throws IOException, DataFormatException {
    Inflater decompressor = new Inflater(GZIPFormat);
    decompressor.setInput(input);
    ByteArrayOutputStream bao = new ByteArrayOutputStream();
    byte[] readBuffer = new byte[1024];
    int readCount = 0;
    while (!decompressor.finished()) {
      readCount = decompressor.inflate(readBuffer);
      if (readCount > 0) {
        bao.write(readBuffer, 0, readCount);
      }
    }
    decompressor.end();
    return bao.toByteArray();
  }

#End If

zipping and unzipping isn't necessarily as strightforward as it might appear. you have compression levels, compression methods, and actual zip types to take into account. you should research the matter more. the posted code can be modified to handle these considerations.

for these types of routines, there is inline java and javaobject. i realize erel is fond of javaobject, and although i am relatively comfortable doing things this way (as you tried in your code), i find some routines easier for me to deal with in inline java.
 
Upvote 0

emexes

Expert
Licensed User
Last edited:
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
I just love the Way the title is, with repeating words.

word Count​

Byte - 4
Array - 4
a - 4
to - 2
Zip - 1
Unzip - 1
and - 1
another - 1
:cool:

Now you have the building blocks to build a TLM after me (TLM=Tiny Language Model 😂 😂 😂 😂 😂 😂 ).
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
Thank you all, informative or fun, I certainly got benefited...
 
Upvote 0
Top