位置:海鸟网 > IT > ASP.NET >

利用.Net实现压缩和解压缩文件

用.NET自带的Compression实现压缩和解压缩文件的源码如下:

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

namespace CompressionSample
{
    class ZipUtil
    {
        public void CompressFile(string iFile, string oFile)
        {
            //判断文件是否存在
            if (File.Exists(iFile) == false)
            {
                throw new FileNotFoundException("文件未找到!");
            }
            //创建文件流
            byte[] buffer = null;
            FileStream iStream = null;
            FileStream oStream = null;
            GZipStream cStream = null;
            try
            {
                //把文件写进数组
                iStream = new FileStream(iFile, FileMode.Open, FileAccess.Read, FileShare.Read);
                buffer = new byte[iStream.Length];
                int num = iStream.Read(buffer, 0, buffer.Length);
                if (num != buffer.Length)
                {
                    throw new ApplicationException("压缩文件异常!");
                }
                //创建文件输出流并输出
                oStream = new FileStream(oFile, FileMode.OpenOrCreate, FileAccess.Write);
                cStream = new GZipStream(oStream, CompressionMode.Compress, true);
                cStream.Write(buffer, 0, buffer.Length);
            }
            catch (ApplicationException ex)
            {
                MessageBox.Show(ex.Message, "压缩出现异常", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                //关闭流对象
                if (iStream != null) iStream.Close();
                if (cStream != null) cStream.Close();
                if (oStream != null) oStream.Close();
            }
        }

        public void DecompressFile(string iFile, string oFile)
        {
            //判断文件是否存在
            if (File.Exists(iFile) == false)
            {
                throw new FileNotFoundException("文件为找到!");
            }
            //创建文件流
            FileStream iStream = null;
            FileStream oStream = null;
            GZipStream dStream = null;
            byte[] qBuffer = new byte[4];
            try
            {
                //把压缩文件写入数组
                iStream = new FileStream(iFile, FileMode.Open);
                dStream = new GZipStream(iStream, CompressionMode.Decompress, true);
                int position = (int)iStream.Length - 4;
                iStream.Position = position;
                iStream.Read(qBuffer, 0, 4);
                iStream.Position = 0;
                int num = BitConverter.ToInt32(qBuffer, 0);
                byte[] buffer = new byte[num + 100];
                int offset = 0, total = 0;
                while (true)
                {
                    int bytesRead = dStream.Read(buffer, offset, 100);
                    if (bytesRead == 0) break;
                    offset += bytesRead;
                    total += bytesRead;
                }
                //创建输出流并输出
                oStream = new FileStream(oFile, FileMode.Create);
                oStream.Write(buffer, 0, total);
                oStream.Flush();
            }
            catch (ApplicationException ex)
            {
                MessageBox.Show(ex.Message, "解压缩出现异常", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                //关闭流对象
                if (iStream != null) iStream.Close();
                if (dStream != null) dStream.Close();
                if (oStream != null) oStream.Close();
            }
        }
    }
}