coldfusion - ColdFusion zip an entire folder
问题描述
I moved some files from one directory to another in my server. I would like to zip the destination folder after moving the files.
<cfoutput>
<cfset destination = expandPath("./TenantFiles/tempEmail/11/") />
<cfif not directoryExists(destination)>
<cfdirectory action="create" directory="#destination#">
<cfelse>
<cfdirectory action="delete" directory="#destination#" recurse="true">
<cfdirectory action="create" directory="#destination#">
</cfif>
<cfloop query="myQuery">
<cfset sourcefile = expandPath("./TenantFiles/11/#myQuery.TenantID#/#myQuery.DocumentName#") />
<cfif FileExists(sourcefile)>
<cfscript>
FileMove(#sourcefile#, #destination#);
</cfscript>
</cfif>
</cfloop>
<cfzip action="zip" file="#destination#\ZipFile.zip" source="#destination#" filter="*.pdf" />
</cfoutput>
It is returning the error:
Can not create a zip file with no entries.
Make sure that there is at least one entry in the zip file.
However the #destination#
folder has a lot of pdf files.
Thanks
解决方案
我没有在可能的问题上来回讨论,而是创建了一个函数,可以用来测试您的环境中是否发生了其他事情。我消除了文件的移动和其他可能的问题来源。
尝试设置一个匹配此示例的测试文件夹,并在使用您的真实查询和文件之前查看它是否有效。我将其设置为不在 zip 中存储文件路径,因为这是您的文件移动和添加的结果,但最终可能不是您想要的。
<cfset myQuery = queryNew("") />
<cfset TenantIDs = [1,2,3,4] />
<cfset DocumentNames = ['one.pdf','two.pdf','three.txt','four.doc'] />
<cfset queryAddColumn(myQuery, 'TenantID', "integer", TenantIDs) />
<cfset queryAddColumn(myQuery, 'DocumentName', "varchar", DocumentNames) />
<cfset zipPdfFiles(expandPath('./testfiles'), myQuery, "TenantID", "DocumentName", expandPath('./testfiles/zipFile.zip'), "pdf") />
<cffunction name="zipPdfFiles" output="true">
<cfargument name="baseFilePath" required="true" />
<cfargument name="fileQuery" required="true" />
<cfargument name="folderColumn" required="true" />
<cfargument name="fileNameColumn" required="true" />
<cfargument name="zipFilePath" required="true" />
<cfargument name="allowExtensions" default="" hint="comma separated list of extensions or blank for all" />
<cfzip action="zip" file="#arguments.zipFilePath#" storePath="false">
<cfloop query="arguments.fileQuery">
<cfif not len(arguments.allowExtensions) or listFindNoCase(arguments.allowExtensions, listLast(myQuery.DocumentName, "."))>
<cfzipparam source="#arguments.baseFilePath#/#arguments.fileQuery[arguments.folderColumn][currentRow]#/#arguments.fileQuery[arguments.fileNameColumn][currentRow]#" />
</cfif>
</cfloop>
</cfzip>
</cffunction>
推荐阅读
- node.js - 新版本的软件包已发布,但未显示在 npm 上,并且用户尚未安装
- git - Azure DevOps Powershell git push 命令抛出“无法提示,因为终端提示已被禁用”安全错误
- python - ValueError: 'c' 参数有 1 个元素,不能与大小为 400 的 'x' 和大小为 400 的 'y' 一起使用
- sql-server - 如果响应超过 1192 字节,SQL Server 断开连接
- mysql - 尝试从表中在 phpmyAdmin 中创建函数
- python - 如何根据 for 循环的结果制作矩阵
- javascript - 如何使用 React 测试库选择没有标签的输入表单?
- html - 颜色未填充整个 div + 填充故障
- python - 用python从numpy矩阵绘制位图
- flutter - Flutter pigeon 相互引用 @HostApi 类