在 Java 中解压缩字节数组



在解压缩 adhaar QR 码示例数据步骤时,按照给定 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);

问题是你使用的是使用Zlib压缩算法的Java的Inflater类。然而,在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类型

最新更新