ColdFusion UDF Library - jZip
December 16th, 2008
«« Back to ColdFusion Library
Category:
SYSTEM
Description:
A Java based CFML UDF to manipulate zip files.
Parameters:
Returns:
This function returns differently depending on the mode of operation. z (zip) - If successful, returns true. Else it will return an error message. e (extract) - If successful, returns true. Else it will return an error message. l (list) - If successful, returns a query of the files contained in the archive. Else it will return an error message.
Requires:
getFiles(), getDriveFromPath()
Sample Use:
Copy to Clipboard
Category:
SYSTEM
Description:
A Java based CFML UDF to manipulate zip files.
Parameters:
- zipfile (String, required)
The zip file to operate on. This can be a full path or relative. - path (String, Default: )
The filename or path to zip. This can be a full path or relative. If blank then the zipfile will be extracted to a directory named after the zip file, under the current directory. - flags (String, Default: )
zip flags
Values:- z - Create a zip file from the path specified. Default flag.
- e - Extract files from the zip.
- l - List all files in the zip.
- cn - Compression level. Where n is a number from 0-9. 0 being no compression
- 9 being maximum compression. Default is 9.
- rn - Recursion. Sets the recursion level (how many sub directories deep) will be compressed. 0 is none (Default); -1 is all.
- a - Absolute paths (instead of the default relative paths).
- v - Return a variable containing the zip contents; instead of writing the zip to a file. In this case
- the first parameter (zipfile) must be a variable to set in the calling page.
- d - Delete files after creating the zip.
- z - Create a zip file from the path specified. Default flag.
Returns:
This function returns differently depending on the mode of operation. z (zip) - If successful, returns true. Else it will return an error message. e (extract) - If successful, returns true. Else it will return an error message. l (list) - If successful, returns a query of the files contained in the archive. Else it will return an error message.
Requires:
getFiles(), getDriveFromPath()
Sample Use:
Copy to Clipboard
/* #depends:getFiles,getDriveFromPath# */
function jZip(zipfile){
var flags = "zc9";
var action = "zip";
var compression = 9;
var recursion = 0;
var relative = true;
var input = "";
var output = "";
var fileStream = "";
var insert = "";
var byteArray = repeatString(chr(32),1024).getBytes();
var len = 0;
var filename = "";
var files = arrayNew(1);
var value = false;
var loop = 0;
var temp = "";
var returnMode = "file";
if (arrayLen(arguments) eq 1){
flags = "e";
path = expandPath("./") & reverse(listRest(reverse(getFileFromPath(zipfile)),".")) & "\";
}
else{
if (arrayLen(arguments) gte 2)path = arguments[2];
if (arrayLen(arguments) gte 3)flags = arguments[3];
else{
if (not len(server.getDriveFromPath(path)))flags = path;
}
};
if (findNoCase("z",flags))action = "zip";
if (findNoCase("e",flags))action = "extract";
if (findNoCase("l",flags))action = "list";
if (reFindNoCase("c[[:digit:]]",flags))compression = mid(flags,reFindNoCase("c[[:digit:]]",flags)+1,1);
if (reFindNoCase("r-*[[:digit:]]+",flags)){
temp = reFindNoCase("r(-*[[:digit:]]+)",flags,1,true);
recursion = mid(flags,temp.pos[2],temp.len[2]);
};
if (findNoCase("a",flags))relative = false;
if (findNoCase("v",flags)){
returnMode = "variable";
if (not isSimpleValue(path)){
value = "Returning variable, but parameter zipfile is not a simple value. This parameter must be a string.";
action = "error";
};
};
switch (action){
case "zip":
if (not len(server.getDriveFromPath(zipfile)) and returnMode eq "file")zipfile = expandPath(zipfile);
if (isSimpleValue(path)){
if (not len(server.getDriveFromPath(path)))path = expandPath(path);
files = server.getFiles(path,"",2,recursion);
files = listToArray(valueList(files.fullPath,"?"),"?");
}
else{
if (isQuery(path) and listFindNoCase(path.columnList,"directory") and listFindNoCase(path.columnList,"name")){
for (loop = 1; loop lte path.recordCount; loop = incrementValue(loop))files[arrayLen(files)+1] = "#path.directory[loop]#\#path.name[loop]#";
}
if (isArray(path)){
for (loop = 1; loop lte arrayLen(path); loop = incrementValue(loop)){
if (not len(server.getDriveFromPath(path[loop])))files[arrayLen(files)+1] = expandPath(path[loop]);
else files[arrayLen(files)+1] = path;
}
}
}
if (isArray(files)){
if (returnMode eq "file")fileStream = createObject("java","java.io.FileOutputStream").init(zipfile);
else fileStream = createObject("java","java.io.ByteArrayOutputStream").init();
output = createObject("java","java.util.zip.ZipOutputStream").init(fileStream);
output.setLevel(compression);
for (loop = 1; loop lte arrayLen(files); loop = incrementValue(loop)){
input = createObject("java","java.io.FileInputStream").init(files[loop]);
if (relative)filename = replaceNoCase(files[loop],getDirectoryFromPath(path),"","ONE");
else filename = files[loop];
insert = createObject("java","java.util.zip.ZipEntry").init(filename);
insert.setComment("Packed by jZip v1.0");
output.putNextEntry(insert);
len = input.read(byteArray);
while (len gt 0){
output.write(byteArray,0,len);
len = input.read(byteArray);
}
output.closeEntry();
input.close();
};
output.close();
if (returnMode eq "variable"){
fileStream.flush();
setVariable(zipfile,toBinary(toBase64(fileStream.toString("ISO-8859-1"),"ISO-8859-1")));
}
fileStream.close();
value = true;
}
else{
value = "Path must either be: A full or relative path. A query (returned by cfdirectory). Or an array of filenames.";
}
break;
case "extract":
if (not len(server.getDriveFromPath(zipfile)))zipfile = expandPath(zipfile);
if (not len(server.getDriveFromPath(path)))path = expandPath(path);
input = createObject("java","java.util.zip.ZipFile").init(zipfile);
temp = input.entries();
while (temp.hasMoreElements()){
handle = temp.nextElement();
fileStream = input.getInputStream(handle);
insert = createObject("java","java.io.File").init(getDirectoryFromPath("#path##handle.getName()#"));
insert.mkdirs();
output = createObject("java","java.io.FileOutputStream").init("#path##handle.getName()#");
len = fileStream.read(byteArray);
while (len gt 0){
output.write(byteArray,0,len);
len = fileStream.read(byteArray);
}
output.close();
fileStream.close();
};
input.close();
value = true;
break;
case "list":
if (not len(server.getDriveFromPath(zipfile)))zipfile = expandPath(zipfile);
input = createObject("java","java.util.zip.ZipFile").init(zipfile);
output = input.entries();
value = queryNew("name,size,compressed,comment,method,lastModified,hash,isDirectory,isFile,crc");
while (output.hasMoreElements()){
handle = output.nextElement();
queryAddRow(value);
querySetCell(value,"name",handle.getName());
querySetCell(value,"size",handle.getSize());
querySetCell(value,"compressed",handle.getCompressedSize());
querySetCell(value,"comment",handle.getComment());
querySetCell(value,"method",handle.getMethod());
temp = createObject("java","java.util.Date").init(handle.getTime());
querySetCell(value,"lastModified",temp);
querySetCell(value,"hash",handle.hashCode());
querySetCell(value,"isDirectory",yesNoFormat(handle.isDirectory()));
querySetCell(value,"isFile",yesNoFormat(not handle.isDirectory()));
querySetCell(value,"crc",handle.getCrc());
}
input.close();
break;
case "error":
break;
default:
value = "No action specified.";
break;
}
return value;
};
// http://www.programmers.org/index.php/coldfusion-udf-library/?ckey=92&function=jZip



