猿问

解压缩 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);


森栏
浏览 201回答 2
2回答

犯罪嫌疑人X

问题是你使用的是使用Zlib压缩算法的java的膨胀类。然而,在UIDAI安全QR码中,正在使用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类型
随时随地看视频慕课网APP

相关分类

Java
我要回答