Click here to Skip to main content
15,887,331 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a Azure fileshare directory with a zip file, lets say abc.zip.
I want to unzip this with C# code in a C# Azure function.
What I have is the right filled CloudFileDirectory. Now I want to call a method that unzip the file thats is in this Azure fileshare directory.

The name of the method is "public static async Task<resulthelper> ExtractZipFileCloudStorageAsync(CloudFileDirectory extractDirectory)" and takes 1 argument.

I have tried a lot of code but nothing seems to work. The last code I tried is trying to make a stream from a CloudFile.
The code line "using (var zipArchive = new ZipArchive(stream))" gives me a error (catch) => "End of Central Directory record could not be found."

What do I do wrong?

What I have tried:

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 ...
    }
}
}
Posted

1 solution

I rebuilded it an now it works =>

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}");
        }
    }
}
 
Share this answer
 
Comments
Andre Oosthuizen 16-Feb-24 13:53pm    
My +5 on working to a solution yourself!! Well done.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900