<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>
Tips and Tricks for ColdFusion and Other Web Technologies like Java, jQuery, DataBase
Showing posts with label cfzip. Show all posts
Showing posts with label cfzip. Show all posts
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.
Labels:
cfzip,
ColdFusion,
UnZip .gz
Tuesday, April 24, 2012
Filtering CFZIP file extraction Operation
To extract a zip file in ColdFusion we have a wonderful tag "cfzip". By, using which we can list the files available in that zip or we can give password for password encrypted zip to extract all files.
Sometimes we need to extract only 1/2 files from the zip bundles then how can we extract only the required files using cfzip.
<cfzip action="unzip" file="#local.inFilePath#" destination="#local.outputPath#" recurse="true" filter="#local.fileList.name#" overwrite="true">
</cfzip>
file: Takes the input zip file.
destination: It's the destination directory in which you want to extract the files.
recurse: Specifies whether action applies to sub directories in the zip bundle.
overwrite: If a file exists with the same name in the destination directory then it will overwrite.
filter:Filter the files which you need to extract.
Case 1: Extract All text files from the zip bundle
filter = "*.txt";
Case 2: Extract a specific file(xyz.jpg) from the zip bundle
filter = "xyz.jpg";
Case 3: Extract all files whose file name contains string "listing"
filter = "*listing*.txt"
Case 4: Extract more than one files with specific file names
filter = "abc.txt|xyz.txt". It will extract only abc.txt and xyz.txt
The filter is applicable to the following actions of "cfzip"
1. delete
2. list
3. unzip
4. zip
Hope it will help you...
Sometimes we need to extract only 1/2 files from the zip bundles then how can we extract only the required files using cfzip.
<cfzip action="unzip" file="#local.inFilePath#" destination="#local.outputPath#" recurse="true" filter="#local.fileList.name#" overwrite="true">
</cfzip>
file: Takes the input zip file.
destination: It's the destination directory in which you want to extract the files.
recurse: Specifies whether action applies to sub directories in the zip bundle.
overwrite: If a file exists with the same name in the destination directory then it will overwrite.
filter:Filter the files which you need to extract.
Case 1: Extract All text files from the zip bundle
filter = "*.txt";
Case 2: Extract a specific file(xyz.jpg) from the zip bundle
filter = "xyz.jpg";
Case 3: Extract all files whose file name contains string "listing"
filter = "*listing*.txt"
Case 4: Extract more than one files with specific file names
filter = "abc.txt|xyz.txt". It will extract only abc.txt and xyz.txt
The filter is applicable to the following actions of "cfzip"
1. delete
2. list
3. unzip
4. zip
Hope it will help you...
Labels:
cfzip,
ColdFusion,
zip
Subscribe to:
Posts (Atom)