Skip to content

Instantly share code, notes, and snippets.

@yutopio
Created August 1, 2016 04:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yutopio/0c7a908473cc55e9317cf680471dcd38 to your computer and use it in GitHub Desktop.
Save yutopio/0c7a908473cc55e9317cf680471dcd38 to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
public static class ZipUtil
{
public static byte[] Compress(this Dictionary<string, byte[]> entries)
{
using (var stream = new MemoryStream())
{
using (var archive = new ZipArchive(stream, ZipArchiveMode.Create))
{
foreach (var entry in entries)
{
var e = archive.CreateEntry(entry.Key);
using (var s = e.Open())
{
s.Write(entry.Value, 0, entry.Value.Length);
}
}
}
return stream.ToArray();
}
}
public static Dictionary<string, byte[]> Decompress(this byte[] buffer)
{
var ret = new Dictionary<string, byte[]>();
using (var stream = new MemoryStream(buffer))
using (var archive = new ZipArchive(stream, ZipArchiveMode.Read))
{
foreach (var entry in archive.Entries)
{
var name = entry.FullName;
var data = new byte[entry.Length];
using (var s = entry.Open())
{
for (var count = 0; count < data.Length;)
{
count += s.Read(data, count, data.Length - count);
}
}
ret.Add(name, data);
}
}
return ret;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment