Last active
July 10, 2020 13:22
-
-
Save gsoulavy/a71bbce5f310e53e0698fe0cd11acaf3 to your computer and use it in GitHub Desktop.
SharpZipLib
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using ICSharpCode.SharpZipLib.Zip | |
using ICSharpCode.SharpZipLib.Zip.Compression | |
using ICSharpCode.SharpZipLib.Zip.Compression.Streams | |
using System.Threading.Tasks | |
public class CompressUtil | |
{ | |
public static async Task<byte[]> DecompressData(byte[] bytes) | |
{ | |
using var stream = new InflaterInputStream(new MemoryStream(bytes)); | |
using var memory = new MemoryStream(); | |
var buffer = new byte[4096]; | |
while (true) | |
{ | |
int size = await stream.ReadAsync(buffer, 0, buffer.Length); | |
if (size > 0) | |
{ | |
await memory.WriteAsync(buffer, 0, size); | |
} | |
else | |
break; | |
} | |
stream.Close(); | |
return memory.ToArray(); | |
} | |
public static async Task<byte[]> CompressData(byte[] data) | |
{ | |
using var memory = new MemoryStream(); | |
using var stream = new DeflaterOutputStream(memory, new Deflater()); | |
await stream.WriteAsync(data, 0, data.Length); | |
await stream.FlushAsync(); | |
stream.Close(); | |
return memory.ToArray(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment