首页 > 解决方案 > 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

标签: coldfusioncoldfusion-10cfzip

解决方案


我没有在可能的问题上来回讨论,而是创建了一个函数,可以用来测试您的环境中是否发生了其他事情。我消除了文件的移动和其他可能的问题来源。

尝试设置一个匹配此示例的测试文件夹,并在使用您的真实查询和文件之前查看它是否有效。我将其设置为不在 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>

推荐阅读