문제

.NET에 gziped 인 Base64 문자열이 있으며 Java의 문자열로 다시 변환하고 싶습니다. 특히 C# 구문과 동등한 자바를 찾고 있습니다.

  • Convert.Frombase64String
  • 메모리 스트림
  • gzipstream

변환하려는 방법은 다음과 같습니다.

public static string Decompress(string zipText) {
    byte[] gzipBuff = Convert.FromBase64String(zipText);

    using (MemoryStream memstream = new MemoryStream())
    {
        int msgLength = BitConverter.ToInt32(gzipBuff, 0);
        memstream.Write(gzipBuff, 4, gzipBuff.Length - 4);

        byte[] buffer = new byte[msgLength];

        memstream.Position = 0;
        using (GZipStream gzip = new GZipStream(memstream, CompressionMode.Decompress))
        {
            gzip.Read(buffer, 0, buffer.Length);
        }
        return Encoding.UTF8.GetString(buffer);
     }
}

모든 포인터는 감사합니다.

도움이 되었습니까?

해결책

Base64의 경우 Base64 수업 Apache Commons에서 decodeBase64 a String 그리고 반환 a byte[].

그런 다음 결과를 읽을 수 있습니다 byte[] a ByteArrayInputStream. 마침내 통과하십시오 ByteArrayInputStream a gzipinputstream 압축되지 않은 바이트를 읽으십시오.


코드는이 라인을 따라 무언가처럼 보입니다.

public static String Decompress(String zipText) throws IOException {
    byte[] gzipBuff = Base64.decodeBase64(zipText);

    ByteArrayInputStream memstream = new ByteArrayInputStream(gzipBuff);
    GZIPInputStream gzin = new GZIPInputStream(memstream);

    final int buffSize = 8192;
    byte[] tempBuffer = new byte[buffSize ];
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    while ((size = gzin.read(tempBuffer, 0, buffSize)) != -1) {
        baos.write(tempBuffer, 0, size);
    }        
    byte[] buffer = baos.toByteArray();
    baos.close();

    return new String(buffer, "UTF-8");
}

코드를 테스트하지는 않았지만 몇 가지 수정으로 작동해야한다고 생각합니다.

다른 팁

Base64의 경우 추천합니다 iholder의 구현.

gzipinputstream GZIP 바이트 어레이를 압축 해제하는 데 필요한 것입니다.

BytearRayoutputStream은 메모리에 바이트를 작성하는 데 사용하는 것입니다. 그런 다음 바이트를 가져 와서 문자열 객체의 생성자로 전달하여 변환하고 바람직하게는 인코딩을 지정합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top