CFLib.org – Common Function Library Project

unzipFile(zipFilePath, outputPath)

Last updated September 01, 2003

author

Samuel Neff

Version: 1 | Requires: CF6 | Library: FileSysLib

Description:
unzipFile() utilizes the built in java.util.zip package and requires no software be installed on the server. Pass in the path to a zip file and a target directory and it will unzip the contents. Due to a bug in CFMX 6.0 the UDF does require CFMX 6.1.

Return Values:
void

Example:

<!--- 

unzipFile(expandPath("test.zip"), expandPath("targetDir/"))

--->

Parameters:

Name Description Required
zipFilePath Path to the zip file Yes
outputPath Path where the unzipped file(s) should go Yes

Full UDF Source:

/**
 * Unzips a file to the specified directory.
 * 
 * @param zipFilePath      Path to the zip file (Required)
 * @param outputPath      Path where the unzipped file(s) should go (Required)
 * @return void 
 * @author Samuel Neff (sam@serndesign.com) 
 * @version 1, September 1, 2003 
 */
function unzipFile(zipFilePath, outputPath) {
    var zipFile = ""; // ZipFile
    var entries = ""; // Enumeration of ZipEntry
    var entry = ""; // ZipEntry
    var fil = ""; //File
    var inStream = "";
    var filOutStream = "";
    var bufOutStream = "";
    var nm = "";
    var pth = "";
    var lenPth = "";
    var buffer = "";
    var l = 0;
     
    zipFile = createObject("java", "java.util.zip.ZipFile");
    zipFile.init(zipFilePath);
    
    entries = zipFile.entries();
    
    while(entries.hasMoreElements()) {
        entry = entries.nextElement();
        if(NOT entry.isDirectory()) {
            nm = entry.getName(); 
            
            lenPth = len(nm) - len(getFileFromPath(nm));
            
            if (lenPth) {
            pth = outputPath & left(nm, lenPth);
        } else {
            pth = outputPath;
        }
        if (NOT directoryExists(pth)) {
            fil = createObject("java", "java.io.File");
            fil.init(pth);
            fil.mkdirs();
        }
        filOutStream = createObject(
            "java", 
            "java.io.FileOutputStream");
        
        filOutStream.init(outputPath & nm);
        
        bufOutStream = createObject(
            "java", 
            "java.io.BufferedOutputStream");
        
        bufOutStream.init(filOutStream);
        
        inStream = zipFile.getInputStream(entry);
        buffer = repeatString(" ",1024).getBytes(); 
        
        l = inStream.read(buffer);
        while(l GTE 0) {
            bufOutStream.write(buffer, 0, l);
            l = inStream.read(buffer);
        }
        inStream.close();
        bufOutStream.close();
        }
    }
    zipFile.close();
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson