CodeForge QQ客服 CodeForge 400电话 客服电话 4006316121
bc... [密码]
首页 » 源代码 » C#使用ICSharpCode.SharpZipLib.dll压缩文件夹和文件

C#使用ICSharpCode.SharpZipLib.dll压缩文件夹和文件

zhoulj
发布于2018-01-12 16:57:51
源码作者
浏览次数:
下载次数:0
下载所需积分:1 
源码分类 Tags:
源码分类 所属分类:
c#c# AllAll

分享有礼! 》

  • 请点击右侧的分享按钮,把本代码分享到各社交媒体。
  • 通过您的分享链接访问Codeforge,每来2个新的IP,您将获得0.1 积分的奖励。
  • 通过您的分享链接,每成功注册一个用户,该用户在Codeforge上所获得的每1个积分,您都将获得0.2 积分的分成奖励。

代码介绍

///
/// Zip压缩与解压缩
///
publicclassZipHelper
{
    ///
    /// 压缩单个文件
    ///
    /// 要压缩的文件
    /// 压缩后的文件
    /// 压缩等级
    /// 每次写入大小
    publicstaticvoidZipFile(stringfileToZip,stringzipedFile,intcompressionLevel,intblockSize)
    {
        //如果文件没有找到,则报错
        if(!System.IO.File.Exists(fileToZip))
        {
            thrownewSystem.IO.FileNotFoundException("指定要压缩的文件: "+ fileToZip +" 不存在!");
        }
 
        using(System.IO.FileStream ZipFile = System.IO.File.Create(zipedFile))
        {
            using(ZipOutputStream ZipStream =newZipOutputStream(ZipFile))
            {
                using(System.IO.FileStream StreamToZip =newSystem.IO.FileStream(fileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read))
                {
                    stringfileName = fileToZip.Substring(fileToZip.LastIndexOf("\\") + 1);
 
                    ZipEntry ZipEntry =newZipEntry(fileName);
 
                    ZipStream.PutNextEntry(ZipEntry);
 
                    ZipStream.SetLevel(compressionLevel);
 
                    byte[] buffer =newbyte[blockSize];
 
                    intsizeRead = 0;
 
                    try
                    {
                        do
                        {
                            sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);
                            ZipStream.Write(buffer, 0, sizeRead);
                        }
                        while(sizeRead > 0);
                    }
                    catch(System.Exception ex)
                    {
                        throwex;
                    }
 
                    StreamToZip.Close();
                }
 
                ZipStream.Finish();
                ZipStream.Close();
            }
 
            ZipFile.Close();
        }
    }
 
    ///
    /// 压缩单个文件
    ///
    /// 要进行压缩的文件名
    /// 压缩后生成的压缩文件名
    publicstaticvoidZipFile(stringfileToZip,stringzipedFile)
    {
        //如果文件没有找到,则报错
        if(!File.Exists(fileToZip))
        {
            thrownewSystem.IO.FileNotFoundException("指定要压缩的文件: "+ fileToZip +" 不存在!");
        }
 
        using(FileStream fs = File.OpenRead(fileToZip))
        {
            byte[] buffer =newbyte[fs.Length];
            fs.Read(buffer, 0, buffer.Length);
            fs.Close();
 
            using(FileStream ZipFile = File.Create(zipedFile))
            {
                using(ZipOutputStream ZipStream =newZipOutputStream(ZipFile))
                {
                    stringfileName = fileToZip.Substring(fileToZip.LastIndexOf("\\") + 1);
                    ZipEntry ZipEntry =newZipEntry(fileName);
                    ZipStream.PutNextEntry(ZipEntry);
                    ZipStream.SetLevel(5);
 
                    ZipStream.Write(buffer, 0, buffer.Length);
                    ZipStream.Finish();
                    ZipStream.Close();
                }
            }
        }
    }
 
    ///
    /// 压缩多层目录
    ///
    /// The directory.
    /// The ziped file.
    publicstaticvoidZipFileDirectory(stringstrDirectory,stringzipedFile)
    {
        using(System.IO.FileStream ZipFile = System.IO.File.Create(zipedFile))
        {
            using(ZipOutputStream s =newZipOutputStream(ZipFile))
            {
                ZipSetp(strDirectory, s,"");
            }
        }
    }
 
    ///
    /// 递归遍历目录
    ///
    /// The directory.
    /// The ZipOutputStream Object.
    /// The parent path.
    privatestaticvoidZipSetp(stringstrDirectory, ZipOutputStream s,stringparentPath)
    {
        if(strDirectory[strDirectory.Length - 1] != Path.DirectorySeparatorChar)
        {
            strDirectory += Path.DirectorySeparatorChar;
        }          
        Crc32 crc =newCrc32();
 
        string[] filenames = Directory.GetFileSystemEntries(strDirectory);
 
        foreach(stringfileinfilenames)// 遍历所有的文件和目录
        {
 
            if(Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
            {
                stringpPath = parentPath;
                pPath += file.Substring(file.LastIndexOf("\\") + 1);
                pPath +="\\";
                ZipSetp(file, s, pPath);
            }
 
            else// 否则直接压缩文件
            {
                //打开压缩文件
                using(FileStream fs = File.OpenRead(file))
                {
 
                    byte[] buffer =newbyte[fs.Length];
                    fs.Read(buffer, 0, buffer.Length);
 
                    stringfileName = parentPath + file.Substring(file.LastIndexOf("\\") + 1);
                    ZipEntry entry =newZipEntry(fileName);
 
                    entry.DateTime = DateTime.Now;
                    entry.Size = fs.Length;
 
                    fs.Close();
 
                    crc.Reset();
                    crc.Update(buffer);
 
                    entry.Crc = crc.Value;
                    s.PutNextEntry(entry);
 
                    s.Write(buffer, 0, buffer.Length);
                }
            }
        }
    }
 
    ///
    /// 解压缩一个 zip 文件。
    ///
    /// The ziped file.
    /// The STR directory.
    /// zip 文件的密码。
    /// 是否覆盖已存在的文件。
    publicvoidUnZip(stringzipedFile,stringstrDirectory,stringpassword,booloverWrite)
    {
 
        if(strDirectory =="")
            strDirectory = Directory.GetCurrentDirectory();
        if(!strDirectory.EndsWith("\\"))
            strDirectory = strDirectory +"\\";
 
        using(ZipInputStream s =newZipInputStream(File.OpenRead(zipedFile)))
        {
            s.Password = password;
            ZipEntry theEntry;
 
            while((theEntry = s.GetNextEntry()) !=null)
            {
                stringdirectoryName ="";
                stringpathToZip ="";
                pathToZip = theEntry.Name;
 
                if(pathToZip !="")
                    directoryName = Path.GetDirectoryName(pathToZip) +"\\";
 
                stringfileName = Path.GetFileName(pathToZip);
 
                Directory.CreateDirectory(strDirectory + directoryName);
 
                if(fileName !="")
                {
                    if((File.Exists(strDirectory + directoryName + fileName) && overWrite) || (!File.Exists(strDirectory + directoryName + fileName)))
                    {
                        using(FileStream streamWriter = File.Create(strDirectory + directoryName + fileName))
                        {
                            intsize = 2048;
                            byte[] data =newbyte[2048];
                            while(true)
                            {
                                size = s.Read(data, 0, data.Length);
 
                                if(size > 0)
                                    streamWriter.Write(data, 0, size);
                                else
                                    break;
                            }
                            streamWriter.Close();
                        }
                    }
                }
            }
 
            s.Close();
        }
    }
 
}
Sponsored links

