Monday, October 18, 2010

Compression With C#, Part 2 - Files

Today I want to continue the tutorial regarding compression methods in C# from the previous post.
While in the previous post I explained the bascis of compressing / decompressing simple strings, I show today an advanced use to compress / decompress whole files.

Compressing:
As in the compression of strings we create a new instance of the class GZipStream and set in its constructor a FileStream pointing to the target file.
Furthermore we create another FileStream pointing to the source file. With it we now read the content of the old file to a byte array.
If we write this then with the function Write() of the GZipstream, this writes the compressed content of the source file to the target file - we have compressed the file, just like WinZip or a similiar program would do.
The corresponding code:

        private void CompressFile(string normalFile, string compressedFile)
        {
            GZipStream CompressStream = new GZipStream(new FileStream(compressedFile, FileMode.Create), CompressionMode.Compress);
           
            FileStream NormalFileStream = new FileStream(normalFile, FileMode.Open);
            byte[] Content = new byte[NormalFileStream.Length];
            NormalFileStream.Read(Content, 0, Content.Length);
            NormalFileStream.Close();

            CompressStream.Write(Content, 0, Content.Length);
            CompressStream.Close();
        }

Now to the decompression:
Therefor we hand a FileStream over to the GZipStream, which points to the compressed file which is to be readout.
Furthermore a new FileStream is created, which points to the decompressed file which is to be written.
As also in the previous post we use a loop to read the compressed file.
Therefor we use the function Read() of the GZipStream, which reads the designated number of bytes and then decompresses them. The result is saved in a buffer, which is then written via a FileStream to the new file.
If the end of the file is reached, the function Read() reads less bytes than fit in the buffer and the program notices the end.
The compressed file was now decompressed and written to a readable file again.
The corresponding code is:

        private void DecompressFile(string compressedFile, string normalFile)
        {
            GZipStream DecompressStream = new GZipStream(new FileStream(compressedFile, FileMode.Open), CompressionMode.Decompress);

            FileStream NormalFileStream = new FileStream(normalFile, FileMode.Create);

            int BytesReadCount = 0;
            byte[] Buffer = new byte[4096];

            while (true)
            {
                BytesReadCount = DecompressStream.Read(Buffer, 0, Buffer.Length);
                if (BytesReadCount != 0)
                {
                    NormalFileStream.Write(Buffer, 0, BytesReadCount);
                }
                if (BytesReadCount < Buffer.Length)
                    break;
            }

            NormalFileStream.Close();
            DecompressStream.Close();

        }

No comments:

Post a Comment