Azure ADLS中解压带密码保护的Zip文件报错求解决方案
解决Azure ADLS中带密码Zip文件解压迁移问题
错误原因分析
- Azure Data Factory的Copy Activity不支持处理带密码保护的Zip文件
- 错误提示里的“不支持的压缩方法”,说明该Zip使用了Deflate以外的压缩算法(比如AES加密、LZMA等),而Copy Activity仅支持无密码的Deflate格式Zip
解决方案
方法1:用Azure Function自定义处理(推荐)
创建Blob触发的Azure Function,自动处理ADLS中的带密码Zip文件:
- 新建一个Blob触发的Function,触发源设为存放加密Zip的ADLS容器
- 在Function中引入支持密码解压的类库(比如DotNetZip、SharpZipLib)
- 编写逻辑完成:读取加密Zip→密码验证解压→将文件上传到目标容器→可选删除源Zip
- 示例C#代码:
using System.IO; using System.Threading.Tasks; using Microsoft.Azure.WebJobs; using Microsoft.Extensions.Logging; using Azure.Storage.Blobs; using Ionic.Zip; namespace UnzipProtectedZip { public static class UnzipFunction { [FunctionName("UnzipProtectedZip")] public static async Task Run( [BlobTrigger("source-container/{name}", Connection = "ADLSConnection")] Stream zipStream, string name, ILogger log) { log.LogInformation($"开始处理文件: {name}"); // 初始化目标容器客户端 var targetContainer = new BlobContainerClient(Environment.GetEnvironmentVariable("ADLSConnection"), "target-container"); await targetContainer.CreateIfNotExistsAsync(); // 从环境变量获取Zip密码 string zipPwd = Environment.GetEnvironmentVariable("ZipPassword"); using (ZipFile zip = ZipFile.Read(zipStream)) { zip.Password = zipPwd; foreach (ZipEntry entry in zip) { if (!entry.IsDirectory) { using (var entryStream = new MemoryStream()) { entry.Extract(entryStream); entryStream.Position = 0; var targetBlob = targetContainer.GetBlobClient(entry.FileName); await targetBlob.UploadAsync(entryStream, overwrite: true); } } } } // 可选:删除源Zip文件 var sourceContainer = new BlobContainerClient(Environment.GetEnvironmentVariable("ADLSConnection"), "source-container"); await sourceContainer.GetBlobClient(name).DeleteIfExistsAsync(); } } }
- 配置注意:在Function应用设置中添加
ADLSConnection(ADLS存储账户连接字符串)和ZipPassword(Zip文件的密码)
方法2:ADF自定义活动调用外部服务
在Azure Data Factory中使用自定义活动,调用Azure Batch或者Azure Function来完成解压:
- 如果用Azure Batch:创建Batch池,编写解压脚本(比如PowerShell+7-Zip),通过ADF自定义活动提交Batch任务
- 如果用Azure Function:直接在ADF自定义活动中调用已创建的解压Function,传递Zip文件路径和密码参数
方法3:预处理Zip文件(适用非实时场景)
如果可以在文件上传到ADLS前处理,先移除密码或转换压缩格式:
- 使用7-Zip等工具解压并重新压缩为无密码的Deflate格式Zip:
7z x protected.zip -pYourPassword -oextracted/ 7z a -tzip -mm=Deflate unprotected.zip extracted/* - 将处理后的无密码Zip上传到ADLS,再用Copy Activity正常解压迁移
内容的提问来源于stack exchange,提问作者Anuradha Joshi
相关产品推荐
相关产品推荐

