How to compress and decompress file

来源:互联网 发布:淘宝优惠券赚佣金平台 编辑:程序博客网 时间:2024/05/19 13:45

using System.IO;
using System.IO.Compression;
using System.Text;

Here are sample functions to compress and decompress a file:

private void TestCompress()
{
    string srcFile = "C://temp//file-to-compress.txt";
    string dstFile = "C://temp//compressed-file.gzip";

    FileStream fsIn = null; // will open and read the srcFile
    FileStream fsOut = null; // will be used by the GZipStream for output to the dstFile
    GZipStream gzip = null;
    byte[] buffer;
    int count = 0;

    try
    {
        fsOut = new FileStream(dstFile, FileMode.Create, FileAccess.Write, FileShare.None);
        gzip = new GZipStream(fsOut, CompressionMode.Compress, true);

        fsIn = new FileStream(srcFile, FileMode.Open, FileAccess.Read, FileShare.Read);
        buffer = new byte[fsIn.Length];
        count = fsIn.Read(buffer, 0, buffer.Length);
        fsIn.Close();
        fsIn = null;

        // compress to the destination file
        gzip.Write(buffer, 0, buffer.Length);
    }
    catch (Exception ex)
    {
        // handle or display the error
        System.Diagnostics.Debug.Assert(false, ex.ToString());
    }
    finally
    {
        if (gzip != null)
        {
            gzip.Close();
            gzip = null;
        }
        if (fsOut != null)
        {
            fsOut.Close();
            fsOut = null;
        }
        if (fsIn != null)
        {
            fsIn.Close();
            fsIn = null;
        }
    }
}


private void TestDecompress()
{
    string srcFile = "C://temp//compressed-file.gzip";
    string dstFile = "C://temp//decompressed-file.txt";

    FileStream fsIn = null; // will open and read the srcFile
    FileStream fsOut = null; // will be used by the GZipStream for output to the dstFile
    GZipStream gzip = null;
    const int bufferSize = 4096;
    byte[] buffer = new byte[bufferSize];
    int count = 0;

    try
    {
        
        fsIn = new FileStream(srcFile, FileMode.Open, FileAccess.Read, FileShare.Read);
        fsOut = new FileStream(dstFile, FileMode.Create, FileAccess.Write, FileShare.None);
        gzip = new GZipStream(fsIn, CompressionMode.Decompress, true);
        while (true)
        {
            count = gzip.Read(buffer, 0, bufferSize);
            if (count != 0)
            {
                fsOut.Write(buffer, 0, count);
            }
            if (count != bufferSize)
            {
                // have reached the end
                break;
            }
        }
    }
    catch (Exception ex)
    {
        // handle or display the error
        System.Diagnostics.Debug.Assert(false, ex.ToString());
    }
    finally
    {
        if (gzip != null)
        {
            gzip.Close();
            gzip = null;
        }
        if (fsOut != null)
        {
            fsOut.Close();
            fsOut = null;
        }
        if (fsIn != null)
        {
            fsIn.Close();
            fsIn = null;
        }
    }
}

原创粉丝点击