| 1 | <!--- |
|---|
| 2 | ******************************************************************************** |
|---|
| 3 | Copyright 2005-2008 ColdBox Framework by Luis Majano and Ortus Solutions, Corp |
|---|
| 4 | www.coldboxframework.com | www.luismajano.com | www.ortussolutions.com |
|---|
| 5 | ******************************************************************************** |
|---|
| 6 | |
|---|
| 7 | * Application: newsight Zip Component |
|---|
| 8 | * File Name: Zip.cfc |
|---|
| 9 | * CFC Component Name: Zip |
|---|
| 10 | * Support: ColdFusion MX 6.0, ColdFusion MX 6.1, ColdFusion MX 7 |
|---|
| 11 | * Created By: Artur Kordowski - info@newsight.de |
|---|
| 12 | * Created: 16.06.2005 |
|---|
| 13 | * Description: A collections of functions that supports the Zip and GZip functionality by using the |
|---|
| 14 | * Java Zip file API. |
|---|
| 15 | * |
|---|
| 16 | * Version History: [dd.mm.yyyy] [Version] [Author] [Comments] |
|---|
| 17 | * 16.06.2005 0.1 Beta A. Kordowski Beta status reached. |
|---|
| 18 | * 27.06.2005 1.0 A. Kordowski Component complete. |
|---|
| 19 | * 07.08.2005 1.1 A. Kordowski Fixed some bugs. Add GZip functionality. New functions |
|---|
| 20 | * gzipAddFile() and gzipExtract(). |
|---|
| 21 | * 02.10.2005 1.2 A.Kordowski Fixed bug for ColdFusion MX 6. |
|---|
| 22 | * |
|---|
| 23 | * Comments: [dd.mm.yyyy] [Version] [Author] [Comments] |
|---|
| 24 | * 27.06.2005 0.1 Beta A. Kordowski Thanks a lot to Warren Sung for testing the Component with |
|---|
| 25 | * ColdFusion MX 6.1 on Linux Debian 3.1. |
|---|
| 26 | * 27.06.2005 0.1 Beta A. Kordowski Component tested with ColdFusion MX 7 on Windows XP Professional. |
|---|
| 27 | * 29.06.2005 1.0 A. Kordowski Created documentation. |
|---|
| 28 | * 01.07.2005 1.0 A. Kordowski Release component. |
|---|
| 29 | * 08.08.2005 1.1 A. Kordowski Update documentation. |
|---|
| 30 | * |
|---|
| 31 | * Docs: http://livedocs.newsight.de/com/Zip/ |
|---|
| 32 | * |
|---|
| 33 | * Notice: For comments, bug reports or suggestions to optimise this component, feel free to send |
|---|
| 34 | * a E-Mail: info@newsight.de |
|---|
| 35 | * |
|---|
| 36 | * License: THIS IS A OPEN SOURCE COMPONENT. YOU ARE FREE TO USE THIS COMPONENT IN ANY APPLICATION, |
|---|
| 37 | * TO COPY IT OR MODIFY THE FUNCTIONS FOR YOUR OWN NEEDS, AS LONG THIS HEADER INFORMATION |
|---|
| 38 | * REMAINS IN TACT AND YOU DON'T CHARGE ANY MONEY FOR IT. USE THIS COMPONENT AT YOUR OWN |
|---|
| 39 | * RISK. NO WARRANTY IS EXPRESSED OR IMPLIED, AND NO LIABILITY ASSUMED FOR THE RESULT OF |
|---|
| 40 | * USING THIS COMPONENT. |
|---|
| 41 | * |
|---|
| 42 | * THIS COMPONENT IS LICENSED UNDER THE CREATIVE COMMONS ATTRIBUTION-SHAREALIKE LICENSE. |
|---|
| 43 | * FOR THE FULL LICENSE TEXT PLEASE VISIT: http://creativecommons.org/licenses/by-sa/2.5/ |
|---|
| 44 | * |
|---|
| 45 | ************************************************************************************************ |
|---|
| 46 | |
|---|
| 47 | Author : Luis Majano |
|---|
| 48 | Date : September 23, 2005 |
|---|
| 49 | Description : |
|---|
| 50 | Converted this cfc into a ColdBox plugin. |
|---|
| 51 | |
|---|
| 52 | Modification History: |
|---|
| 53 | 08/01/2006 - Updated the cfc to work for ColdBox. |
|---|
| 54 | ---> |
|---|
| 55 | <cfcomponent name="zip" |
|---|
| 56 | hint = "A collections of functions that supports the Zip and GZip functionality by using the Java Zip file API." |
|---|
| 57 | extends="coldbox.system.plugin" |
|---|
| 58 | output="false" |
|---|
| 59 | cache="false"> |
|---|
| 60 | |
|---|
| 61 | <!------------------------------------------- CONSTRUCTOR -------------------------------------------> |
|---|
| 62 | |
|---|
| 63 | <cffunction name="init" access="public" returntype="zip" output="false"> |
|---|
| 64 | <cfargument name="controller" type="any" required="true"> |
|---|
| 65 | <cfset super.Init(arguments.controller) /> |
|---|
| 66 | <cfscript> |
|---|
| 67 | //Local Plugin Definition |
|---|
| 68 | setpluginName("Zip Plugin"); |
|---|
| 69 | setpluginVersion("1.0"); |
|---|
| 70 | setpluginDescription("This is a zip utility for the framework."); |
|---|
| 71 | //This plugin's properties |
|---|
| 72 | instance.ioFile = CreateObject("java","java.io.File"); |
|---|
| 73 | instance.ioInput = CreateObject("java","java.io.FileInputStream"); |
|---|
| 74 | instance.ioOutput = CreateObject("java","java.io.FileOutputStream"); |
|---|
| 75 | instance.ioBufOutput = CreateObject("java","java.io.BufferedOutputStream"); |
|---|
| 76 | instance.zipFile = CreateObject("java","java.util.zip.ZipFile"); |
|---|
| 77 | instance.zipEntry = CreateObject("java","java.util.zip.ZipEntry"); |
|---|
| 78 | instance.zipInput = CreateObject("java","java.util.zip.ZipInputStream"); |
|---|
| 79 | instance.zipOutput = CreateObject("java","java.util.zip.ZipOutputStream"); |
|---|
| 80 | instance.gzInput = CreateObject("java","java.util.zip.GZIPInputStream"); |
|---|
| 81 | instance.gzOutput = CreateObject("java","java.util.zip.GZIPOutputStream"); |
|---|
| 82 | instance.objDate = CreateObject("java","java.util.Date"); |
|---|
| 83 | |
|---|
| 84 | /* Set Localized Variables */ |
|---|
| 85 | instance.os = Server.OS.Name; |
|---|
| 86 | instance.slash = createObject("java","java.lang.System").getProperty("file.separator"); |
|---|
| 87 | |
|---|
| 88 | //LM. To fix Overflow. |
|---|
| 89 | instance.filename = ""; |
|---|
| 90 | |
|---|
| 91 | //Return instance |
|---|
| 92 | return this; |
|---|
| 93 | </cfscript> |
|---|
| 94 | </cffunction> |
|---|
| 95 | |
|---|
| 96 | <!------------------------------------------- PUBLIC -------------------------------------------> |
|---|
| 97 | |
|---|
| 98 | <cffunction name="AddFiles" access="public" output="no" returntype="boolean" hint="Add files to a new or an existing Zip file archive."> |
|---|
| 99 | <!--- ************************************************************* ---> |
|---|
| 100 | <cfargument name="zipFilePath" required="yes" type="string" hint="Pathname of the Zip file to add files."> |
|---|
| 101 | <cfargument name="files" required="no" type="string" default="" hint="| (Chr(124)) delimited list of files to add to the Zip file. Required if argument 'directory' is not set."> |
|---|
| 102 | <cfargument name="directory" required="no" type="string" default="" hint="Absolute pathname of directory to add to the Zip file. Required if argument 'files' is not set."> |
|---|
| 103 | <cfargument name="filter" required="no" type="string" default="" hint="File extension filter. One filter can be applied. Only if argument 'directory' is set."> |
|---|
| 104 | <cfargument name="recurse" required="no" type="boolean" default="no" hint="Get recursive files of subdirectories. Only if argument 'directory' is set."> |
|---|
| 105 | <cfargument name="compression" required="no" type="numeric" default="9" hint="Compression level (0 through 9, 0=minimum, 9=maximum)."> |
|---|
| 106 | <cfargument name="savePaths" required="no" type="boolean" default="no" hint="Save full path info."> |
|---|
| 107 | <!--- ************************************************************* ---> |
|---|
| 108 | <cfscript> |
|---|
| 109 | /* Default variables */ |
|---|
| 110 | var i = 0; |
|---|
| 111 | var l = 0; |
|---|
| 112 | var buffer = RepeatString(" ",1024).getBytes(); |
|---|
| 113 | var entryPath = ""; |
|---|
| 114 | var entryFile = ""; |
|---|
| 115 | var localfiles = ""; |
|---|
| 116 | var path = ""; |
|---|
| 117 | var skip = ""; |
|---|
| 118 | |
|---|
| 119 | try{ |
|---|
| 120 | /* Initialize Zip file */ |
|---|
| 121 | instance.ioOutput.init(PathFormat(arguments.zipFilePath)); |
|---|
| 122 | instance.filename = getFileFromPath(arguments.zipFilePath); |
|---|
| 123 | instance.zipOutput.init(instance.ioOutput); |
|---|
| 124 | instance.zipOutput.setLevel(arguments.compression); |
|---|
| 125 | |
|---|
| 126 | /* Get files list array */ |
|---|
| 127 | if( structKeyExists(arguments, "files") and arguments.files neq "") |
|---|
| 128 | localfiles = ListToArray(PathFormat(arguments.files), "|"); |
|---|
| 129 | else if( structKeyExists(arguments,"directory") and arguments.directory neq ""){ |
|---|
| 130 | localfiles = FilesList(arguments.directory, arguments.filter, arguments.recurse); |
|---|
| 131 | arguments.directory = PathFormat(arguments.directory); |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | /* Loop over files array */ |
|---|
| 135 | for(i=1; i LTE ArrayLen(localfiles); i=i+1){ |
|---|
| 136 | if(FileExists(localfiles[i])){ |
|---|
| 137 | path = localfiles[i]; |
|---|
| 138 | |
|---|
| 139 | // Get entry path and file |
|---|
| 140 | entryPath = GetDirectoryFromPath(path); |
|---|
| 141 | entryFile = GetFileFromPath(path); |
|---|
| 142 | |
|---|
| 143 | // Remove drive letter from path |
|---|
| 144 | if(arguments.savePaths EQ "yes" AND Right(ListFirst(entryPath, instance.slash), 1) EQ ":") |
|---|
| 145 | entryPath = ListDeleteAt(entryPath, 1, instance.slash); |
|---|
| 146 | // Remove directory from path |
|---|
| 147 | else if(arguments.savePaths EQ "no"){ |
|---|
| 148 | if( structKeyExists(arguments, "directory") and arguments.directory neq "" ) |
|---|
| 149 | entryPath = ReplaceNoCase(entryPath, arguments.directory, "", "ALL"); |
|---|
| 150 | else if(structKeyExists(arguments, "files") and arguments.files neq "") |
|---|
| 151 | entryPath = ""; |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | // Remove slash at first |
|---|
| 155 | if(Len(entryPath) GT 1 AND Left(entryPath, 1) EQ instance.slash) entryPath = Right(entryPath, Len(entryPath)-1); |
|---|
| 156 | else if(Len(entryPath) EQ 1 AND Left(entryPath, 1) EQ instance.slash) entryPath = "" ; |
|---|
| 157 | |
|---|
| 158 | // Skip if entry with the same name already exsits |
|---|
| 159 | try { |
|---|
| 160 | instance.ioFile.init(path); |
|---|
| 161 | instance.ioInput.init(instance.ioFile.getPath()); |
|---|
| 162 | |
|---|
| 163 | instance.zipEntry.init(entryPath & entryFile); |
|---|
| 164 | instance.zipOutput.putNextEntry(instance.zipEntry); |
|---|
| 165 | |
|---|
| 166 | l = instance.ioInput.read(buffer); |
|---|
| 167 | |
|---|
| 168 | while(l GT 0){ |
|---|
| 169 | instance.zipOutput.write(buffer, 0, l); |
|---|
| 170 | l = instance.ioInput.read(buffer); |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | instance.zipOutput.closeEntry(); |
|---|
| 174 | instance.ioInput.close(); |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | catch(java.util.zip.ZipException ex) |
|---|
| 178 | { skip = "yes"; } |
|---|
| 179 | } |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | /* Close Zip file */ |
|---|
| 183 | instance.zipOutput.close(); |
|---|
| 184 | |
|---|
| 185 | /* Return true */ |
|---|
| 186 | return true; |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | catch(Any expr) |
|---|
| 190 | { |
|---|
| 191 | /* Close Zip file */ |
|---|
| 192 | instance.zipOutput.close(); |
|---|
| 193 | |
|---|
| 194 | /* Return false */ |
|---|
| 195 | return false; |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | </cfscript> |
|---|
| 199 | |
|---|
| 200 | </cffunction> |
|---|
| 201 | |
|---|
| 202 | <!--- ************************************************************* ---> |
|---|
| 203 | |
|---|
| 204 | <cffunction name="DeleteFiles" access="public" output="no" returntype="boolean" hint="Delete files from an existing Zip file archive."> |
|---|
| 205 | <!--- ************************************************************* ---> |
|---|
| 206 | <cfargument name="zipFilePath" required="yes" type="string" hint="Pathname of the Zip file to delete files from."> |
|---|
| 207 | <cfargument name="files" required="yes" type="string" hint="| (Chr(124)) delimited list of files to delete from Zip file."> |
|---|
| 208 | <!--- ************************************************************* ---> |
|---|
| 209 | <cfscript> |
|---|
| 210 | |
|---|
| 211 | /* NOTICE: There is no function in the Java API to delete entrys from a Zip file. |
|---|
| 212 | So we have to create a workaround for this function. At first we create |
|---|
| 213 | a new temporary Zip file and save there all entrys, excluded the delete |
|---|
| 214 | files. Then we delete the orginal Zip file and rename the temporary Zip |
|---|
| 215 | file. */ |
|---|
| 216 | |
|---|
| 217 | /* Default variables */ |
|---|
| 218 | var l = 0; |
|---|
| 219 | var buffer = RepeatString(" ",1024).getBytes(); |
|---|
| 220 | var entries = ""; |
|---|
| 221 | var entry = ""; |
|---|
| 222 | var inStream = ""; |
|---|
| 223 | var zipTemp = ""; |
|---|
| 224 | var zipRename = ""; |
|---|
| 225 | /* Convert to the right path format */ |
|---|
| 226 | arguments.zipFilePath = PathFormat(arguments.zipFilePath); |
|---|
| 227 | |
|---|
| 228 | try{ |
|---|
| 229 | /* Open Zip file and get Zip file entries */ |
|---|
| 230 | instance.zipFile.init(arguments.zipFilePath); |
|---|
| 231 | entries = instance.zipFile.entries(); |
|---|
| 232 | |
|---|
| 233 | /* Create a new temporary Zip file */ |
|---|
| 234 | instance.ioOutput.init(PathFormat(arguments.zipFilePath & ".temp")); |
|---|
| 235 | instance.zipOutput.init(instance.ioOutput); |
|---|
| 236 | |
|---|
| 237 | /* Loop over Zip file entries */ |
|---|
| 238 | while(entries.hasMoreElements()){ |
|---|
| 239 | entry = entries.nextElement(); |
|---|
| 240 | |
|---|
| 241 | if(NOT entry.isDirectory()){ |
|---|
| 242 | /* Create a new entry in the temporary Zip file */ |
|---|
| 243 | if(NOT ListFindNoCase(arguments.files, entry.getName(), "|")){ |
|---|
| 244 | // Set entry compression |
|---|
| 245 | instance.zipOutput.setLevel(entry.getMethod()); |
|---|
| 246 | |
|---|
| 247 | // Create new entry in the temporary Zip file |
|---|
| 248 | instance.zipEntry.init(entry.getName()); |
|---|
| 249 | instance.zipOutput.putNextEntry(instance.zipEntry); |
|---|
| 250 | |
|---|
| 251 | inStream = instance.zipFile.getInputStream(entry); |
|---|
| 252 | l = inStream.read(buffer); |
|---|
| 253 | |
|---|
| 254 | while(l GT 0){ |
|---|
| 255 | instance.zipOutput.write(buffer, 0, l); |
|---|
| 256 | l = inStream.read(buffer); |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | // Close entry |
|---|
| 260 | instance.zipOutput.closeEntry(); |
|---|
| 261 | } |
|---|
| 262 | } |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | /* Close the orginal Zip and the temporary Zip file */ |
|---|
| 266 | instance.zipFile.close(); |
|---|
| 267 | instance.zipOutput.close(); |
|---|
| 268 | |
|---|
| 269 | /* Delete the orginal Zip file */ |
|---|
| 270 | instance.ioFile.init(arguments.zipFilePath).delete(); |
|---|
| 271 | |
|---|
| 272 | /* Rename the temporary Zip file */ |
|---|
| 273 | zipTemp = instance.ioFile.init(arguments.zipFilePath & ".temp"); |
|---|
| 274 | zipRename = instance.ioFile.init(arguments.zipFilePath); |
|---|
| 275 | zipTemp.renameTo(zipRename); |
|---|
| 276 | |
|---|
| 277 | /* Return true */ |
|---|
| 278 | return true; |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | catch(Any expr) |
|---|
| 282 | { |
|---|
| 283 | /* Close the orginal Zip and the temporary Zip file */ |
|---|
| 284 | instance.zipOutput.close(); |
|---|
| 285 | instance.zipFile.close(); |
|---|
| 286 | |
|---|
| 287 | /* Delete the temporary Zip file, if exists */ |
|---|
| 288 | if(FileExists(arguments.zipFilePath & ".temp")) |
|---|
| 289 | instance.ioFile.init(arguments.zipFilePath & ".temp").delete(); |
|---|
| 290 | |
|---|
| 291 | /* Return false */ |
|---|
| 292 | return false; |
|---|
| 293 | } |
|---|
| 294 | |
|---|
| 295 | </cfscript> |
|---|
| 296 | </cffunction> |
|---|
| 297 | |
|---|
| 298 | <!--- ************************************************************* ---> |
|---|
| 299 | |
|---|
| 300 | <cffunction name="Extract" access="public" output="no" returntype="boolean" hint="Extracts a specified Zip file into a specified directory."> |
|---|
| 301 | <!--- ************************************************************* ---> |
|---|
| 302 | <cfargument name="zipFilePath" required="yes" type="string" hint="Pathname of the Zip file to extract."> |
|---|
| 303 | <cfargument name="extractPath" required="no" type="string" default="#ExpandPath(".")#" hint="Pathname to extract the Zip file to."> |
|---|
| 304 | <cfargument name="extractFiles" required="no" type="string" hint="| (Chr(124)) delimited list of files to extract."> |
|---|
| 305 | <cfargument name="useFolderNames" required="no" type="boolean" default="yes" hint="Create folders using the pathinfo stored in the Zip file."> |
|---|
| 306 | <cfargument name="overwriteFiles" required="no" type="boolean" default="no" hint="Overwrite existing files."> |
|---|
| 307 | <!--- ************************************************************* ---> |
|---|
| 308 | <cfscript> |
|---|
| 309 | |
|---|
| 310 | /* Default variables */ |
|---|
| 311 | var l = 0; |
|---|
| 312 | var entries = ""; |
|---|
| 313 | var entry = ""; |
|---|
| 314 | var name = ""; |
|---|
| 315 | var path = ""; |
|---|
| 316 | var filePath = ""; |
|---|
| 317 | var buffer = RepeatString(" ",1024).getBytes(); |
|---|
| 318 | var lastChr = ""; |
|---|
| 319 | var lenPath = ""; |
|---|
| 320 | var inStream = ""; |
|---|
| 321 | var skip = ""; |
|---|
| 322 | |
|---|
| 323 | /* Convert to the right path format */ |
|---|
| 324 | arguments.zipFilePath = PathFormat(arguments.zipFilePath); |
|---|
| 325 | arguments.extractPath = PathFormat(arguments.extractPath); |
|---|
| 326 | |
|---|
| 327 | /* Check if the 'extractPath' string is closed */ |
|---|
| 328 | lastChr = Right(arguments.extractPath, 1); |
|---|
| 329 | |
|---|
| 330 | /* Set an slash at the end of string */ |
|---|
| 331 | if(lastChr NEQ instance.slash) |
|---|
| 332 | arguments.extractPath = arguments.extractPath & instance.slash; |
|---|
| 333 | |
|---|
| 334 | try{ |
|---|
| 335 | /* Open Zip file */ |
|---|
| 336 | instance.zipFile.init(arguments.zipFilePath); |
|---|
| 337 | |
|---|
| 338 | /* Zip file entries */ |
|---|
| 339 | entries = instance.zipFile.entries(); |
|---|
| 340 | |
|---|
| 341 | /* Loop over Zip file entries */ |
|---|
| 342 | while(entries.hasMoreElements()){ |
|---|
| 343 | entry = entries.nextElement(); |
|---|
| 344 | |
|---|
| 345 | if(NOT entry.isDirectory()){ |
|---|
| 346 | name = entry.getName(); |
|---|
| 347 | |
|---|
| 348 | /* Create directory only if 'useFolderNames' is 'yes' */ |
|---|
| 349 | if(arguments.useFolderNames EQ "yes"){ |
|---|
| 350 | lenPath = Len(name) - Len(GetFileFromPath(name)); |
|---|
| 351 | |
|---|
| 352 | if(lenPath) path = extractPath & Left(name, lenPath); |
|---|
| 353 | else path = extractPath; |
|---|
| 354 | |
|---|
| 355 | if(NOT DirectoryExists(path)){ |
|---|
| 356 | instance.ioFile.init(path); |
|---|
| 357 | instance.ioFile.mkdirs(); |
|---|
| 358 | } |
|---|
| 359 | } |
|---|
| 360 | |
|---|
| 361 | /* Set file path */ |
|---|
| 362 | if(arguments.useFolderNames EQ "yes") filePath = arguments.extractPath & name; |
|---|
| 363 | else filePath = arguments.extractPath & GetFileFromPath(name); |
|---|
| 364 | |
|---|
| 365 | /* Extract files. Files would be extract when following conditions are fulfilled: |
|---|
| 366 | If the 'extractFiles' list is not defined, |
|---|
| 367 | or the 'extractFiles' list is defined and the entry filename is found in the list, |
|---|
| 368 | or the file already exists and 'overwriteFiles' is 'yes'. */ |
|---|
| 369 | if((NOT structKeyExists(arguments, "extractFiles") |
|---|
| 370 | OR (structKeyExists(arguments, "extractFiles") AND ListFindNoCase(arguments.extractFiles, GetFileFromPath(name), "|"))) |
|---|
| 371 | AND (NOT FileExists(filePath) OR (FileExists(filePath) AND arguments.overwriteFiles EQ "yes"))) |
|---|
| 372 | { |
|---|
| 373 | // Skip if entry contains special characters |
|---|
| 374 | try{ |
|---|
| 375 | instance.ioOutput.init(filePath); |
|---|
| 376 | instance.ioBufOutput.init(instance.ioOutput); |
|---|
| 377 | |
|---|
| 378 | inStream = instance.zipFile.getInputStream(entry); |
|---|
| 379 | l = inStream.read(buffer); |
|---|
| 380 | |
|---|
| 381 | while(l GTE 0){ |
|---|
| 382 | instance.ioBufOutput.write(buffer, 0, l); |
|---|
| 383 | l = inStream.read(buffer); |
|---|
| 384 | } |
|---|
| 385 | |
|---|
| 386 | inStream.close(); |
|---|
| 387 | instance.ioBufOutput.close(); |
|---|
| 388 | instance.ioOutput.close(); |
|---|
| 389 | } |
|---|
| 390 | |
|---|
| 391 | catch(Any Expr) |
|---|
| 392 | { skip = "yes"; } |
|---|
| 393 | } |
|---|
| 394 | } |
|---|
| 395 | } |
|---|
| 396 | |
|---|
| 397 | /* Close the Zip file */ |
|---|
| 398 | instance.zipFile.close(); |
|---|
| 399 | |
|---|
| 400 | /* Return true */ |
|---|
| 401 | return true; |
|---|
| 402 | } |
|---|
| 403 | |
|---|
| 404 | catch(Any expr){ |
|---|
| 405 | /* Close the Zip file */ |
|---|
| 406 | instance.zipFile.close(); |
|---|
| 407 | |
|---|
| 408 | /* Return false */ |
|---|
| 409 | return false; |
|---|
| 410 | } |
|---|
| 411 | |
|---|
| 412 | </cfscript> |
|---|
| 413 | </cffunction> |
|---|
| 414 | |
|---|
| 415 | <!--- ************************************************************* ---> |
|---|
| 416 | |
|---|
| 417 | <cffunction name="List" access="public" output="no" returntype="query" hint="List the content of a specified Zip file."> |
|---|
| 418 | <!--- ************************************************************* ---> |
|---|
| 419 | <cfargument name="zipFilePath" required="yes" type="string" hint="Pathname of the Zip file to list the content."> |
|---|
| 420 | <!--- ************************************************************* ---> |
|---|
| 421 | <cfscript> |
|---|
| 422 | /* Default variables */ |
|---|
| 423 | var i = 0; |
|---|
| 424 | var entries = ""; |
|---|
| 425 | var entry = ""; |
|---|
| 426 | var cols = "entry,date,size,packed,ratio,crc"; |
|---|
| 427 | var query = QueryNew(cols); |
|---|
| 428 | var qEntry = ""; |
|---|
| 429 | var qDate = ""; |
|---|
| 430 | var qSize = ""; |
|---|
| 431 | var qPacked = ""; |
|---|
| 432 | var qCrc = ""; |
|---|
| 433 | var qRatio = ""; |
|---|
| 434 | |
|---|
| 435 | cols = ListToArray(cols); |
|---|
| 436 | |
|---|
| 437 | /* Open Zip file */ |
|---|
| 438 | instance.zipFile.init(arguments.zipFilePath); |
|---|
| 439 | |
|---|
| 440 | /* Zip file entries */ |
|---|
| 441 | entries = instance.zipFile.entries(); |
|---|
| 442 | |
|---|
| 443 | /* Fill query with data */ |
|---|
| 444 | while(entries.hasMoreElements()){ |
|---|
| 445 | entry = entries.nextElement(); |
|---|
| 446 | |
|---|
| 447 | if(NOT entry.isDirectory()){ |
|---|
| 448 | QueryAddRow(query, 1); |
|---|
| 449 | |
|---|
| 450 | qEntry = PathFormat(entry.getName()); |
|---|
| 451 | qDate = instance.objDate.init(entry.getTime()); |
|---|
| 452 | qSize = entry.getSize(); |
|---|
| 453 | qPacked = entry.getCompressedSize(); |
|---|
| 454 | qCrc = entry.getCrc(); |
|---|
| 455 | |
|---|
| 456 | if(qSize GT 0) qRatio = Round(Evaluate(100-((qPacked*100)/qSize))) & "%"; |
|---|
| 457 | else qRatio = "0%"; |
|---|
| 458 | |
|---|
| 459 | for(i=1; i LTE ArrayLen(cols); i=i+1) |
|---|
| 460 | QuerySetCell(query, cols[i], Trim(Evaluate("q#cols[i]#"))); |
|---|
| 461 | } |
|---|
| 462 | } |
|---|
| 463 | |
|---|
| 464 | /* Close the Zip File */ |
|---|
| 465 | instance.zipFile.close(); |
|---|
| 466 | |
|---|
| 467 | /* Return query */ |
|---|
| 468 | return query; |
|---|
| 469 | </cfscript> |
|---|
| 470 | </cffunction> |
|---|
| 471 | |
|---|
| 472 | <!--- ************************************************************* ---> |
|---|
| 473 | |
|---|
| 474 | <cffunction name="gzipAddFile" access="public" output="no" returntype="boolean" hint="Create a new GZip file archive."> |
|---|
| 475 | <!--- ************************************************************* ---> |
|---|
| 476 | <cfargument name="gzipFilePath" required="yes" type="string" hint="Pathname of the GZip file to create."> |
|---|
| 477 | <cfargument name="filePath" required="yes" type="string" hint="Pathname of a file to add to the GZip file archive."> |
|---|
| 478 | <!--- ************************************************************* ---> |
|---|
| 479 | <cfscript> |
|---|
| 480 | /* Default variables */ |
|---|
| 481 | var l = 0; |
|---|
| 482 | var buffer = RepeatString(" ",1024).getBytes(); |
|---|
| 483 | var gzFileName = ""; |
|---|
| 484 | var outputFile = ""; |
|---|
| 485 | var lastChr = ""; |
|---|
| 486 | |
|---|
| 487 | |
|---|
| 488 | |
|---|
| 489 | /* Convert to the right path format */ |
|---|
| 490 | arguments.gzipFilePath = PathFormat(arguments.gzipFilePath); |
|---|
| 491 | arguments.filePath = PathFormat(arguments.filePath); |
|---|
| 492 | |
|---|
| 493 | /* Check if the 'extractPath' string is closed */ |
|---|
| 494 | lastChr = Right(arguments.gzipFilePath, 1); |
|---|
| 495 | |
|---|
| 496 | /* Set an slash at the end of string */ |
|---|
| 497 | if(lastChr NEQ instance.slash) |
|---|
| 498 | arguments.gzipFilePath = arguments.gzipFilePath & instance.slash; |
|---|
| 499 | |
|---|
| 500 | try{ |
|---|
| 501 | /* Set output gzip file name */ |
|---|
| 502 | gzFileName = getFileFromPath(arguments.filePath) & ".gz"; |
|---|
| 503 | outputFile = arguments.gzipFilePath & gzFileName; |
|---|
| 504 | |
|---|
| 505 | instance.ioInput.init(arguments.filePath); |
|---|
| 506 | instance.ioOutput.init(outputFile); |
|---|
| 507 | instance.gzOutput.init(instance.ioOutput); |
|---|
| 508 | |
|---|
| 509 | l = instance.ioInput.read(buffer); |
|---|
| 510 | |
|---|
| 511 | while(l GT 0){ |
|---|
| 512 | instance.gzOutput.write(buffer, 0, l); |
|---|
| 513 | l = instance.ioInput.read(buffer); |
|---|
| 514 | } |
|---|
| 515 | |
|---|
| 516 | /* Close the GZip file */ |
|---|
| 517 | instance.gzOutput.close(); |
|---|
| 518 | instance.ioOutput.close(); |
|---|
| 519 | instance.ioInput.close(); |
|---|
| 520 | |
|---|
| 521 | /* Return true */ |
|---|
| 522 | return true; |
|---|
| 523 | } |
|---|
| 524 | |
|---|
| 525 | catch(Any expr) |
|---|
| 526 | { return false; } |
|---|
| 527 | |
|---|
| 528 | </cfscript> |
|---|
| 529 | |
|---|
| 530 | </cffunction> |
|---|
| 531 | |
|---|
| 532 | <!--- ************************************************************* ---> |
|---|
| 533 | |
|---|
| 534 | <cffunction name="gzipExtract" access="public" output="no" returntype="boolean" hint="Extracts a specified GZip file into a specified directory."> |
|---|
| 535 | <!--- ************************************************************* ---> |
|---|
| 536 | <cfargument name="gzipFilePath" required="yes" type="string" hint="Pathname of the GZip file to extract."> |
|---|
| 537 | <cfargument name="extractPath" required="no" type="string" default="#ExpandPath(".")#" hint="Pathname to extract the GZip file to."> |
|---|
| 538 | <!--- ************************************************************* ---> |
|---|
| 539 | <cfscript> |
|---|
| 540 | /* Default variables */ |
|---|
| 541 | var l = 0; |
|---|
| 542 | var buffer = RepeatString(" ",1024).getBytes(); |
|---|
| 543 | var gzFileName = ""; |
|---|
| 544 | var outputFile = ""; |
|---|
| 545 | var lastChr = ""; |
|---|
| 546 | |
|---|
| 547 | /* Convert to the right path format */ |
|---|
| 548 | arguments.gzipFilePath = PathFormat(arguments.gzipFilePath); |
|---|
| 549 | arguments.extractPath = PathFormat(arguments.extractPath); |
|---|
| 550 | |
|---|
| 551 | /* Check if the 'extractPath' string is closed */ |
|---|
| 552 | lastChr = Right(arguments.extractPath, 1); |
|---|
| 553 | |
|---|
| 554 | /* Set an slash at the end of string */ |
|---|
| 555 | if(lastChr NEQ instance.slash) |
|---|
| 556 | arguments.extractPath = arguments.extractPath & instance.slash; |
|---|
| 557 | |
|---|
| 558 | try{ |
|---|
| 559 | /* Set output file name */ |
|---|
| 560 | gzFileName = getFileFromPath(arguments.gzipFilePath); |
|---|
| 561 | outputFile = arguments.extractPath & Left(gzFileName, Len(gzFileName)-3); |
|---|
| 562 | |
|---|
| 563 | /* Initialize gzip file */ |
|---|
| 564 | instance.ioOutput.init(outputFile); |
|---|
| 565 | instance.ioInput.init(arguments.gzipFilePath); |
|---|
| 566 | instance.gzInput.init(instance.ioInput); |
|---|
| 567 | |
|---|
| 568 | while(l GTE 0){ |
|---|
| 569 | instance.ioOutput.write(buffer, 0, l); |
|---|
| 570 | l = instance.gzInput.read(buffer); |
|---|
| 571 | } |
|---|
| 572 | |
|---|
| 573 | /* Close the GZip file */ |
|---|
| 574 | instance.gzInput.close(); |
|---|
| 575 | instance.ioInput.close(); |
|---|
| 576 | instance.ioOutput.close(); |
|---|
| 577 | |
|---|
| 578 | /* Return true */ |
|---|
| 579 | return true; |
|---|
| 580 | } |
|---|
| 581 | catch(Any expr) |
|---|
| 582 | { return false; } |
|---|
| 583 | |
|---|
| 584 | </cfscript> |
|---|
| 585 | </cffunction> |
|---|
| 586 | |
|---|
| 587 | <!--- ************************************************************* ---> |
|---|
| 588 | |
|---|
| 589 | <!------------------------------------------- PRIVATE -------------------------------------------> |
|---|
| 590 | |
|---|
| 591 | <!--- ************************************************************* ---> |
|---|
| 592 | |
|---|
| 593 | <cffunction name="FilesList" access="private" output="no" returntype="array" hint="Create an array with the file names of specified directory."> |
|---|
| 594 | <!--- ************************************************************* ---> |
|---|
| 595 | <cfargument name="directory" required="yes" type="string" hint="Absolute pathname of directory to get files list."> |
|---|
| 596 | <cfargument name="filter" required="no" type="string" default="" hint="File extension filter. One filter can be applied."> |
|---|
| 597 | <cfargument name="recurse" required="no" type="boolean" default="no" hint="Get recursive files of subdirectories."> |
|---|
| 598 | <!--- ************************************************************* ---> |
|---|
| 599 | <cfset var i = 0> |
|---|
| 600 | <cfset var n = 0> |
|---|
| 601 | <cfset var dir = ""> |
|---|
| 602 | <cfset var array = ArrayNew(1)> |
|---|
| 603 | <cfset var path = ""> |
|---|
| 604 | <cfset var subdir = ""> |
|---|
| 605 | |
|---|
| 606 | <cfdirectory action = "list" |
|---|
| 607 | name = "dir" |
|---|
| 608 | directory = "#PathFormat(arguments.directory)#" |
|---|
| 609 | filter = "#arguments.filter#"> |
|---|
| 610 | |
|---|
| 611 | <cfscript> |
|---|
| 612 | /* Loop over directory query */ |
|---|
| 613 | for(i=1; i LTE dir.recordcount; i=i+1){ |
|---|
| 614 | path = PathFormat(arguments.directory & instance.slash & dir.name[i]); |
|---|
| 615 | |
|---|
| 616 | /* Add file to array */ |
|---|
| 617 | if(dir.type[i] eq "file" and dir.name[i] neq instance.filename) |
|---|
| 618 | ArrayAppend(array, path); |
|---|
| 619 | |
|---|
| 620 | /* Get files from sub directorys and add them to the array */ |
|---|
| 621 | else if(dir.type[i] EQ "dir" AND arguments.recurse EQ "yes"){ |
|---|
| 622 | subdir = FilesList(path, arguments.filter, arguments.recurse); |
|---|
| 623 | |
|---|
| 624 | for(n=1; n LTE ArrayLen(subdir); n=n+1) |
|---|
| 625 | ArrayAppend(array, subdir[n]); |
|---|
| 626 | } |
|---|
| 627 | } |
|---|
| 628 | |
|---|
| 629 | /* Return array */ |
|---|
| 630 | return array; |
|---|
| 631 | |
|---|
| 632 | </cfscript> |
|---|
| 633 | </cffunction> |
|---|
| 634 | |
|---|
| 635 | <!--- ************************************************************* ---> |
|---|
| 636 | |
|---|
| 637 | <cffunction name="PathFormat" access="private" output="no" returntype="string" hint="Convert path into Windows or Unix format."> |
|---|
| 638 | <!--- ************************************************************* ---> |
|---|
| 639 | <cfargument name="path" required="yes" type="string" hint="The path to convert."> |
|---|
| 640 | <!--- ************************************************************* ---> |
|---|
| 641 | <cfif FindNoCase("Windows", instance.os)> |
|---|
| 642 | <cfset arguments.path = Replace(arguments.path, "/", "\", "ALL")> |
|---|
| 643 | <cfelse> |
|---|
| 644 | <cfset arguments.path = Replace(arguments.path, "\", "/", "ALL")> |
|---|
| 645 | </cfif> |
|---|
| 646 | <cfreturn arguments.path> |
|---|
| 647 | </cffunction> |
|---|
| 648 | <!--- ************************************************************* ---> |
|---|
| 649 | |
|---|
| 650 | </cfcomponent> |
|---|