C
C#2y ago
Alex

❔ Invalid Data Exception when extracting files

I am getting a System.IO.InvalidDataException: Found invalid data while decoding. error while running this line...
ZipFile.ExtractToDirectory(InternalFileManager.INSTANCE.QueryFilePath(filename), InternalFileManager.INSTANCE.QueryDirectoryPath("tmp", true));
ZipFile.ExtractToDirectory(InternalFileManager.INSTANCE.QueryFilePath(filename), InternalFileManager.INSTANCE.QueryDirectoryPath("tmp", true));
Though I have no idea why. The file that I am extracting is one that has been downloaded, with the following code: https://pastecode.io/s/2rhskczr Is there something i'm missing here?
9 Replies
mtreit
mtreit2y ago
You are doing bad things with async code... Your file is probably corrupted. Are you not getting compiler warnings for doing things like this without an await?
fileStream.WriteAsync(buffer, 0, byteChunk);
fileStream.WriteAsync(buffer, 0, byteChunk);
Your use of .Result everywhere is bad. Don't use the async versions of those methods if you aren't going to actually write async code. If this is supposed to be a zip file, does using another mechanism for extracting the file(s) work? For instance, using Expand-Archive from PowerShell
Alex
AlexOP2y ago
Got it. I thought there were only the async methods, and I can't have this be async as of now I'll come back with results not really! Oh yeah, I didn't try that. It is corrupt.
mtreit
mtreit2y ago
Yeah you aren't waiting for the async write to finish before looping so it could be still in progress when you do another write on top of it. Just make everything sync, or make your method async. And use await appropriately.
Alex
AlexOP2y ago
Yeah it works now. I read up on some resources about Async and I figured out a solution for it. It doesn't work like I thought it did. Thank you!
mtreit
mtreit2y ago
Well, shameless plug, last week this discord hosted the Solution1 conference and I gave a talk on the topic of async code (among other things) that you might find helpful: https://youtu.be/8lUs9ukVrFY
Alex
AlexOP2y ago
Oh that's amazing!!! Do you happen to know the timestamp for the asyncio talk? That's the one where I have the most trouble on Forgive me but, ping @mtreit I'm currently doing aerosol so I can't really move, might aswell watch
mtreit
mtreit2y ago
The second half of the talk is basically about async
Alex
AlexOP2y ago
I see, thank you!
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server