adamhathcock / sharpcompress

SharpCompress is a fully managed C# library to deal with many compression types and formats.
MIT License
2.25k stars 479 forks source link

Bookshare zip files don't uncompress #283

Open ivanicin opened 7 years ago

ivanicin commented 7 years ago

Bookshare is a service that distribute books in .zip files (DAISY format). Those files aren't recognized as the proper zip archive by the current version of Sharpcompress. Interesting enough, this related project reads them properly on Windows: https://github.com/p3root/sharpcompress

I can send you a sample for testing on email, or you can get it by registering to: http://developer.bookshare.org, then find test credentials and available books for test here (after you sign in): http://developer.bookshare.org/docs/read/Bookshare_Test_Data_for_Partners, and then log in to Bookshare and download the book (this link will lead you to one that is available for download for tests: https://www.bookshare.org/search?keyword=Bookshare+Demo%3A+The+Rocket+Boys+of+NIH).

ivanicin commented 7 years ago

Just to note that when using System.IO.Compression I also get the exception: System.IO.InvalidDataException: End of Central Directory record could not be found.

However the file opens without the problem in the explorer and as said above that somewhat obsolete version of this project opens it without the problem. Any ideas why this is happening and if I can do something Adam?

adamhathcock commented 7 years ago

Zip format has become a bit haphazard over the years. The official implementation requires a valid central directory but it’s possible to extract sometimes without it.

It depends on what those files have in it. I haven’t looked yet.

ivanicin commented 7 years ago

After some checking my initial report wasn't fully right, the problem is caused by different platforms behaving differently. This works properly with Windows, but on Xamarin.iOS it doesn't.

adamhathcock commented 7 years ago

You’ll have to show me code that works on windows but not xamarin along with exceptions or whatever.

ivanicin commented 7 years ago
            using (Stream zipMemoryStream = await file.OpenAsync(PCLStorage.FileAccess.Read))
            {
                using (var zipArchive = SharpCompress.Archives.ArchiveFactory.Open(zipMemoryStream))
                {
                }
             }

It crashes on the second line on Xamarin with exception System.InvalidOperationException: Cannot determine compressed stream type. Supported Archive Formats: Zip, GZip, Tar, Rar, 7Zip, LZip at SharpCompress.Archives.ArchiveFactory.Open (System.IO.Stream stream, SharpCompress.Readers.ReaderOptions readerOptions) [0x000de] in <714c60c556704f94af7b2094897f5d2b>:0

adamhathcock commented 7 years ago

That's weird. I would expect some error reading from the file system.

If you know the file is a zip, try using a ZipArchive from ZipArchiveFactory directly to avoid the detection issue just as a test.

ivanicin commented 7 years ago

Also of note, it works on Xamarin.Mac properly. I guess that there is something wrong in the Xamarin.iOS framework, not sure if you can fix this (maybe just workaround the problem), I have also submitted the problem regarding System.IO.Compression to Xamarin.

I'll check later what happens if I do like you have advised.

adamhathcock commented 7 years ago

System.IO.Compression isn’t used in the code you showed me?

The funny part is is that Mono uses SharpCompress for their zip implementation last I checked. Not sure what issues you’re having.

ivanicin commented 7 years ago

No I just pointed out that there is also issue there. Actually I have tried yesterday several more zip packages and every single of them had issues on iOS with this file. As said I believe it is a deeper problem in the framework just there might be some workaround.

ivanicin commented 7 years ago

Adam,

using (var archive = SharpCompress.Archives.Zip.ZipArchive.Open(fileStream))

did the trick. Thanks! I leave this open because it is a bug and you should try to fix it, but regarding my issue, it is resolved with this workaround.

adamhathcock commented 7 years ago

It's weird the autodetection doesn't work on iOS. Maybe something to do with improper read counting from a Stream like https://github.com/adamhathcock/sharpcompress/issues/275

Wufus commented 3 years ago

After creating ZipArchive with sharpcompress, i cant open this archive with sharpcompress.

End of Central Directory record could not be found.