Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > .NET для новичков > C# | GZip - сжатый больше обычного


Автор: VOX 15.12.2009, 09:08
Здравствуйте!
Код

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.IO.Compression;

namespace CS
{
  class Program
  {
    static void Main(string[] args)
    {
        CompressFile(@"c:\temp\test.txt", @"c:\temp\test.txt.gz");
        DecompressFile(@"c:\temp\test.txt.gz", @"c:\temp\test.txt.test");
    }

    static void CompressFile(string inFilename, 
                             string outFilename)
    {

      FileStream sourceFile = File.OpenRead(inFilename);
      FileStream destFile = File.Create(outFilename);

      // Create the Compressed stream
      GZipStream compStream =
        new GZipStream(destFile, CompressionMode.Compress);

      // Write the data
      int theByte = sourceFile.ReadByte();
      while (theByte != -1)
      {
        compStream.WriteByte((byte)theByte);
        theByte = sourceFile.ReadByte();
      }

      // Clean it up
      sourceFile.Close();
      compStream.Close();
      destFile.Close();
    }

    static void DecompressFile(string inFilename, string outFilename)
    {
      FileStream sourceFile = File.OpenRead(inFilename);
      FileStream destFile = File.Create(outFilename);

      // Create the Compressed stream
      GZipStream compStream =
        new GZipStream(sourceFile, CompressionMode.Decompress);

      // Write the data
      int theByte = compStream.ReadByte();
      while (theByte != -1)
      {
        destFile.WriteByte((byte)theByte);
        theByte = compStream.ReadByte();
      }
      // Clean it up
      sourceFile.Close();
      compStream.Close();
      destFile.Close();
      Console.WriteLine();
      Console.ReadKey();
    }
  }
}


Проблема в том, что гзип после сжатия весит больше чем не сжатый файл,
подскажите в чём может быть проблема?
Спасибо!

Автор: explorer 15.12.2009, 10:43
Вот интересно, если считывать по одному байту, то что сжимать-то??? Один байт, он и в Африке один байт, его сжать нельзя. 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);
        }
    }
}

Автор: VOX 15.12.2009, 13:20
ога, понял! Спасибо большое!!!

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)