意味的には正しいですが、ネイサンが提案した解決策はやや不完全だと思います。
彼は、手順1の前に抽出されたテキストをBase64でデコードするのを忘れたと思います。また、サードパーティのZIPユーティリティを使用して一時ファイルを検証することで、人間の介入が不要になるように改善を加えます。
これは、一時ZIPファイルにアクセスし、それが有効なZIPファイルであるかどうかを確認することを意味します。したがって、要件を実装するためのアルゴリズムは次のようになります。
- 要素Base64でエンコードされたテキストコンテンツにアクセスし、Base64でデコードします
- ZIPの生のテキストを一時ファイルに出力して、ZIPファイルを作成します
- 一時的に作成したZIPファイルが有効かどうかを確認してください
これらすべてを念頭に置いて、完全なGroovyスクリプトは次のようになります。
import org.apache.commons.codec.binary.Base64
// Step 1: Access element Base64-encoded text content and Base64 decode it
String tempZipFilename = "temp.zip"
def textBase64 = context.expand(
'${Step#Request#//ns2:getInputConfigFilesResponse[1]/return[1]}' )
def b64 = new Base64()
def zipTextBytes = b64.decode(textBase64.getBytes())
// Step 2: Output ZIP raw text into a temporary file
def zipFile = new java.io.File(tempZipFilename)
FileOutputStream fos = new java.io.FileOutputStream(zipFile)
fos.write( zipTextBytes )
fos.flush()
fos.close()
log.info "Temporary ZIP file stored as: ${zipFile.getCanonicalPath()}"
// Step 3: Check if the temporarily created ZIP file is valid
boolean responseValid
try {
zipFile = new java.util.zip.ZipFile(tempZipFilename)
responseValid = true
log.info "Number of files in the ZIP file: ${zipFile.size()}"
} catch (java.util.zip.ZipException e) {
responseValid = false
log.error "The received response contains a bad ZIP"
}
log.info "Web service response is " + (responseValid ? "valid" : "invalid")
これが私と同じようにうまくいくかどうか教えてください。:-)
乾杯!
ションジラ
psこの質問に「ZIP」タグを追加して、ここに埋め込まれているGroovyからZIPを処理するためのソリューションをより簡単に見つけられるようにすることをお勧めします。