Suchith Suchith - 3 months ago 10
C# Question

Unzip folder reccursively in Android Xamarin mono 3.2.6

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.

Answer

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

Comments