Generating data matrix from a ZIP string in Java

I’m trying to generate a data matrix from a zip string using Zxing in Java and the result is java.lang.IllegalArgumentException: Message contains characters outside ISO-8859-1 encoding.

The code is:

byte[] bytesOfZIP= (byte[])statD.returnObj; // here is the zipped content
String texto = new String(bytesOfZIP, "UTF8");
DataMatrixWriter barcodeWriter = new DataMatrixWriter();
BitMatrix bitMatrix = barcodeWriter.encode(texto, BarcodeFormat.DATA_MATRIX, 400, 400);// exception appears at this line
BufferedImage bi = MatrixToImageWriter.toBufferedImage(bitMatrix);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bi, "jpg", baos);
byte[] fbytes = baos.toByteArray();

I’m looking for any solution to encode a ZIP file with these special chars not necessarily with Zxing.

Answer

Not an answer

There are some problems, hence in answer form.

byte[] bytesOfZIP = (byte[])statD.returnObj; // here is the zipped content

An Object (or char[], or String) cannot be case to a byte[] array.

String texto = new String(bytesOfZIP, "UTF8");

This will compile but the official name is "UTF-8" and as already mentioned, StandardCharsets.UTF_8 would be nicer (no UnsupportedEncodingException as UTF-8m is standard, always available). However bytesOfZIP are never UTF-8 text, which requires a special format. In fact binary data should never be pressed in String with 2 byte chars. It requires twice a conversion to Unicode, double memory, and is almost certain to be corrupted.

DataMatrixWriter barcodeWriter = new DataMatrixWriter();
BitMatrix bitMatrix = barcodeWriter.encode(texto, BarcodeFormat.DATA_MATRIX, 400, 400);

BufferedImage bi = MatrixToImageWriter.toBufferedImage(bitMatrix);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bi, "jpg", baos);
byte[] fbytes = baos.toByteArray();

JPEG (.jpg, .jpeg) is a lossy compression. For a black-and-white barcode try use png.

Solution

Unzipping should first check the format: a test.txt.gz would use a GZipInputStream. A test.zip a ZipFile, a ZipInputStream, or copying from zip file system. These are standard classes to be found in the internet.

As the contents are probably pure text, you can first try it hard-coded after manually unzipping.

If you start with a byte[] then the matrix printer probably requires MS Latin 1, or Charset.fromName("Windows-1252"). Latin-1, ISO-8859-1 or StandardCharsets.ISO_8859_1 has a forbidden control range from u0080 - u00BF.