需要一个关于 easyzlib 的示例(如何压缩和解压缩从命令行 Windows/Linux 中发送到 argv 中的字符串)

发布于 2024-11-30 22:03:57 字数 2209 浏览 1 评论 0原文

好的,现在已经完成一半了,这就是我所做的:

`int main(int argc,char* argv[]){
 FILE *inputFile,*outputFile;
unsigned char *inputBuffer, *outputBuffer;
unsigned char *readMod = "r";
int result,x;
long int outputSize;
long int outputSizeun;
size_t inputSize;
  if(argc >= 3){
            inputFile = fopen(argv[2],readMod );
            // get length of input
            fseek(inputFile, 0, SEEK_END);
            inputSize = ftell(inputFile);
            fseek(inputFile, 0, SEEK_SET);
            //allocate the inputBufer size
            inputBuffer = (unsigned char *)malloc(inputSize);
            fread(inputBuffer, 1, inputSize, inputFile);
            outputSize = EZ_COMPRESSMAXDESTLENGTH(inputSize);
            //allocate the outputBuffer size
            outputBuffer = (unsigned char *)malloc(outputSize);
            //check for the -z(compression)/-u(decompression) option     s
            if(strcmp("-z",argv[1])==0){
                result = ezcompress(outputBuffer, &outputSize,     inputBuffer, inputSize);
            }else if(strcmp("-u",argv[1])==0){
                result = ezuncompress(outputBuffer,     &outputSizeun, inputBuffer, inputSize);
            }else{
                printf("Error : unknown operation \" %s \" type     -z for compression or -u for decompression\n",argv[1]);
            }
            if (result == 0) {
                    // if the output filename was not      present it output the compressed data into a file named "compress"
                if(argv[3] == NULL){ argv[3] = "output";}
                //write the output 
                outputFile = fopen(argv[3], "w");
                fseek(outputFile, 0, SEEK_END);
                fseek(outputFile, 0, SEEK_SET);
                fwrite(outputBuffer, 1, outputSize, outputFile);
                fclose(outputFile);
            } else {
                // Something went wrong
                printf("%d ",result);
            }
            //now freeing buffers
            free(inputBuffer);
            free(outputBuffer);
    }else{
        printf("insufficnt Arguments :-s");
        return 1;
    }
    return 0;
 }

为什么当我运行 **a.exe -u 输出时此代码返回 -3

OK now am half the way ,This is what i've done:

`int main(int argc,char* argv[]){
 FILE *inputFile,*outputFile;
unsigned char *inputBuffer, *outputBuffer;
unsigned char *readMod = "r";
int result,x;
long int outputSize;
long int outputSizeun;
size_t inputSize;
  if(argc >= 3){
            inputFile = fopen(argv[2],readMod );
            // get length of input
            fseek(inputFile, 0, SEEK_END);
            inputSize = ftell(inputFile);
            fseek(inputFile, 0, SEEK_SET);
            //allocate the inputBufer size
            inputBuffer = (unsigned char *)malloc(inputSize);
            fread(inputBuffer, 1, inputSize, inputFile);
            outputSize = EZ_COMPRESSMAXDESTLENGTH(inputSize);
            //allocate the outputBuffer size
            outputBuffer = (unsigned char *)malloc(outputSize);
            //check for the -z(compression)/-u(decompression) option     s
            if(strcmp("-z",argv[1])==0){
                result = ezcompress(outputBuffer, &outputSize,     inputBuffer, inputSize);
            }else if(strcmp("-u",argv[1])==0){
                result = ezuncompress(outputBuffer,     &outputSizeun, inputBuffer, inputSize);
            }else{
                printf("Error : unknown operation \" %s \" type     -z for compression or -u for decompression\n",argv[1]);
            }
            if (result == 0) {
                    // if the output filename was not      present it output the compressed data into a file named "compress"
                if(argv[3] == NULL){ argv[3] = "output";}
                //write the output 
                outputFile = fopen(argv[3], "w");
                fseek(outputFile, 0, SEEK_END);
                fseek(outputFile, 0, SEEK_SET);
                fwrite(outputBuffer, 1, outputSize, outputFile);
                fclose(outputFile);
            } else {
                // Something went wrong
                printf("%d ",result);
            }
            //now freeing buffers
            free(inputBuffer);
            free(outputBuffer);
    }else{
        printf("insufficnt Arguments :-s");
        return 1;
    }
    return 0;
 }

why does this code returns -3 when i run **a.exe -u output

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗恋未遂 2024-12-07 22:03:57

ezcompress 和 ezuncompress 直接处理表示为字符数组的文件数据。

for (int i = 1; i < argc; i++) {
    // open argument you want to compress
    FILE *inputFile = fopen(argv[i], "r");

    // get length of input
    // http://stackoverflow.com/questions/238603/how-can-i-get-a-files-size-in-c
    fseek(inputFile, 0, SEEK_END);
    size_t inputSize = ftell(inputFile);
    fseek(inputFile, 0, SEEK_SET);

    unsigned char *inputBuffer = (unsigned char *)malloc(inputSize);

    fread(inputBuffer, 1, inputSize, inputFile);

    long outputSize = EZ_COMPRESSMAXDESTLENGTH(sz);
    unsigned char *outputBuffer = (unsigned char *)malloc(outputSize);
    int result = ezcompress(outputBuffer, &outputSize, inputBuffer, inputSize);

    if (result != EZ_BUF_ERROR) {
        // Do stuff with outputBuffer which has length outputSize
    } else {
        // Something went wrong
    }

    free(intputBuffer);
    free(outputBuffer);
}

ezcompress and ezuncompress work on the file data directly which are represented as arrays of characters.

for (int i = 1; i < argc; i++) {
    // open argument you want to compress
    FILE *inputFile = fopen(argv[i], "r");

    // get length of input
    // http://stackoverflow.com/questions/238603/how-can-i-get-a-files-size-in-c
    fseek(inputFile, 0, SEEK_END);
    size_t inputSize = ftell(inputFile);
    fseek(inputFile, 0, SEEK_SET);

    unsigned char *inputBuffer = (unsigned char *)malloc(inputSize);

    fread(inputBuffer, 1, inputSize, inputFile);

    long outputSize = EZ_COMPRESSMAXDESTLENGTH(sz);
    unsigned char *outputBuffer = (unsigned char *)malloc(outputSize);
    int result = ezcompress(outputBuffer, &outputSize, inputBuffer, inputSize);

    if (result != EZ_BUF_ERROR) {
        // Do stuff with outputBuffer which has length outputSize
    } else {
        // Something went wrong
    }

    free(intputBuffer);
    free(outputBuffer);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文