C-ZLib-反向压缩

C-ZLib-反向压缩,c,zlib,compression,C,Zlib,Compression,我刚刚在Visual Studio 2015上安装了ZLib 此代码: #include <stdio.h> #include <string.h> #include <assert.h> #include <Perso.h> #include "zlib.h" int main() { size_t fileLength = fileSize("Input.txt", 0); unsigned char* a = NUL

我刚刚在Visual Studio 2015上安装了ZLib

此代码:

#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <Perso.h>

#include "zlib.h"

int main()
{
    size_t fileLength = fileSize("Input.txt", 0);



    unsigned char* a = NULL;
    a = malloc(sizeof(unsigned char)*fileLength);
    verifyMalloc(a);
    nullString(a, fileLength);
    fileInUnsignedCharTab("Input.txt", a, fileLength, 0, 0, 0);

    unsigned char* b = NULL;
    b = malloc(sizeof(unsigned char)*fileLength);
    verifyMalloc(b);
    nullString(b, fileLength);

    unsigned char* c = NULL;
    c = malloc(sizeof(unsigned char)*fileLength);
    verifyMalloc(c);
    nullString(c, fileLength);



    uLong ucompSize = fileLength + 1;
    uLong compSize = compressBound(ucompSize);

    compress((Bytef *)b, &compSize, (Bytef *)a, ucompSize);
    uncompress((Bytef *)c, &ucompSize, (Bytef *)b, compSize);

    FILE* Output = NULL;
    Output = fopen("Output.txt", "wb+");
    fwrite(b, sizeof(b[0]), strlen(b), Output);
    fclose(Output);
}
#包括
#包括
#包括
#包括
#包括“zlib.h”
int main()
{
size\u t fileLength=fileSize(“Input.txt”,0);
无符号字符*a=NULL;
a=malloc(sizeof(无符号字符)*文件长度);
验证MALLOC(a);
空字符串(a,文件长度);
fileInUnsignedCharTab(“Input.txt”,a,fileLength,0,0,0);
无符号字符*b=NULL;
b=malloc(sizeof(无符号字符)*文件长度);
验证MALLOC(b);
空字符串(b,文件长度);
无符号字符*c=NULL;
c=malloc(sizeof(无符号字符)*文件长度);
验证malloc(c);
空字符串(c,文件长度);
uLong ucompSize=文件长度+1;
uLong compSize=压缩绑定(ucompSize);
压缩((Bytef*)b和compSize,(Bytef*)a和ucompSize);
解压((Bytef*)c和ucompSize,(Bytef*)b,compSize);
文件*Output=NULL;
Output=fopen(“Output.txt”、“wb+”);
fwrite(b,sizeof(b[0]),strlen(b),Output;
fclose(输出);
}
压缩文件Input.txt并将结果放入Output.txt。 现在,假设我将压缩文件Output.txt发送给一位朋友,他将如何解压缩该文件

我尝试了相同的代码,但只使用了

解压缩((Bytef*)c和ucompSize,(Bytef*)b,compSize)
但它给了我一个奇怪的结果,不可读

如果您有解压压缩输出的想法,那就太好了:)


提前感谢

在“output.txt”中,您不应该写入
strlen(b)
字节,而应该写入
compSize
。实际上,压缩流中可能有一些
num
字节,这将使
strlen(b)
小于
compSize
。此外,为什么要设置
ucompSize=fileLength+1
?(为什么
+1
?)。您应该测试
压缩
解压缩
函数的返回。感谢您的回答,没有更多问题:)这是因为strlen(b),这对compSize很好。+1代表终止符“\0”