Вот интересно, если считывать по одному байту, то что сжимать-то??? Один байт, он и в Африке один байт, его сжать нельзя. Zip-подобные алгоритмы работают со словарями. Поэтому читать нужно массив байт! Вот, хотя бы так:
| Код | using System.IO; using System.IO.Compression; using System.Text;
class Program { static void Main() { try { // 1. // Starting file is 26,747 bytes. string anyString = File.ReadAllText("TextFile1.txt");
// 2. // Output file is 7,388 bytes. CompressStringToFile("new.gz", anyString); } catch { // Couldn't compress. } }
public static void CompressStringToFile(string fileName, string value) { // A. // Write string to temporary file. string temp = Path.GetTempFileName(); File.WriteAllText(temp, value);
// B. // Read file into byte array buffer. byte[] b; using (FileStream f = new FileStream(temp, FileMode.Open)) { b = new byte[f.Length]; f.Read(b, 0, (int)f.Length); }
// C. // Use GZipStream to write compressed bytes to target file. using (FileStream f2 = new FileStream(fileName, FileMode.Create)) using (GZipStream gz = new GZipStream(f2, CompressionMode.Compress, false)) { gz.Write(b, 0, b.Length); } } }
|
|