Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Android: System.IO.Compression.ZipFile.ExtractToDirectory Fails in release mode

Everything works fine in debug mode but when we run it in release the ExtractToDirectory call fails.

Here is the function for reference. Just to make sure we are not doing anything weird.

private bool UnzipFiles()
    {
        bool toReturn = true;
        try
        {
            UpdateStatus("Almost done...");
            string file = Path.Combine (DownloadFolder, "ZipFile.zip");
            if(System.IO.Directory.Exists(UnzippingDestinationFolder))
            {
                System.IO.Directory.Delete(UnzippingDestinationFolder, recursive:true);
            }

            System.IO.Compression.ZipFile.ExtractToDirectory(file, UnzippingDestinationFolder);
            UpdateStatus("Finished!");
            var files = System.IO.Directory.GetFiles(UnzippingDestinationFolder);

            int m = 3;

        }
        catch (Exception e)
        {
            toReturn = false;
        }

Finally, here is the exception we get.

System.NullReferenceException: Object reference not set to an instance of an object
  at SharpCompress.Common.Zip.Headers.ZipFileEntry.DecodeString (System.Byte[] str) [0x00000] in <filename unknown>:0 
  at SharpCompress.Common.Zip.Headers.DirectoryEntryHeader.Read (System.IO.BinaryReader reader) [0x00000] in <filename unknown>:0 
  at SharpCompress.Common.Zip.ZipHeaderFactory.ReadHeader (UInt32 headerBytes, System.IO.BinaryReader reader) [0x00000] in <filename unknown>:0 
  at SharpCompress.Common.Zip.SeekableZipHeaderFactory+<ReadSeekableHeader>c__Iterator0.MoveNext () [0x00000] in <filename unknown>:0 
  at SharpCompress.Archive.Zip.ZipArchive+<LoadEntries>c__Iterator0.MoveNext () [0x00000] in <filename unknown>:0 
  at SharpCompress.LazyReadOnlyCollection`1+LazyLoader[SharpCompress.Archive.Zip.ZipArchiveEntry].MoveNext () [0x00000] in <filename unknown>:0 
  at System.IO.Compression.ZipArchive.CreateZip (System.IO.Stream stream, ZipArchiveMode mode) [0x00000] in <filename unknown>:0 
  at System.IO.Compression.ZipArchive..ctor (System.IO.Stream stream, ZipArchiveMode mode, Boolean leaveOpen, System.Text.Encoding entryNameEncoding) [0x00000] in <filename unknown>:0 
  at System.IO.Compression.ZipFile.Open (System.String archiveFileName, ZipArchiveMode mode, System.Text.Encoding entryNameEncoding) [0x00000] in <filename unknown>:0 
  at System.IO.Compression.ZipFile.ExtractToDirectory (System.String sourceArchiveFileName, System.String destinationDirectoryName, System.Text.Encoding entryNameEncoding) [0x00000] in <filename unknown>:0 
  at System.IO.Compression.ZipFile.ExtractToDirectory (System.String sourceArchiveFileName, System.String destinationDirectoryName) [0x00000] in <filename unknown>:0 
  at NewBaron.Screens.DownloadContentScreen.UnzipFiles () [0x00000] in <filename unknown>:0 
like image 306
stitch1280 Avatar asked Jul 14 '15 23:07

stitch1280


2 Answers

A slight changes to Victor's solution. Not linking the SDKs generated an apk that was 53MBs. Too large for the play store's apk size limit.

I set the linking behavior to link SDK assemblies only and it brought the apk size down to 29MBs

Here is the updated window.

enter image description here

like image 117
stitch1280 Avatar answered Sep 28 '22 00:09

stitch1280


@mattewrobbinsdev's suggestion was exactly it. For future readers, here's the dialog in Xamarin Studio:

Dialog

like image 39
Victor Chelaru Avatar answered Sep 27 '22 23:09

Victor Chelaru