unzipFile(zipFilePath, outputPath)
Last updated September 1, 2003
Version: 1 | Requires: ColdFusion MX | 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:
<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>
Search CFLib.org
Latest Additions
Robby Lansaw added
getComponentProp...
3 day(s) ago
Dave Anderson added
iniToStruct
25 day(s) ago
Dave Anderson added
deDupeArray
25 day(s) ago
Richard added
dice
27 day(s) ago
Top Rated
backupDatabase
Rated 5.0, 22 time(s)
indentXml
Rated 5.0, 10 time(s)
generateSsccAsn
Rated 5.0, 4 time(s)
highlightAndCrop
Rated 5.0, 4 time(s)