使用ICSharpCode.SharpZipLib进行文件压缩,目前还没实现加密压缩/解压缩。

using System;
using System.IO;
using System.Data;

using ICSharpCode.SharpZipLib;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using ICSharpCode.SharpZipLib.Checksums;

using Msse.DocShare.NR_System;

namespace Msse.DocShare.Spl.Public
{
    public class zip
    {
        public static void UnZip( error oe,string strFile, string strDir )
        {
            #region 说明
            ///
            /// 功能:将一个压缩文件解压缩到一个目录下
            ///
            /// 参数:
            ///   strFile:压缩文件
            ///   strDir:解压缩目录
            ///
            /// 返回:
            ///   无
            ///
            #endregion
            if (strDir == "")    strDir = Directory.GetCurrentDirectory();
            if (!strDir.EndsWith(@"/"))    strDir = strDir + @"/";

            ZipInputStream s = new ZipInputStream(File.OpenRead(strFile));
            ZipEntry theEntry;

            while ((theEntry = s.GetNextEntry()) != null)
            {
                string directoryName = "";
                string pathToZip = "";
                pathToZip = theEntry.Name;

                if (pathToZip != "")
                    directoryName = Path.GetDirectoryName(pathToZip) + @"/";
                string fileName = Path.GetFileName(pathToZip);
                Directory.CreateDirectory(strDir + directoryName);
                if (fileName != "")
                {
                    FileStream streamWriter = File.Create(strDir + directoryName + fileName);
                    int size = 2048;
                    byte[] data = new byte[2048];
                    while (true)
                    {
                        size = s.Read(data, 0, data.Length);
                        if (size > 0)
                            streamWriter.Write(data, 0, size);
                        else
                            break;
                    }
                    streamWriter.Close();
                }
            }
            s.Close();
        }

        public static void ZipFile( error oe,string strSrcFile,string strZipFile )
        {
            #region 说明
            ///
            /// 功能:压缩一个文件,不包含路径信息
            ///
            /// 参数:
            ///   strSrcFile:待压缩文件
            ///   strZipFile: 压缩后文件
            ///
            /// 返回:
            ///   无
            ///
            #endregion
            string strFileName = Path.GetFileName(strSrcFile);// 文件名,不含路径信息

            #region 读取文件信息
            FileStream fs = File.OpenRead(strSrcFile);
            long iLength = fs.Length;// 文件长度
            byte[] buffer = new byte[iLength];
            fs.Read(buffer, 0, buffer.Length);
            fs.Close();
            #endregion

            System.IO.FileStream fsZipFile = System.IO.File.Create(strZipFile);
            ZipOutputStream outStream = new ZipOutputStream(fsZipFile);
            ZipEntry entry = new ZipEntry( strFileName);            
            entry.CompressionMethod = CompressionMethod.Deflated; // deflate
            entry.DateTime = DateTime.Now;
            entry.Size = iLength;                
            #region CRC校验
            Crc32 crc = new Crc32();
            crc.Update(buffer);            
            entry.Crc  = crc.Value;            
            #endregion
            outStream.PutNextEntry(entry);            
            outStream.Write(buffer, 0, buffer.Length);    
            outStream.Finish();
            outStream.Close();
        }

        #region 压缩多个文件
        public static void ZipFile(    error oe,string[] saSrcFile,string strZipFile )
        {
            #region 说明
            ///
            /// 功能:将多个文件压缩成一个文件
            ///
            /// 参数:
            ///   saSrcFile:多个文件,采用全路径,例:e:/tmp/tmp1/DD.cs
            ///
            #endregion

            ZipOutputStream outStream = new ZipOutputStream(File.Create(strZipFile));
            
            Crc32 crc = new Crc32();            

            foreach (string strSrcFile in saSrcFile)
            {
                ZipFile(strSrcFile,crc,outStream);
            }

            outStream.Finish();
            outStream.Close();
        }
        private static void ZipFile(string strSrcFile,Crc32 crc,ZipOutputStream outStream )
        {
            #region 说明
            ///
            /// 功能:压缩一个文件,不包含路径信息
            ///
            /// 参数:
            ///   strSrcFile:待压缩文件
            ///   strZipFile: 压缩后文件
            ///
            ///
            #endregion
            string strFileName = Path.GetFileName(strSrcFile);// 文件名,不含路径信息

            #region 读取文件信息
            FileStream fs = File.OpenRead(strSrcFile);
            long iLength = fs.Length;// 文件长度
            byte[] buffer = new byte[iLength];
            fs.Read(buffer, 0, buffer.Length);
            fs.Close();
            #endregion

            ZipEntry entry = new ZipEntry( strFileName);            
            entry.CompressionMethod = CompressionMethod.Deflated; // deflate
            entry.DateTime = DateTime.Now;
            entry.Size = iLength;                
            #region CRC校验
            crc.Reset();
            crc.Update(buffer);            
            entry.Crc  = crc.Value;            
            #endregion
            outStream.PutNextEntry(entry);            
            outStream.Write(buffer, 0, buffer.Length);    
        }

        #endregion    
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值