【解決方法】Azure ファイル共有ディレクトリで zip ファイルを解凍するにはどうすればよいですか?

プログラミングQA


zip ファイル (abc.zip としましょう) を含む Azure ファイル共有ディレクトリがあります。
これを C# Azure 関数の C# コードを使用して解凍したいと考えています。
私が持っているのは、適切に入力された CloudFileDirectory です。 ここで、この Azure ファイル共有ディレクトリにあるファイルを解凍するメソッドを呼び出したいと思います。

メソッドの名前は「public static async Task ExtractZipFileCloudStorageAsync(CloudFileDirectory extractDirectory)」で、引数を 1 つとります。

多くのコードを試しましたが、何も機能しないようです。 私が試した最後のコードは、CloudFile からストリームを作成しようとしています。
コード行「using (var zipArchive = new ZipArchive(stream))」では、エラー (キャッチ) => 「中央ディレクトリ レコードの終わりが見つかりませんでした。」というエラーが発生します。

私の何が間違っているのでしょうか?

私が試したこと:

C#
 public static async Task<ResultHelper> ExtractZipFileCloudStorageAsync(CloudFileDirectory extractDirectory)
 {
    try
    {
       var zipFiles = await extractDirectory.ListFilesAndDirectoriesSegmentedAsync(null);

    using (var httpClient = new HttpClient())
    {
        foreach (var zipFile in zipFiles.Results)
        {                        
            if (zipFile is CloudFile file && file.Uri.AbsoluteUri.EndsWith(".zip", StringComparison.OrdinalIgnoreCase))
            {                            
                var zipFileBytes = await GetMemoryStreamAsync(file);
                using (var stream = new MemoryStream(zipFileBytes.GetBuffer()))
                {
                    using (var zipArchive = new ZipArchive(stream))
                    {

                    }
                }
            }
        }

         return ...
     }
    catch (Exception ex)
    {
        return ...
    }
}
}

解決策 1

再構築したところ、動作するようになりました =>

C#
public static class ExtractZipFileHelper
{        
    /// <summary>
    /// Extract zip file for Azure fileshare folders (cloud directory).
    /// </summary>
    /// <param name="extractDirectory"></param>
    /// <returns></returns>
    public static async Task<ResultHelper> ExtractZipFileCloudStorageAsync(CloudFileDirectory extractDirectory)
    {
        try
        {
            if (extractDirectory == null)
            {
                return ResultHelper.CreateResultHelper(false, "The parameter 'extractDirectory' is empty or NULL!");
            }
            
            return await ExtractZipFileAsync(extractDirectory);
        }
        catch (Exception ex)
        {
            return ResultHelper.CreateResultHelper(false, $"Error while extracting zip files: {ex.Message}");
        }
    }        

    /// <summary>
    /// Extract all zip files in the extractDirectory.
    /// </summary>
    /// <param name="extractDirectory"></param>
    /// <returns></returns>
    private static async Task<ResultHelper> ExtractZipFileAsync(CloudFileDirectory extractDirectory)
    {
        try
        {
            var filesAndDirectories = await extractDirectory.ListFilesAndDirectoriesSegmentedAsync(null);
            // Loop through all entries in the CloudFileDirectory.
            foreach (var item in filesAndDirectories.Results)
            {
                // Verify that the item is a CloudFile.
                if (item is CloudFile cloudFile)
                {
                    // Check if the item ends in '.zip'.
                    if (cloudFile.Name.EndsWith(".zip"))
                    {
                        // Create a MemoryStream for the contents of the zip file.
                        using (MemoryStream memoryStream = new MemoryStream())
                        {
                            // Download the contents of the zip file to the MemoryStream.
                            await cloudFile.DownloadToStreamAsync(memoryStream);

                            // Use a ZipArchive to read the contents of the zip file.
                            using (ZipArchive zipArchive = new ZipArchive(memoryStream))
                            {
                                // Walk through all entries in the zip file.
                                foreach (ZipArchiveEntry entry in zipArchive.Entries)
                                {
                                    // Get a reference to the CloudFile object for each entry.
                                    CloudFile entryFile = extractDirectory.GetFileReference(entry.FullName);

                                    // Create a MemoryStream for the entry content.
                                    using (MemoryStream entryStream = new MemoryStream())
                                    {
                                        // Open the entry in the zip file.
                                        using (Stream entryFileStream = entry.Open())
                                        {
                                            // Copy the entry content to the MemoryStream.
                                            await entryFileStream.CopyToAsync(entryStream);
                                        }

                                        entryStream.Position = 0;
                                        // Upload the contents to the corresponding CloudFile.
                                        await entryFile.UploadFromStreamAsync(entryStream);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return ResultHelper.CreateResultHelper(true);
        }
        catch (Exception ex)
        {
            return ResultHelper.CreateResultHelper(false, $"Error while extracting zip files: {ex.Message}");
        }
    }
}

コメント

タイトルとURLをコピーしました