简体   繁体   中英

Decompressing byte array in java

While decompressing adhaar qr code sample data steps follow as given https://uidai.gov.in/images/resource/User_manulal_QR_Code_15032019.pdf , I got java.util.zip.DataFormatException: incorrect header check error while decompressing the byte array

// getting aadhaar sample qr code data from

// https://uidai.gov.in/images/resource/User_manulal_QR_Code_15032019.pdf

String s ="taking  here Aadhaar sample qr code data";
BigInteger bi = new BigInteger(s, 10); 

byte[] array = bi.toByteArray();    
Inflater decompresser = new Inflater(true);
decompresser.setInput(array);
ByteArrayOutputStream outputStream = new 
ByteArrayOutputStream(array.length);
byte[] buffer = new byte[1024];  
while (!decompresser.finished()) {  
    int count = decompresser.inflate(buffer);  
    outputStream.write(buffer, 0, count);  
}  
outputStream.close();  
byte[] output = outputStream.toByteArray(); 
String st = new String(output, 0, 255, "ISO-8859-1");
System.out.println("==========="+st);

The problem is that you are using Inflater class of java which uses Zlib compression algorithm. However in UIDAI secure QR code, GZip compression algorithm is being used. So the decompression logic has to be modified as following:-

ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            ByteArrayInputStream in = new ByteArrayInputStream(data);
            GZIPInputStream gis = new GZIPInputStream(in);
            byte[] buffer = new byte[1024];
            int len;
            while((len = gis.read(buffer)) != -1){                                        os.write(buffer, 0, len);
            }
            os.close();
            gis.close();
        }
        catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        byte[] output = os.toByteArray();

This is the project which decoded the data Correctly: https://github.com/dimagi/AadharUID

It supports secure, xml and uid_number type

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM