Unzip example

3 posts, 1 answers
  1. Sébastien
    Sébastien avatar
    10 posts
    Member since:
    Dec 2018

    Posted 19 Dec 2019 Link to this post

    Hello,

    Do you have an example to unzip a zip with multiple files ? 

    I wish I could use ExtractToDirectory(string sourceArchiveFileName, string destinationDirectoryName), but it seems to not be available on Xamarin yet. 

    Thanks.

     

     

  2. Answer
    Lance | Team Lead - US DevTools Support
    Admin
    Lance | Team Lead - US DevTools Support avatar
    1070 posts

    Posted 20 Dec 2019 Link to this post

    Hello Sébastien,

    The Zip Extensions are available for Xamarin, but you need to take special consideration for the file paths. I've written a demo for you, find it attached to this reply.

    For your convenience, here's the relevant code:

    private async void DownloadAndExtractFilesButton_Clicked(object sender, EventArgs e)
    {
        var zipFileName = "MyFiles.zip";
        var localFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
        var zipFilePath = Path.Combine(localFolder,zipFileName);
    
        // Downloading ZIP file
        using (var client = new HttpClient())
        {
            using (var downloadStream = await client.GetStreamAsync("https://progressdevsupport.blob.core.windows.net/sampledocs/MyFiles.zip"))
            using (var fileStream = File.OpenWrite(zipFilePath))
            {
                await downloadStream.CopyToAsync(fileStream);
            }
        }
    
        // Create folder to extract files into
        var destinationFolder = Path.Combine(localFolder, "ExtractedFiles");
    
        // Extract files into new directory
        ZipFile.ExtractToDirectory(zipFilePath, destinationFolder);
    
        // Verify files in new directory
        OutputLabel.Text = $"Listing files in {destinationFolder}:\r\n\n";
    
        foreach (var fileName in Directory.EnumerateFiles(destinationFolder))
        {
            OutputLabel.Text += $"{fileName}\r\n";
        }
    }

    Notice two important items:

    • The ExtractToDirectory works as expected (it is an extension method for System.IO.Compression.ZipFile)
    • The LocalApplicationData folder, is required to properly work with files in Xamarin (visit the Microsoft documentation to learn more: Working With Files - Saving and Loading Files

    Bonus

    I added a separate method that shows how you can enumerate the ZIP archives files and work with them directly without having to extract to a folder first.

    private async void OpenArchive_Clicked(object sender, EventArgs e)
    {
        using (var client = new HttpClient())
        {
            using (var downloadStream = await client.GetStreamAsync("https://progressdevsupport.blob.core.windows.net/sampledocs/MyFiles.zip"))
            using (var memStream = new MemoryStream())
            {
                await downloadStream.CopyToAsync(memStream);
    
                using (var archive = new Telerik.Zip.ZipArchive(memStream))
                {
                    // Iterate over the files in the archive
                    foreach (var zipArchiveEntry in archive.Entries)
                    {
                        // Display the file name
                        OutputLabel.Text += $"{zipArchiveEntry.Name}\r\n";
    
                        // Open the file
                        using (var archiveEntryStream = zipArchiveEntry.Open())
                        {
                            // Writing the file's contents to the UI
                            using (var reader = new StreamReader(archiveEntryStream))
                            {
                                OutputLabel.Text += $"{reader.ReadToEnd()}\r\n\n";
                            }
                        }
                    }
                }
            }
        }
    }

    In that example, I was able to open and use the file contents without needing to first extract and save to a new directory. See RadZipLibrary Getting Started for more information.

    The demo uses the Telerik NuGet server to add the Telerik.UI.for.Xamarin package. If you don't already have it as a package source, visit the Telerik NuGet Server documentation.

    Further Assistance

    If you have any trouble, please open a Support Ticket here and share the code that you're using. This will allow us to directly investigate and offer possible solutions.

    Regards,
    Lance | Team Lead - US DevTools Support
    Progress Telerik

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. Sébastien
    Sébastien avatar
    10 posts
    Member since:
    Dec 2018

    Posted 23 Dec 2019 Link to this post

    Thank you very much Lance for this detailed example :)
Back to Top