haf / DotNetZip.Semverd

Please use System.IO.Compression! A fork of the DotNetZip project without signing with a solution that compiles cleanly. This project aims to follow semver to avoid versioning conflicts. DotNetZip is a FAST, FREE class library and toolset for manipulating zip files. Use VB, C# or any .NET language to easily create, extract, or update zip files.
Other
546 stars 218 forks source link

Ionic.Zip.ZipException: Cannot read that as a ZipFile #236

Open ahmadfraz1992 opened 3 years ago

ahmadfraz1992 commented 3 years ago

We are having issue on opening the zip file from .netstandard library project. We are working on xamarin.Android Current version Here is the code snippet.

Dictionary<string, byte[]> files = new Dictionary<string, byte[]>();
string keyname = "";
try
{
    using (var strm = new MemoryStream(byteArray))
    using (ZipFile z = ZipFile.Read(strm))
    {
        foreach (ZipEntry zEntry in z)
        {
            MemoryStream tempS = new MemoryStream();
            zEntry.ExtractWithPassword(tempS, password);
            files.Add(zEntry.FileName, tempS.ToArray());
        }
    }
}
catch (Exception ex)
{
}

When we try reading the ZipFile it generates an error. Following is the stack trace of exception.

at Ionic.Zip.ZipFile.ReadIntoInstance (Ionic.Zip.ZipFile zf) [0x00170] in C:\projects\dotnetzip-semverd\src\Zip.Shared\ZipFile.Read.cs:660

at Ionic.Zip.ZipFile.Read (System.IO.Stream zipStream, System.IO.TextWriter statusMessageWriter, System.Text.Encoding encoding, System.EventHandler`1[TEventArgs] readProgress) [0x00072] in C:\projects\dotnetzip-semverd\src\Zip.Shared\ZipFile.Read.cs:543

at Ionic.Zip.ZipFile.Read (System.IO.Stream zipStream) [0x00000] in C:\projects\dotnetzip-semverd\src\Zip.Shared\ZipFile.Read.cs:414

And the stack trace of inner exception is

at Ionic.Zip.SharedUtilities.StringFromBuffer (System.Byte[] buf, System.Text.Encoding encoding) [0x00000] in C:\projects\dotnetzip-semverd\src\Zip.Shared\Shared.cs:252

at Ionic.Zip.ZipEntry.ReadDirEntry (Ionic.Zip.ZipFile zf, System.Collections.Generic.Dictionary`2[TKey,TValue] previouslySeen) [0x0048d] in C:\projects\dotnetzip-semverd\src\Zip.Shared\ZipDirEntry.cs:277

at Ionic.Zip.ZipFile.ReadCentralDirectory (Ionic.Zip.ZipFile zf) [0x00089] in C:\projects\dotnetzip-semverd\src\Zip.Shared\ZipFile.Read.cs:724

Please let me know what i am doing wrong Is this the library bug.

jshergal commented 1 year ago

Do you have sample data you can include with this issue? For instance do you have a sample file or a dump of the byteArray that you are reading from? The exception is occurring when attempting to read the file name from the buffer and my guess it is related to the text encoding of the file name. Without sample data that reproduces the issue, it will be very difficult to track this down.

lalakii commented 7 months ago

I tried.

……
memoryStream.Seek(0, SeekOrigin.Begin);
ZipRead(memoryStream).ExtractAll(....) ```
……

It's working fine now, but I don't know why it's doing this.