CFLib.org – Common Function Library Project

unzipFile(zipFilePath, outputPath)

Last updated September 1, 2003

Version: 1 | Requires: ColdFusion MX | Library: FileSysLib

 
Rated 13 time(s). Average Rating: 4.2

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:

view plain print about
<!--- 

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:

view plain print about
<cfscript>
/**
 * 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();
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Adam Cameron Adam Cameron added
createPrimeNumbe...
14 day(s) ago

Ray Ford Ray Ford added
timeZoneNow
29 day(s) ago

Henry Ho Henry Ho added
queryExecute
a while ago

Rick Root Rick Root added
deleteDirectory
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 48 time(s)

Barney Boisvert indentXml
Rated 5.0, 12 time(s)

Rachel Lehman deAccent
Rated 5.0, 9 time(s)

Darwan Leonardo Sitepu splitNumber
Rated 5.0, 8 time(s)

Created by Raymond Camden / Design by Justin Johnson