0

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

Mina Kolta
  • 1,551
  • 2
  • 12
  • 22
  • 1
    You want to compress strings from argv, really? Or do you mean you want to treat argv's elements as names of files to compress, like gzip the program does? – John Zwinck Aug 20 '11 at 02:08
  • sure i need to pass a file name and get the compressed string , what if the string contains spaces ?? it'll be tens of arguments , sorry for the explanation – Mina Kolta Aug 20 '11 at 02:35
  • What do you mean, "What if the string contains spaces?" Which string? The filename, or the data contained within that file? It would be helpful if you had a lot more clarity when you are trying to get some programming done. – John Zwinck Aug 20 '11 at 03:05
  • you said **You want to compress strings from argv, really?** ,I'm trying to tell you that it's not applicable to pass a string in the arguments because if this string had spaces like "`data toCompress`" it will be considered that i'm passing 2 arguments not 1,what i need exactly to pass file name and the output file and get the result in this output file – Mina Kolta Aug 20 '11 at 12:46
  • You just need to quote the filenames, or use backslashes to escape spaces, so that your shell will populate argv with the filenames you want. So `myprog 'file 1 I want' 'another file'` will give your program the two intact filenames including spaces in argv[1] and [2]. – John Zwinck Aug 20 '11 at 13:17

1 Answers1

1

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);
}
user885074
  • 481
  • 2
  • 5