用户操作
[即时聊天] [发私信] [加为好友]
ntshenwh
最近评论
hdnero:wow power leveling
文章分类
收藏
    相册
    存档
    软件项目交易
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes

    原创 有关.Net中使用压缩解压缩的问题补充(递归压缩文件夹)收藏

    新一篇: 使用程序备份服务器端数据库文件和其他文件(一) | 旧一篇: .Net中使用压缩和解压缩(三)

    对于之前发布的有关Net中使用压缩解压缩技术中,有个相当大的缺陷,是压缩文件时,不能递归压缩子文件夹。现将递归压缩的方法提供一下,还望大家多多探讨。(该代码参考别人写的例子修改了一下)

    注意:请引用以下类库

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    using System.DirectoryServices;

    using ICSharpCode.SharpZipLib.Zip;
    using ICSharpCode.SharpZipLib.GZip;
    using ICSharpCode.SharpZipLib.Checksums;

     

     public class ZipHelper
     {

        .........

          #region 压缩文件夹,支持递归
           
            /// <summary>
            /// 压缩文件夹
            /// </summary>
            /// <param name="dir">待压缩的文件夹</param>
            /// <param name="targetFileName">压缩后文件路径(包括文件名)</param>
            /// <param name="recursive">是否递归压缩</param>
            /// <returns></returns>
            public static bool Compress(string dir, string targetFileName,bool recursive)
            {
                //如果已经存在目标文件,询问用户是否覆盖
                if (File.Exists(targetFileName))
                {
                    if (!_ProcessOverwrite(targetFileName))
                        return false;
                }

                if (recursive == false)
                    return Compress(dir, targetFileName);

              
                FileStream ZipFile;
                ZipOutputStream ZipStream;

                //open
                ZipFile = File.Create(targetFileName);
                ZipStream = new ZipOutputStream(ZipFile);

                if (dir != String.Empty)
                {
                    _CompressFolder(dir, ZipStream, dir);
                }

                //close
                ZipStream.Finish();
                ZipStream.Close();

                if (File.Exists(targetFileName))
                    return true;
                else
                    return false;
            }

            /// <summary>
            /// 压缩某个子文件夹
            /// </summary>
            /// <param name="basePath"></param>
            /// <param name="zips"></param>
            /// <param name="zipfolername"></param>    
            private static void _CompressFolder(string basePath, ZipOutputStream zips, string zipfolername)
            {
                if (File.Exists(basePath))
                {
                    _AddFile(basePath, zips, zipfolername);
                    return;
                }
                string[] names = Directory.GetFiles(basePath);
                foreach (string fileName in names)
                {
                    _AddFile(fileName, zips, zipfolername);
                }

                names = Directory.GetDirectories(basePath);
                foreach (string folderName in names)
                {
                    _CompressFolder(folderName, zips, zipfolername);
                }

            }

            /// <summary>
            /// 压缩某个子文件
            /// </summary>
            /// <param name="fileName"></param>
            /// <param name="zips"></param>
            /// <param name="zipfolername"></param>
            private static void _AddFile(string fileName, ZipOutputStream zips, string zipfolername)
            {
                if (File.Exists(fileName))
                {
                    _CreateZipFile(fileName,zips,zipfolername);
                }
            }

            /// <summary>
            /// 压缩单独文件
            /// </summary>
            /// <param name="FileToZip"></param>
            /// <param name="zips"></param>
            /// <param name="zipfolername"></param>
            private static void _CreateZipFile(string FileToZip, ZipOutputStream zips,string zipfolername)
            {
                try
                {
                    FileStream StreamToZip = new FileStream(FileToZip, FileMode.Open, FileAccess.Read);
                    string temp = FileToZip;
                    string temp1 = zipfolername;
                    if (temp1.Length > 0)
                    {
                        int i = temp1.LastIndexOf('\\') + 1;
                        int j = temp.Length - i;
                        temp = temp.Substring(i, j);
                    }
                    ZipEntry ZipEn = new ZipEntry(temp);

                    zips.PutNextEntry(ZipEn);
                    byte[] buffer = new byte[16384];
                    System.Int32 size = StreamToZip.Read(buffer, 0, buffer.Length);
                    zips.Write(buffer, 0, size);
                    try
                    {
                        while (size < StreamToZip.Length)
                        {
                            int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);
                            zips.Write(buffer, 0, sizeRead);
                            size += sizeRead;
                        }
                    }
                    catch (System.Exception ex)
                    {
                        throw ex;
                    }

                    StreamToZip.Close();
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
            #endregion

       .........

    }

     

    发表于 @ 2007年12月20日 09:54:00|评论(loading...)|编辑

    新一篇: 使用程序备份服务器端数据库文件和其他文件(一) | 旧一篇: .Net中使用压缩和解压缩(三)

    评论:没有评论。

    发表评论  


    登录
    Csdn Blog version 3.1a
    Copyright © ntshenwh