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
Alex2y 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
Alex2y 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
Alex2y 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
Alex2y 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
More Posts
❔ VS crashes when i try to add web browserim using windows forms and when i try to add web browser visual studio crahes❔ [Homework] Console Game HelpHello! I'm a newbie to coding (CS Freshman) & I have this project to submit tomorrow (4th Dec.), inv✅ Newtonsoft.JSON correctly serializes data but erroneously leaves out arrays during deserializationMy simplified code is as follows: ```cs public class Building { public string K❔ IAsyncEnumerable and IQueriable unit testingI have an interface of the repository, which has a method Query<T>. This method should return IQuery❔ Generics with different input and output typeI'm writing a generic at the moment which it something like this: ```csharp T GetSomething<T, U>(U I❔ Can I use slice operator with loop index (remainder operator%)?I want to slice an array from start to end index but if end index exceeds the array length, loop it ❔ My ASP.NET project isn't launchingI just made templates for a Standalone Angular project and a ASP.NET 6 project but it seems like onl❔ Making properties accessible by both static and non-static methodsI had to make a static duplicate of a property, because I wanted it to be accessible by both non-sta❔ Windows Forms different controls but same event doing different code?```cs private void OnSecondsChange(object sender, EventArgs e) { textBoxUpTime.Text += $"{sender✅ What is better, in this case, Replace() or Substring()In Unity it seems that text input comes with a 'Zero Width Space Character' at the end, and that mes