简体   繁体   English

在java中解压缩字节数组

[英]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在解压缩 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);

The problem is that you are using Inflater class of java which uses Zlib compression algorithm.问题是您使用的是使用 Zlib 压缩算法的 Java 的 Inflater 类。 However in UIDAI secure QR code, GZip compression algorithm is being used.但是在UIDAI安全二维码中,使用了GZip压缩算法。 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这是正确解码数据的项目: https : //github.com/dimagi/AadharUID

It supports secure, xml and uid_number type它支持安全、xml 和 uid_number 类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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