在java中缩小压缩

时间:2015-11-20 11:46:33

标签: java compression deflate

我们要求向IDP发送一个saml请求。请求应该缩小,base64& URL encoded.Below是用于收缩请求的代码。

       byte[] encodedArray=IDPUtil.compress(samlRequest.getBytes());

       public static byte[] compress(byte[] data){

        byte[] output = null;
        try {
            Deflater deflater = new Deflater();  
            deflater.setInput(data);  
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);   
            deflater.finish();  
            byte[] buffer = new byte[1024];   
            while (!deflater.finished()) {  
                int count = deflater.deflate(buffer); 
                outputStream.write(buffer, 0, count);
                output = outputStream.toByteArray();  
                System.out.println("Original: " + data.length / 1024 + " Kb");  
                System.out.println("Compressed: " + output.length / 1024 + " Kb");  
            }  
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return output;  
    }

在IDP结束时,我们正在获得以下异常

        java.util.zip.DataFormatException: invalid bit length repeat
        at java.util.zip.Inflater.inflateBytes(Native Method)
        at java.util.zip.Inflater.inflate(Inflater.java:238)

以下是解压缩逻辑

   String urlDecodedString=URLDecoder.decode(urlEncodedString,"UTF-8");
        byte[] base64DecodedArray=base64.decode(urlDecodedString);
        System.out.println(new String(decompress(base64DecodedArray)));

       public static byte[] decompress(byte[] data){

         System.out.println("Length : "+data.length/1024 + "KB");

        byte[] output = null ;
        Inflater inflater = new Inflater();   
        inflater.setInput(data);  
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);  
      byte[] buffer = new byte[1024]; 
      try {
        while (!inflater.finished()) {  
            int count;
            count = inflater.inflate(buffer);
            outputStream.write(buffer, 0, count);  
        } 
         outputStream.close();
         output = outputStream.toByteArray(); 
       } catch (DataFormatException e) {
         e.printStackTrace();
       } catch (IOException e) {
        e.printStackTrace();
      }  
      return output;  
     }

请推荐一下这里的内容?

0 个答案:

没有答案