使用LZ4压缩/解压缩

时间:2018-03-16 08:52:45

标签: c++ lossless-compression lz4

当我用LZ4压缩/解压缩时,它给了我一些与原始字符串无关的东西。

例如" 1234"是好的,但不是一句话。

以下是更正的代码,感谢您的答案

   int Compress(const char* source, char* dest, int sourceSize, int maxDestSize)
    {
        return LZ4_compress_default(source, dest, sourceSize, maxDestSize);
    }

    int Decompress(const char* source, char* dest, int compressedSize, int maxDecompressedSize)
    {
        return LZ4_decompress_safe(source, dest, compressedSize, maxDecompressedSize);
    }

    int main()
    {
        char source[1024] = "Arguing that you don\'t care about the right to privacy because you have nothing to hide is no different than saying you don\'t care about free speech because you have nothing to say.";

        char buff[1024 * 4] = { 0 };
        char buff_a[1024 * 4] = { 0 };

        int x = Compress(source, buff, strlen(source), sizeof(buff));
        printf("\nBYTE COMPRESSED : %i\nDATA COMPRESSED : \n", x);
        for (int i = 0; i < x; i++)
            printf(" %2x", (unsigned)buff[i]);

        int y = Decompress(buff, buff_a, x, sizeof(buff_a));
        printf("\n\nBYTE DECOMPRESSED : %i\nDATA DECOMPRESSED : \n", y);
        for (int i = 0; i < y; i++)
            printf(" %02x", (unsigned)buff_a[i]);

        printf("\n\n\n\nEXIT\n");
        getchar();
        return 0;
    }

0 个答案:

没有答案