【问题标题】:Zip a directory using groovy in SoapUI Pro在 SoapUI Pro 中使用 groovy 压缩目录
【发布时间】:2014-06-12 12:13:55
【问题描述】:

我正在使用 SoapUI Pro 为一家邮政公司测试一组 Web 服务。

作为测试用例的一部分,我生成 pdf 格式的标签和清单文档。我将这些 pdf 文件存储在一个文件夹中,该文件夹由测试用例开始时的 groovy 脚本创建。有时,当我运行测试时,不会生成标签或 pdf,因为我正在测试错误代码,即使用不会创建发货的测试输入,因此不会生成标签。

我在测试用例末尾有一个脚本,如果我创建的文件夹为空,它将删除它。如果它不为空,即其中有标签 pdf,我想修改它以压缩整个文件夹。但不确定如何执行此操作。

以下是我删除空文件夹的脚本。抱歉,我没有尝试任何代码来进行压缩。

import groovy.xml.NamespaceBuilder
import org.apache.tools.antBuilder.*
//This script deletes the pdf sub folder created by 'CreateFolderForPDFs' if no pdfs  are generated as part of test
// i.e test does not produce valid shipments.
def pdfSubFolderPath = context.expand( '${#TestCase#pdfSubFolderPath}' )
def pdfSubFolderSize = new File(pdfSubFolderPath).directorySize()

if( pdfSubFolderSize == 0){
    new File(pdfSubFolderPath).deleteDir()
    log.info "Deleted the pdf sub folder " +pdfSubFolderPath +"because contains no pdfs"
}
else 
{
//zip folder and contents
}

【问题讨论】:

    标签: groovy zip soapui


    【解决方案1】:

    我修改你的代码来压缩你pdfSubFolderPath中的所有文件:

    import groovy.xml.NamespaceBuilder
    import org.apache.tools.antBuilder.*
    import java.util.zip.ZipOutputStream
    import java.util.zip.ZipEntry
    import java.nio.channels.FileChannel
    
    //This script deletes the pdf sub folder created by 'CreateFolderForPDFs' if no pdfs  are generated as part of test
    // i.e test does not produce valid shipments.
    def pdfSubFolderPath = context.expand( '${#TestCase#pdfSubFolderPath}' )
    def pdfSubFolderSize = new File(pdfSubFolderPath).directorySize()
    
    if( pdfSubFolderSize == 0){
        new File(pdfSubFolderPath).deleteDir()
        log.info "Deleted the pdf sub folder " +pdfSubFolderPath +"because contains no pdfs"
    }
    else 
    {
        //zip folder and contents
        def zipFileName = "C:/temp/file.zip" // output zip file name
        def inputDir = pdfSubFolderPath; // dir to be zipped
    
        // create the output zip file
        def zipFile = new ZipOutputStream(new FileOutputStream(zipFileName))
    
        // for each file in the directori
        new File(inputDir).eachFile() { file ->
            zipFile.putNextEntry(new ZipEntry(file.getName()))
            file.eachByte( 1024 ) { buffer, len -> zipFile.write( buffer, 0, len ) }
            zipFile.closeEntry()
        }
        zipFile.close()
    }
    

    希望这会有所帮助,

    【讨论】:

    • 如果文件长度 > 1024 字节会怎样?最好使用:file.eachByte( 1024 ) { buffer, len -> zipFile.write( buffer, 0, len ) }
    猜你喜欢
    • 2012-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多