如何使用Azure函数解压缩Azure文件共享中的文件?

如何使用Azure函数解压缩Azure文件共享中的文件?,azure,azure-functions,azure-storage,azure-files,Azure,Azure Functions,Azure Storage,Azure Files,我有一个Azure存储帐户和Azure文件共享。我想使用Azure函数将zip存档文件解压缩到文件共享中的另一个目录。我用C#写了这段代码: 但我得到了一个错误: System.Private.CoreLib: Exception while executing function: HttpTriggerExtract. Microsoft.WindowsAzure.Storage: Operation is not valid due to the current state of the

我有一个Azure存储帐户和Azure文件共享。我想使用Azure函数将zip存档文件解压缩到文件共享中的另一个目录。我用C#写了这段代码:

但我得到了一个错误:

System.Private.CoreLib: Exception while executing function: HttpTriggerExtract. Microsoft.WindowsAzure.Storage: 
Operation is not valid due to the current state of the object.
如何解决这个问题

编辑:我使用MemoryStream修复了错误。此外,此代码可以工作:

        foreach (var zipEntry in file1.Entries) {

            var fsz = output.GetFileReference(zipEntry.Name);

            using (var ms = new MemoryStream())
            {

                using (var fileStream = zipEntry.Open())
                {
                    await fileStream.CopyToAsync(ms);

                    ms.Seek(0, SeekOrigin.Begin);
                    await fsz.UploadFromStreamAsync(ms);

                }

            }

关于此问题,请参考以下代码(我使用package
WindowsAzure.Storage 9.3.1
来完成此操作)


您还有其他顾虑吗?如果你没有其他顾虑,可以吗?它可能会帮助更多有类似问题的人。问题是从fileStream对象获取流。我不得不使用额外的内存流,现在一切都好了。谢谢你的分享。你能发布你的答案吗?是的,我已经编辑了我的问题并添加了工作代码。请投票。
        foreach (var zipEntry in file1.Entries) {

            var fsz = output.GetFileReference(zipEntry.Name);

            using (var ms = new MemoryStream())
            {

                using (var fileStream = zipEntry.Open())
                {
                    await fileStream.CopyToAsync(ms);

                    ms.Seek(0, SeekOrigin.Begin);
                    await fsz.UploadFromStreamAsync(ms);

                }

            }
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
            CloudFileClient cloudFileClient = storageAccount.CreateCloudFileClient();
            CloudFileShare cloudFileShare = cloudFileClient.GetShareReference("share2");
            CloudFileDirectory rootDirectory = cloudFileShare.GetRootDirectoryReference();
            CloudFileDirectory input = rootDirectory.GetDirectoryReference("input");
            CloudFileDirectory output = rootDirectory.GetDirectoryReference("output");
            CloudFile cloudFile = input.GetFileReference("sample.zip");
            using (var stream = await cloudFile.OpenReadAsync())
            using (var zipArchive = new ZipArchive(stream)) {
                foreach (var entry in zipArchive.Entries)
                {
                    if (entry.Length > 0) {
                        CloudFile extractedFile = output.GetFileReference(entry.Name);

                        using (var entryStream = entry.Open())
                        {
                            byte[] buffer = new byte[16 * 1024];
                            using (var ms = await extractedFile.OpenWriteAsync(entry.Length))
                            {
                                int read;
                                while ((read = await entryStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
                                {
                                    ms.Write(buffer, 0, read);
                                }
                            }
                        }
                    }
                
                }

            }