ใช้คลาส GZipStream สำหรับบีบอัดและแตกไฟล์แบบ GZIP
บีบอัด (Compress)
ใช้ GZipStream ร่วม FileStream ระบุ: ไฟล์ต้นทาง และชื่อไฟล์ zip สำหรับ output
ตัวอย่าง
using(var compress = new GZipStream(outputFile, CompressionMode.Compress, false)) {
byte[] b = new byte[inFile.Length];
int read = inFile.Read(b, 0, b.Length);
while (read > 0) {
compress.Write(b, 0, read);
read = inFile.Read(b, 0, b.Length);
}
}
แตกไฟล์ (Decompress)
ใช้ GZipStream เดียวกัน ระบุ: ไฟล์ต้นทาง zip และชื่อไฟล์ output
using (var zip = new GZipStream(inStream, CompressionMode.Decompress, true))
วนลูปอ่านจนกว่าจะไม่มีข้อมูล เขียนไปยัง output stream ได้ไฟล์ที่แตกแล้ว
ตัวอย่าง
using(var zip = new GZipStream(inputStream, CompressionMode.Decompress, true)) {
byte[] b = new byte[inputStream.Length];
while (true) {
int count = zip.Read(b, 0, b.Length);
if (count != 0) outputStream.Write(b, 0, count);
if (count != b.Length) break;
}
}