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
Adam Cameron added
composeDateTime
20 day(s) ago
Chris Weller added
convertQueryStri...
a while ago
Greg Nettles added
arrayDiff
a while ago
Nathan Dintenfass added
ArrayOfStructsSo...
a while ago
Top Rated
backupDatabase
Rated 5.0, 36 time(s)
indentXml
Rated 5.0, 10 time(s)
deAccent
Rated 5.0, 6 time(s)
countArbitraryDa...
Rated 5.0, 5 time(s)