在Java中解压缩字节数组

时间:2019-03-30 05:31:26

标签: java compression qr-code

在按照https://uidai.gov.in/images/resource/User_manulal_QR_Code_15032019.pdf进行解压缩adhaar qr代码样本数据步骤的同时,我得到了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);

2 个答案:

答案 0 :(得分:1)

问题是您正在使用Java的Inflater类,该类使用Zlib压缩算法。但是,在UIDAI安全QR码中,正在使用GZip压缩算法。因此,减压逻辑必须进行如下修改: ByteArrayOutputStream os = new ByteArrayOutputStream(); try { ByteArrayInputStream in = new Byte在此处输入代码ArrayInputStream(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(); ;

答案 1 :(得分:1)

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

它支持安全的xml和uid_number类型