【问题标题】:Is there a way to open and read zip files programmatically uploaded by PXUploadDialog?有没有办法打开和读取 PXUploadDialog 以编程方式上传的 zip 文件?
【发布时间】:2018-07-10 23:45:10
【问题描述】:

我有一个用户想要上传一个包含多个图像的 zip 文件和一个包含与图像相关的数据的 CSV 文件。他们希望能够上传 zip 文件,并让程序通过查找和处理 CSV 文件中的数据,然后将 zip 中的图像存储到适当的位置来对其进行剖析。

我正在尝试弄清楚如何打开 zip,以便循环浏览其中的每个文件以找到我需要的内容。有没有办法做到这一点?

【问题讨论】:

    标签: acumatica


    【解决方案1】:

    您可以使用 Acumatica Framework 中的 ZipArchive:

    // Uploaded file needs to be attached to a DAC record
    Guid[] files = PXNoteAttribute.GetFileNotes(DACCache, DACRecord);
    UploadFileMaintenance upload = PXGraph.CreateInstance<UploadFileMaintenance>();
    
    foreach (Guid fileID in files)
    {
        FileInfo fileInfo = upload.GetFile(fileID);
    
        if (fileInfo != null)
        {
            using (MemoryStream stream = new MemoryStream(fileInfo.BinData))
            {
                ZipArchive zip = ZipArchive.OpenReadonly(stream);
    
                string tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
                Directory.CreateDirectory(tempDirectory);
                ZipFolder.Decompress(zip, tempDirectory, true);
    
                foreach (string filePath in Directory.GetFiles(tempDirectory))
                {
                    // Enumerating decompressed files
                }
            }
        }
    }
    

    【讨论】:

    • 谢谢 HB。我会付诸行动,并让你知道结果。
    • 代码未经测试,几乎没有错误处理,但应该可以帮助您入门。
    • 我在 Acumatica 的 ZipArchive 上看到的唯一函数是 CreateFrom()。我需要 OpenReadonly 的参考资料吗?另外,我找不到任何类似于 ZipFolder 的东西。到目前为止,我能够让其余的语法正常工作。有什么建议么?附言您所说的 ZipFolder 是指 ZipFile 吗?
    • PX.DbServices.ZipArchive 和 PX.DbServices.ZipFolder,您可能可以使用 PX.Common.ZipArchive 获得类似的结果,但是代码会与我提供的不同。
    • 谢谢 HB!这就是我需要的。可悲的是,我忘了提到该文件是密码的。 Acumatica 是否能够在尝试打开压缩文件时向其发送密码?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多