Created
August 14, 2019 11:11
-
-
Save bennadel/5225bcdda64c1362112734f3b1d54d4d to your computer and use it in GitHub Desktop.
Playing With The Zip Virtual File System (VFS) In Lucee 5.3.2.77
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfscript> | |
// Let's check to make sure the Zip virtual file system is enabled before we attempt | |
// to use it to create a Zip archive file. | |
// -- | |
// NOTE: I have this here simply as an exploration of the VFS metadata function. | |
if ( ! getVfsMetaData( "zip" ).enabled ) { | |
echo( "Zip VFS not enabled." ); | |
abort; | |
} | |
// In the ZIP Virtual File System, the exclamation mark / bang symbol is used to | |
// delimit the "real path" (the location of the ZIP file) from the "virtual path" | |
// (the location of the Zip Entry within the Zip archive). The path following the | |
// "!" character will be relative to the root of the archive (even for absolute | |
// file paths, ie those that start with "/"). | |
// -- | |
// NOTE: Physical file paths in Lucee can be either absolute or relative to the | |
// current directory. As such, I can use "./" to define the file path relative to | |
// the working directory of the current CFML template. | |
zipFS = "zip://./archive.zip!"; | |
// ------------------------------------------------------------------------------- // | |
// ------------------------------------------------------------------------------- // | |
fileWrite( "#zipFS#/README.md", "Welcome to my Archive" ); | |
// Even though we're dealing with a "Virtual File System", the same rules around file | |
// IO still exist. We can't just write to a directory before it exists. As such, we | |
// have to ensure our sub-directories before we write to them (assuming that we're | |
// not using a function, like directoryCopy(), which can create parent paths). | |
directoryCreate( | |
path = "#zipFS#/files/poems/", | |
createPath = "true", | |
ignoreExists = true | |
); | |
// Write some content to the newly-created directory in our Zip archive. | |
fileWrite( "#zipFS#/files/poems/one.txt", "Roses are red, violets are blue..." ); | |
fileWrite( "#zipFS#/files/poems/two.txt", "Once upon a time, in a land far away..." ); | |
fileWrite( "#zipFS#/files/poems/three.txt", "There once was a man from Manhattan..." ); | |
// Copy an entire directory. | |
// -- | |
// NOTE: Even though the "haikus" directory doesn't exist in our Zip, the | |
// directoryCopy() function is capable of creating the destination path via the | |
// "createPath" option. As such, we don't need to ensure directory before we write | |
// to it. | |
directoryCopy( | |
source = "./haikus/", | |
destination = "#zipFS#/files/haikus/", | |
recurse = true, | |
createPath = true | |
); | |
// ------------------------------------------------------------------------------- // | |
// ------------------------------------------------------------------------------- // | |
// Now that we've create and populated our Zip archive with files, let's look at what | |
// we have in our virtual file system. | |
fileList = directoryList( | |
path = zipFS, | |
recurse = true | |
); | |
// The directory list of the Zip archive will return file-paths that are prefixed | |
// with the location of the archive file. As such, let's strip off that prefix for | |
// simpler rendering. We can do easily when we remember that the exclamation point | |
// ("!") delimits the physical file path from the virtual file path. | |
shortFileList = fileList.map( | |
( path ) => { | |
// Return only the "virtual file path" portion of the full file path. | |
return( path.listRest( "!" ) ); | |
} | |
); | |
dump( | |
label = "Zip Virtual File System", | |
var = shortFileList | |
); | |
// ------------------------------------------------------------------------------- // | |
// ------------------------------------------------------------------------------- // | |
// As a final step in the demo, let's extract the Zip archive so we can examine the | |
// contents in the Finder. | |
extractPath = "./extract-#createUniqueId()#/"; | |
directoryCreate( extractPath ); | |
extract( "zip", "./archive.zip", extractPath ); | |
</cfscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment