在java中解压字节数组

Rak*_*mar 3 java compression qr-code

在解压缩 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);
Run Code Online (Sandbox Code Playgroud)

小智 5

问题是您正在使用java的Inflater类,它使用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();
Run Code Online (Sandbox Code Playgroud)