在 java 中解压字节数组

Decompressing byte array in java

解压 adhaar 二维码示例数据步骤如下 https://uidai.gov.in/images/resource/User_manulal_QR_Code_15032019.pdf,我得到 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);

问题是您正在使用 java 的 Inflater class,它使用 Zlib 压缩算法。但是在 UIDAI 安全二维码中,使用的是 GZip 压缩算法。所以解压逻辑要修改如下:-

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();

这是正确解码数据的项目: https://github.com/dimagi/AadharUID

它支持安全,xml和uid_number类型