2

I got a scenario where i need to Unzip the folder recursively. I have gone through the several links but not matching my requirements. I'm using mono 3.2.6 and need to extract recursively in android xamarin.

I used third party tool ICSharpCode.SharpZipLib.Zip. This was working fine in my application in debugging mode. But throws an error in Release mode. After APK installation in real device.

Even tried Dotnet.Zip but i'm unable unzip folder recursively. here suggested open source dll but it is concentrated on file rather then folder

Another link which shows the solution in java android, i'm looking for the same type in c# xamarin android. Is there any way to do this, even free third party tools are ok. can anybody suggest me the solution or hints are appreciated.

Community
  • 1
  • 1
Suchith
  • 1,276
  • 17
  • 39

2 Answers2

6

If it is throwing error in Release mode then it may be because of the linker. To zip and unzip files you can also use

java.util.zip

package. More info here.

Edit: Sample code

include namespace using Java.Util.Zip;

using ( ZipInputStream s = new ZipInputStream ( File.OpenRead ( strSourcePath ) ) )
{  
    ZipEntry theEntry;
    while ( ( theEntry = s.NextEntry ) != null )
    {
        string directoryName = Path.GetDirectoryName ( theEntry.Name );
        string fileName = Path.GetFileName ( theEntry.Name );
        directoryName = Path.Combine ( strDestFolderPath , directoryName );
        if ( directoryName.Length > 0 )
        {
            Directory.CreateDirectory ( directoryName );
        }
        if ( fileName != String.Empty )
        {
            using ( FileStream streamWriter = File.Create ( Path.Combine ( strDestFolderPath , theEntry.Name ) ) )
            {
                int size = 2048;
                byte [] data = new byte[size];
                while ( true )
                {
                    size = s.Read ( data , 0 , data.Length );
                    if ( size > 0 )
                    {
                        streamWriter.Write ( data , 0 , size );
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }
    } 
}

where strSourcePath is source zip file path and strDestFolderPath is destination folder path

thumbmunkeys
  • 20,606
  • 8
  • 62
  • 110
Chethan Shetty
  • 532
  • 6
  • 18
  • 1
    This worked for me, another solution is to add Change the linker Internalization setting to "west" option. Android->Build->Linker->Internalization-west – Suchith Nov 04 '14 at 08:41
0

You can also use the System.IO.Compression namespace.

 public static async Task ExtractToDirectory(string strSourcePath, string strDestFolderPath,
        IProgress<int> progessReporter)
    {

        await Task.Factory.StartNew(() =>
        {

            using (ZipArchive archive = new ZipArchive(File.Open(strSourcePath, FileMode.Open)))
            {
                double zipEntriesExtracted = 0;
                double zipEntries;

                zipEntries = archive.Entries.Count;

                foreach (ZipArchiveEntry entry in archive.Entries)
                {
                    try
                    {
                        string fullPath = Path.Combine(strDestFolderPath, entry.FullName);
                        if (String.IsNullOrEmpty(entry.Name))
                        {
                            Directory.CreateDirectory(fullPath);
                        }
                        else
                        {
                            var destFileName = Path.Combine(strDestFolderPath, entry.FullName);

                            using (var fileStream = File.Create(destFileName))
                            {
                                using (var entryStream = entry.Open())
                                {
                                    entryStream.CopyTo(fileStream);
                                }
                            }
                        }

                        zipEntriesExtracted++;
                        progessReporter.Report((int)((zipEntriesExtracted / zipEntries) * 100));
                    }
                    catch (Exception ex)
                    {

                    }
                }
            }

        }, TaskCreationOptions.LongRunning);


    }
smedasn
  • 1,249
  • 1
  • 13
  • 16