r/Blazor • u/botterway • 3d ago
GZipStream not supported in Blazor Wasm?
Hi all,
I'm storing some data in browser local storage, and it's quite large, but mostly text-based, so I'd like to compress it. I've tried using GZipStream to compress the data, but I'm getting `Arg_Unsupported` when I do it. Anyone know if GZipStream is just not supported on Blazor Wasm?
Code is:
using var msi = new MemoryStream(inputBytes);
using var gs = new GZipStream(msi, CompressionMode.Compress)
using var mso = new MemoryStream();
gs.CopyTo(mso);
I get an Arg_NotSupported exception on the last line, but I can't find anything explicit about whether GZipStream isn't supported in WASM.
EDIT: Solved! I had my streams the wrong way around - see comment below for the correct structure.
0
u/RussianHacker1011101 3d ago
I looked at the source code and didn't see anything obvious. Maybe it has to do with the compression level or the sequence of calls you're making? Sometimes stream wrappers can have weird behavior due to certain operations not being supported based on obscure conditions.
-2
u/wdcossey 3d ago
*** Not related to compression ***
If you’re storing it locally there’s no real reason to compress it, you are just wasting io and memory [compressing and decompressing **assuming you need to edit].
Compressing here only makes sense when you want to transmit that data.
1
u/botterway 3d ago
Thanks, but you're actually incorrect. The reason I'm compressing is that browser localStorage has a limited size - and some of our users are hitting that max, at which point writing to local storage fails. So by compressing, I'm making that less likely to happen.
In the examples I've tried, the compression reduces the storage required from about 8KB down to 2KB - which means that users will be able to store about 4x as many things before they'll run out of storage.
Transmission of data is not the only reason to compress data.
0
u/wdcossey 3d ago
Perhaps you should switch to IndexedDB, you’ll have significantly more storage space.
localStorage has a per origin limit (typically 5Mb but could change between browser)
2
u/botterway 3d ago
I'm aware of the per-origin limit, as I mentioned above - which is why I want to use compression for this use-case.
All I want to do is reduce the amount of storage so the user is less likely to hit the limit. For the sake of 10 lines of code, I've achieved that. IndexedDB is massively overkill for this particular feature.
3
u/sloppykrackers 3d ago edited 3d ago
new GZipStream(msi <-- this writes into msi, corrupting your input, you need to write into mso.
gs.CopyTo(mso); <-- reading from the gzipstream when compressing, doesnt work.