Monday, May 9, 2016

SilkPerformer GetFileContent() Function

SilkPerformer provides a very handy function that allows one to read in an entire file into a single variable. However, it works only if the file is text. When GetFileContent() encounters a null byte (0x00), it truncates the rest of the file.

I wanted to use this function to uncompress a gzipped file using ZipUncompressGzip() or ZipUncompressGzip2(). I wasn't able to do so; hence, I used FOpen(), FRead(), and FClose() to retrieve the file content into a string variable. Then I was able to use ZipUncompressGzip2() to get the job done. Yes, it worked!

  1.     FOpen(hZipFile, sSoapFile, OPT_FILE_ACCESS_READ);  
  2.     FRead(hZipFile, sTemp, 10000);  // should be large enough  
  3.     FClose(hZipFile);  
  4.     ZipUncompressGzip2(sTemp, sSoapMsg);  

If you wish to read in a larger file, you would have to modify the code around FRead() such that you can read in a loop until the entire content is read in. Or use FSizeGet() to get the size of the file and call FRead() appropriately. Here's the revised version:

  1.     FOpen(hZipFile, sSoapFile, OPT_FILE_ACCESS_READ);  
  2.     FSizeGet(hZipFile, nLen);               // get the file size  
  3.     FRead(hZipFile, sTemp, nLen);           // read in the whole thing  
  4.     FClose(hZipFile);                       // don't forget to close it  
  5.     ZipUncompressGzip2(sTemp, sSoapMsg);    // now uncompress  

No comments:

Post a Comment