Created
August 1, 2016 04:22
-
-
Save yutopio/0c7a908473cc55e9317cf680471dcd38 to your computer and use it in GitHub Desktop.
This file contains 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 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