开发者

need an example on easyzlib (how to compress and decompress strings sent in the argv from command line Windows/Linux)

开发者 https://www.devze.com 2023-03-29 04:32 出处:网络
OK now am half the way ,This is what i\'ve done: `int main(int argc,char* argv[]){ FILE *inputFile,*outputFile;

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


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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消