Пример #1
0
        public static bool ZipDocumentAndUploadToAzure(XmlDocumentType xmlDocumentType, XDocument doc, Configuration config, string dateTimeStamp, IDictionary <string, byte[]> files = null)
        {
            if (IsNullOrEmpty(config.StorageAccountName) ||
                IsNullOrEmpty(config.StorageAccountKey) ||
                IsNullOrEmpty(config.StorageAccountShareReference) ||
                IsNullOrEmpty(config.StorageAccountCatalogDirectoryReference) ||
                IsNullOrEmpty(config.StorageAccountResourcesDirectoryReference))
            {
                return(false);
            }

            StorageCredentials  cred           = new StorageCredentials(config.StorageAccountName, config.StorageAccountKey);
            CloudStorageAccount storageAccount = new CloudStorageAccount(cred, true);

            CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
            CloudFileShare  share      = fileClient.GetShareReference(config.StorageAccountShareReference);

            share.CreateIfNotExists();

            CloudFileDirectory root = share.GetRootDirectoryReference();
            CloudFileDirectory dir  = root.GetDirectoryReference(config.GetAzureStorageDirectoryName(xmlDocumentType));

            dir.CreateIfNotExists();

            CloudFile cloudFile = dir.GetFileReference(GetZipFileName(config, xmlDocumentType, dateTimeStamp));

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

                using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Create, true))
                {
                    if (files != null)
                    {
                        foreach (KeyValuePair <string, byte[]> imageFile in files)
                        {
                            ZipArchiveEntry zipEntry = archive.CreateEntry(imageFile.Key);
                            using (Stream entryStream = zipEntry.Open())
                            {
                                entryStream.Write(imageFile.Value, 0, imageFile.Value.Length);
                            }
                        }
                    }

                    ZipArchiveEntry entry = archive.CreateEntry("catalog.xml");
                    using (Stream entryStream = entry.Open())
                    {
                        using (XmlWriter xw = XmlWriter.Create(entryStream, xws))
                        {
                            doc.WriteTo(xw);
                        }
                    }
                }
                stream.Position = 0;
                cloudFile.UploadFromStream(stream);

                switch (xmlDocumentType)
                {
                case XmlDocumentType.Catalog:
                    config.CatalogPathInCloud = cloudFile.Name;
                    break;

                default:
                    config.ResourceNameInCloud = cloudFile.Name;
                    break;
                }
                return(true);
            }
        }
Пример #2
0
        public static string GetZipFileName(Configuration config, XmlDocumentType xmlDocumentType, string dateTimeStamp, int fileCount)
        {
            string zipFileWithTimeStamp = $"{config.GetAzureFileName(xmlDocumentType)}.{dateTimeStamp}.{fileCount}.zip";

            return(zipFileWithTimeStamp);
        }