0

I have an byte[] of about 9mil bytes, and I would like to store it to a file. I wanted to use Deflater to compress the array, but it seems like the process only made the array (slightly) bigger. Is my data incompressible? If so, why? Are there any other ways to make the array smaller?

my code:

    ByteArrayOutputStream baos = null;
    Deflater dfl = new Deflater();
    dfl.setLevel(Deflater.BEST_COMPRESSION);
    dfl.setInput(data);
    dfl.finish();
    baos = new ByteArrayOutputStream();
    byte[] tmp = new byte[1024];
    try{
        while(!dfl.finished()){
            int size = dfl.deflate(tmp);
            baos.write(tmp, 0, size);
        }
    } catch (Exception ex){

    } finally {
        try{
            if(baos != null) baos.close();
        } catch(Exception ex){}
    }

    Log.d(TAG, data.length + " " + baos.toByteArray().length);
    return baos.toByteArray();

EDIT:

I converted an image from disk into a byte array. I then encrypted it. Now I would like to store that (encrypted) byte[] in a file, so that later I can read it and decrypt it, and therefore recreate the image. The original image is ~0.6MB, and the encrypted file turns out to be ~9MB.. I would like to avoid such a big difference, for the user's sake..

Rafal
  • 85
  • 7

1 Answers1

1

Encrypted data is supposed to behave like random noise. Random noise is uncompressible. I suggest you compress first, and then encrypt.

Now, if the image is in a compressed format (jpeg for instance), compressing it further isn't likely to help.

Buhb
  • 7,088
  • 3
  • 24
  • 38
  • Yes, the original images are jpgs. I understand why the file is so big since I am really getting width*height*4 bytes, ergo the big byte[]. So no hopes here to reduce the file? :( – Rafal Apr 17 '15 at 21:22
  • So it's not the actual jpeg data you're storing, but taking each pixel from the image and storing it as 4 bytes? That's a curious strategy. I'd just encrypt the original jpeg data and be done with it. If you still want to go your route, the good news is that your proprietary 4-bytes-per-pixel format is uncompressed, so as long as you compress first and then encrypt, you should be able to loose a few bytes. – Buhb Apr 17 '15 at 21:26
  • There really is no particular reason why I chose this way. The encryption function wants a byte array, so I just did bitmap.copyPixelsToBuffer(buf) and then buff.array(). Are you saying I can take a shortcut somewhere? – Rafal Apr 17 '15 at 21:31
  • If path is a path to a jpeg file on the file system, you can get to the data through new FileInputStream(path), and from there to byte array. Take a look here: http://stackoverflow.com/questions/2436385/android-getting-from-a-uri-to-an-inputstream-to-a-byte-array – Buhb Apr 17 '15 at 21:38
  • YOU ARE GOLDEN! The encrypted byte[] is now ~60KB! Thank you! I store it as .png file, and then read it back and decrypt it and get my image back. When I open the encrypted png from gallery I see nothing but a picture of one color, but I guess there is nothing I can do about it. – Rafal Apr 17 '15 at 22:01
  • Originally, I wanted to display some kind of "noisy image," just for a visual effect.. – Rafal Apr 17 '15 at 22:02