利用.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();
            }
        }
    }
}

转载于:https://www.cnblogs.com/SweetGan/archive/2011/06/20/2085116.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值