将XML文件压缩到内存中并上传到azure文件存储

时间:2017-10-16 14:47:55

标签: xml azure zip

我有一个XDocument作为参数提供,需要压缩并上传到Azure文件存储。这里有一些问题可以部分解决这个问题,但是对于blob存储。签名是不同的,如果没有在云中获取压缩文件的校验和错误,我无法使其适用于我的情况。

public static bool ZipAndUploadDocumentToAzure(XDocument doc, Configuration config)
    {
        if (IsNullOrEmpty(config.StorageAccountName) ||
            IsNullOrEmpty(config.StorageAccountKey) ||
            IsNullOrEmpty(config.StorageAccounyShareReference) ||
            IsNullOrEmpty(config.StorageAccounyDirectoryReference))
        {
            return false;
        }

        CloudStorageAccount storageAccount = CloudStorageAccount.Parse($"DefaultEndpointsProtocol=https;AccountName={config.StorageAccountName};AccountKey={config.StorageAccountKey}");

        CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
        CloudFileShare share = fileClient.GetShareReference(config.StorageAccounyShareReference);
        CloudFileDirectory root = share.GetRootDirectoryReference();
        CloudFileDirectory dir = root.GetDirectoryReference(config.StorageAccounyDirectoryReference);

        var cloudFile = dir.GetFileReference("myarchive.zip");


        using (var stream = new MemoryStream())
        {
            var xws = new XmlWriterSettings
            {
                OmitXmlDeclaration = true,
                Indent = true
            };

            using (XmlWriter xw = XmlWriter.Create(stream, xws))
            {
                doc.WriteTo(xw);
            }

            //where to actually use ZipArchive 'using block' without saving 
            //any of the zip or XML or XDocument files locally 
            //and that it doesn't produce checksum error

            cloudFile.UploadFromStream(stream);//this gives me XML file 
                                               //saved to file storage
            return true;
        }
    }

1 个答案:

答案 0 :(得分:1)

您实际上需要创建一个ZipArchive,然后在那里编写您的Xml文档。请参阅以下示例代码:

    static void ZipAndUploadFile()
    {
        var filePath = @"C:\Users\Gaurav.Mantri\Desktop\StateProvince.xml";
        XDocument doc = null;
        using (var fs = new FileStream(filePath, FileMode.Open))
        {
            doc = XDocument.Load(fs);
        }
        var cred = new StorageCredentials(accountName, accountKey);
        var account = new CloudStorageAccount(cred, true);
        var fc = account.CreateCloudFileClient();
        var share = fc.GetShareReference("test");
        share.CreateIfNotExists();
        var rootDirectory = share.GetRootDirectoryReference();
        var file = rootDirectory.GetFileReference("test.zip");
        using (var stream = new MemoryStream())
        {
            var xws = new XmlWriterSettings
            {
                OmitXmlDeclaration = true,
                Indent = true
            };

            using (var archive = new ZipArchive(stream, ZipArchiveMode.Create, true))
            {
                var entry = archive.CreateEntry("doc.xml");
                using (var entryStream = entry.Open())
                {
                    using (XmlWriter xw = XmlWriter.Create(entryStream, xws))
                    {
                        doc.WriteTo(xw);
                    }
                }
            }
            stream.Position = 0;
            file.UploadFromStream(stream);
        }
    }