データの圧縮と展開
Posted feedbacks - C#
C# で定番の #ziplib から GZip(Output|Input)Stream を使いました。コンパイラ通してませんが大きく間違えてることは無いと思いたい……。
see: #ziplib
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | using System;
using System.IO;
using System.Text;
using ICSharpCode.SharpZipLib.GZip;
static class GZipSample {
static byte[] Compress(string text) {
using (MemoryStream ms = new MemoryStream()) {
using (GZipOutputStream gz = new GZipOutputStream(ms))
using (StreamWriter sw = new StreamWriter(gz)) {
sw.Write(text);
}
return ms.ToArray();
}
}
static string Decompress(byte[] data) {
using (MemoryStream ms = new MemoryStream(ms)) {
using (GZipInputStream gz = new GZipInputStream(ms))
using (StreamReader sr = new StreamReader(gz)) {
return sr.ReadToEnd();
}
}
}
static void Main(String[] args) {
Console.WriteLine(Decompress(Compress("Hello world")));
}
}
|


mattsan
#8262()
Rating1/5=0.20
データを圧縮するcompress、展開するdecompressという関数やメソッドなどを書いてください。データはバイト列でもストリームでもそれ以外の形式でもOKです。
圧縮形式は問いませんが、できるだけ一般的なフォーマット(zip,lzhなど)でお願いします。
また、標準以外のライブラリを使う場合には出典の記載をお願いします。
「○○でも実用的な圧縮/展開プログラムがかけるんだぞ!」というのを、ぜひ示してください。
[ reply ]