Open landrufang opened 1 year ago
I can confirm this on my end. It was encountered a few months ago porting some unit tests over to Avalonia.
The following code will not work for .zip files. It will work for all other file types tested (such as XML or TXT).
var assemblyName = Assembly.GetExecutingAssembly().GetName().Name;
var baseUri = "avares://" + assemblyName + "/TestFiles/Native/";
foreach (var fileName in fileNames)
{
using (Stream stream = AssetLoader.Open(new Uri(baseUri + fileName)))
{
// Test
}
}
one may need to seek the stream to pos 0
stream.Seek(0);
Good idea but resetting the stream position doesn't seem to fix this. That is already done in test code on my end using stream.Position = 0;
.
@robloo can you attach that test for us to double-check?
@timunie I wish but can't for a few reasons 1) it's testing IO of the apps native file format. 2) It goes deep into the internal workings of the app and object model.
Should be pretty easy for me to reproduce it though. I'll just zip a text file and try reading the contents.
I found that a "SlicedStream" is returned which is an Avalonia class. If I copy the data to a MemoryStream, it loads the zip file as expected. Don't know the root cause here but may give someone a clue where to look.
using (var stream = AssetLoader.Open(new Uri("avares://AvaloniaApplication1/Assets/Test.zip", UriKind.RelativeOrAbsolute)))
{
stream.Seek(0, SeekOrigin.Begin);
using (var ms = new MemoryStream())
{
stream.CopyTo(ms);
var zip = new ZipArchive(ms, ZipArchiveMode.Read);
Debug.WriteLine(zip.Entries.Count); // prints 1 as expected as we have one entry in our test zip
}
}
I found that a "SlicedStream" is returned which is an Avalonia class. If I copy the data to a MemoryStream, it loads the zip file as expected. Don't know the root cause here but may give someone a clue where to look.
using (var stream = AssetLoader.Open(new Uri("avares://AvaloniaApplication1/Assets/Test.zip", UriKind.RelativeOrAbsolute))) { stream.Seek(0, SeekOrigin.Begin); using (var ms = new MemoryStream()) { stream.CopyTo(ms); var zip = new ZipArchive(ms, ZipArchiveMode.Read); Debug.WriteLine(zip.Entries.Count); // prints 1 as expected as we have one entry in our test zip } }
yes , this is worked. there is somthing wrong in "SlicedStream". may be fix the "SlicedStream" is best way.
also use stream copyto cost a huge memory.
It feels like offset property should be adjusted here - https://github.com/AvaloniaUI/Avalonia/blob/master/src/Avalonia.Base/Platform/Internal/SlicedStream.cs#L24
by read the source code of ZipArchive. it's seems like not only the offset property cause this
@landrufang if you have an idea how to solve the issue, we'd be happy about a PR
@maxkatz6 this is not related to xaml but how AssetLoader works
Y this is a bug with SlicedStream where the offset the stream is created with is not taken into account when some position is read.
Describe the bug
i have a zip file in assets. when i try to read it and decompress, something exception hanppens. here is my code
Exception throws at
ZipArchive archive = new ZipArchive(stream)
Exception message:System.ArgumentOutOfRangeException:“Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection. Arg_ParamName_Name”
and here is stacktrace