मैं Azure फ़ाइलशेयर निर्देशिका में एक ज़िप फ़ाइल को कैसे अनज़िप करूँ?

प्रोग्रामिंग


मेरे पास एक ज़िप फ़ाइल के साथ एक Azure फ़ाइलशेयर निर्देशिका है, मान लीजिए abc.zip।
मैं इसे C# Azure फ़ंक्शन में C# कोड से अनज़िप करना चाहता हूं।
मेरे पास सही भरी हुई CloudFileDirectory है। अब मैं एक ऐसी विधि को कॉल करना चाहता हूं जो इस Azure फ़ाइलशेयर निर्देशिका में मौजूद फ़ाइल को अनज़िप कर दे।

विधि का नाम “सार्वजनिक स्थैतिक एसिंक कार्य

मैंने बहुत सारे कोड आज़माए हैं लेकिन कुछ भी काम नहीं कर रहा है। आखिरी कोड जो मैंने आज़माया वह क्लाउडफ़ाइल से एक स्ट्रीम बनाने का प्रयास कर रहा है।
कोड लाइन “उपयोग (var zipArchive = new ZipArchive(stream))” से मुझे एक त्रुटि मिलती है (कैच) => “सेंट्रल डायरेक्ट्री रिकॉर्ड का अंत नहीं मिल सका।”

मुझसे क्या गलती हुई है?

मैंने क्या प्रयास किया है:

सी#
 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

मैंने इसे दोबारा बनाया और अब यह काम करता है =>

सी#
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をコピーしました