Descompactar string GZipB64 - Java

Estou fazendo um projeto api rest onde tenho retorno de json e um dos campos vem como base64, preciso descompactar o campo para poder ter a string xml ou json.

Segue abaixo o código que estou utilizado para obter o retorno.

public final void run() {
    try {
        OkHttpClient httpsClient = createHttpsClient();
        Request request = new Request.Builder().url(call).build();

        Response response = httpsClient.newCall(request).execute();

        if (!response.isSuccessful())
            throw new NfseException(NFSeMessageErrorEnum.INVALID_ENV, Integer.toString(response.code()));

        Gson gson = new Gson();
        SefinNacionalXml sefinNfse = gson.fromJson(response.body().string(), SefinNacionalXml.class);
        
        byte[]  base64 = Base64.getDecoder().decode(sefinNfse.getNfseXmlGZipB64());
        String msgDecode  = new String(base64, "UTF-8");
        System.out.print(msgDecode);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

E na hora de tentar descompartar e ter a string, está vindo um monte de caracteres especiais.

Retorno

5����a��J�Rd|,Hz
�bc��v�p�@>"�;����p���.���������f T�^��6�w�@� �����d�G���s_�aW���ŕ�4EZb�1�2��xqճҎU�\N���&�cfq��r4���/CX�n���Kɥ�%�W�����E�
�l

Conteúdo do campo que estou tentando converter para string.

H4sIAAAAAAAEALVY2Y7jSnJ991cIdQG/1HRx0d5Wa5xcRRYXcZWoF4ObSEpcJJISScEPNu4f+A8GfvDT/MRcwB/iL3GSVFWp+3bP3DvANKBiZDIyIjLixMlkL/5YJ/Hg6udFlKVfnrAX9Gngp27mRWnw5cnQmU+zpz8uFxKj+QOomRZfnsKyPH1GkKqqXoqT773s7ZufevZLkF1fnBxJ94X/1Ot+Tgv8K/1q+JLlAYKjKIqgcwQqeUUU/PTUBWBnnX/0abmI0n3nkfO+PEFhiKETuAbD0dFoOB0PR3A9NkTxIXr/N8IxHCrNpyg6HU1m+ATaqIXMpZNoSdCCPFjJKreTJZ1eIG/zncI694vSdu3su2ofbxdpG8/yK48LpJ9cuFCVS93IW94DXSAfU52bXvyeizclPY8cyXaXIIDZj+zET8vsDwM3y3O/tAM/GWSXQZSWfp74XmT/8vNf/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KXetTfN9HpnY86i3OJXDsUDrz2rDvXprKpkrs41H8XMK3pMoiskRZEsDsQom8fGWezObyc/zGqdKdhTMzf1NXqN4HRwE/mJJ695iTQLiRMzm3pwOt3N4uOFU46qcUGhnQjUR6kjOmgDJLN2ZmFTsek0WVBdDwZnjcWW8TmrUF614RlFZHJx7i5JKpuvGyHJ1vD5rGb2+caZe7IupyrskPQkaCCBjhMJL/rc3+H6mv90j7zf+j28BKHf/D/D/NMVWQ90aAAA=

É que você não está descompactando, você só converteu o Base64, falta descompactar o Gzip:

byte[] bytesCompactados = Base64.getDecoder().decode(sefinNfse.getNfseXmlGZipB64());
GZIPInputStream descompactador = new GZIPInputStream(new ByteArrayInputStream(bytesCompactados));
String msgDecode = IOUtils.toString(descompactador);
System.out.print(msgDecode);

Dica:
Use nomes coerentes para suas variáveis, você chamou seus bytes de base64, mas esses bytes não estão em Base 64, afinal você decodificou a String original, essa sim estava em Base 64.

1 curtida

Aqui tem uma forma usada para descompactar a NF-e, provavelmente também funciona para a NFS-e:

1 curtida

Muito obrigada pessoal, deu certo. Muito obrigada pela ajuda.

1 curtida