源码文件列表

温馨提示: 点击源码文件名可预览文件内容哦 ^_^
名称 大小 修改日期
程序员商城

资源评论

(提交有效评论获得积分)
评论内容不能少于15个字,不要超出160个字。
  • 1
  • 第1页
  • 共1页

C#使用ICSharpCode.SharpZipLib.dll压缩文件夹和文件 (72.88 kB)

需要 1 积分
您持有 积分

CodeForge积分(原CF币)全新升级,功能更强大,使用更便捷,不仅可以用来下载海量源代码马上还可兑换精美小礼品了 了解更多

您的积分不足,优惠套餐快速获取 30 积分

订单支付完成后,积分将自动加入到您的账号。以下是优惠期的人民币价格,优惠期过后将恢复美元价格。

更多付款方式:网银PayPal

上传代码,免费获取CodeForge积分

您本次下载所消耗的积分将转交上传作者。

同一源码,30天内重复下载,只扣除一次积分。

登录 CodeForge

还没有CodeForge账号? 立即注册
关注微博
联系客服

Switch to the English version?

Yes
CodeForge 英文版
No
CodeForge 中文版

完善个人资料,获价值¥30元积分奖励!

^_^"呃 ...

Sorry!这位大神很神秘,未开通博客呢,请浏览一下其他的吧
好的