I'm compressing my image (in base64 string format) on client side using LZMA-JS, and then sending the compressed byte array to server. There I want to decompress the byte array and save it in DB.
Client Code:
LZMA.compress(base64Img, 9, function on_compress_complete(result) {
imageCallback(result);
}, function on_compress_progress_update(percent) {
console.log("Compressing: " + (percent * 100) + "%");
});
I tried using LZMACompressorInputStream and LZMA2InputStream at server to decompress the same, but both give "org.Pukalani.xz.CorruptedInputException: Compressed data is corrupt" exception.
Server Side Code:
private byte[] decompressByteArray(final byte[] bytes) throws IOException {
try (final ByteArrayOutputStream os = new ByteArrayOutputStream();) {
final ByteArrayInputStream in = new ByteArrayInputStream(bytes);
final LZMACompressorInputStream gis = new LZMACompressorInputStream(in);
final byte[] buffer = new byte[1024];
int len;
while ((len = gis.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
gis.close();
return os.toByteArray();
} catch (final IOException e) {
log.error("error", e);
return new ByteArrayOutputStream().toByteArray();
}
}
Is there any other way to decompress the byte array? I have a around 30 images in base64 format which I want to send in single call, is there any other way to send these across.