Friday, April 27, 2012

Unzip a ".gz" file in ColdFusion

Below is the code for ColdFusion function to unzip a ".gz" file. The code describes each logic with comment line. Hope it will not create any trouble for understanding.
<cfscript>
    function unzipGZ(inputFilePath)
    {
        var returnString = "";
        var outFileName = "";
        var outPath = GetDirectoryFromPath(inputFilePath);
        var inputFileName = getFileFromPath(arguments.inputFilePath);
        var buffer = repeatString(" ",1024).getBytes(); //Create a buffer of 1024 bytes
        
        var bufferLen = 0;
        var inStream = createObject("java", "java.io.FileInputStream");
        var outStream = createObject("java", "java.io.FileOutputStream");
        var gzInStream = createObject("java", "java.util.zip.GZIPInputStream");
        
        //OutPut File Name
        outFileName = left(inputFileName, (len(inputFileName) - 3));
        
        try{
            
            //File UnZip Operation
            inStream.init(inputFilePath);
            gzInStream.init(inStream);
            outStream.init(outPath & outFileName);
            
            //Read the file stream into buffer and write into out put file
            do{
                bufferLen = gzInStream.read(buffer, 0, 1024);
                if( bufferLen != -1 )
                    outStream.write(buffer, 0, bufferLen);
            } while(bufferLen != -1);
            
            //Store the OutPut File Path in the function return string
            returnString = outPath & outFileName;
        } catch(Any e){
            WriteDump(e);abort;
            
            //Store the error message in the return string
            returnString = e.message & "Details: #e.Detail#";
        } finally{
            try{
                //Close all Opened file Streams
                outStream.close();
                gzInStream.close();
                inStream.close();
            }catch(Any e){}
        }
        
        return returnString;
    }
    
   request.fileAfterUnZip = unzipGZ("D:\YMLDP_Data\Temp\listings-leaserental.txt.gz");
   WriteDump(request.fileAfterUnZip);
</cfscript>

1 comment:

  1. Above code show me my path
    C:\xampp\htdocs\zip\bt_ads

    ReplyDelete

Followers