in reply to keeping binary data raw

"Surprising results" is a rather useless diagnostic, especially when the program can't easily be run.

.= means "append to". Did you check what's initially in $compressed?

Update: Oh! Is the return value of your function NUL-terminated? char* is treated as a pointer to a NUL-terminated string. I think you need to return an SV.

Update: Doh! tye posted a more detailed version of what I said in my update while I was writting it. Please read his post.