asp.net 实现文件的压缩和解压

如果该博客能给您带来帮助,请给博主一个评论谢谢!!话不多说下面请看具体的实现步骤。
1.首先在web项目中引用ICSharpCode.SharpZipLib.dll文件,可在博主的资源中下载。
2.具体的压缩和解压方法实现如下(代码中有详细的备注)

 /// <summary>
        /// 生成压缩文件
        /// </summary>
        /// <param name="strZipPath">生成的zip文件的路径</param>
        /// <param name="strZipTopDirectoryPath">源文件的上级目录</param>
        /// <param name="intZipLevel">T压缩等级</param>
        /// <param name="strPassword">压缩包解压密码</param>
        /// <param name="filesOrDirectoriesPaths">源文件路径</param>
        /// <returns></returns>
        private bool Zip(string strZipPath, string strZipTopDirectoryPath, int intZipLevel, string strPassword, string[] filesOrDirectoriesPaths)
        {
            try
            {
                List<string> AllFilesPath = new List<string>();//新建路径集合
                if (filesOrDirectoriesPaths.Length > 0) //得到原路径的所有文件
                {
                    for (int i = 0; i < filesOrDirectoriesPaths.Length; i++)
                    {
                        if (System.IO.File.Exists(filesOrDirectoriesPaths[i]))//检测如果存在的是文件
                        {
                            AllFilesPath.Add(filesOrDirectoriesPaths[i]);//添加到新路径集合中
                        }
                        else if (Directory.Exists(filesOrDirectoriesPaths[i]))//检测如果存在的是路径
                        {
                            GetDirectoryFiles(filesOrDirectoriesPaths[i], AllFilesPath);//将路径中的所有文件添加到新文件集合中
                        }
                    }
                }

                if (AllFilesPath.Count > 0)//验证文件集合长度是否大于0
                {

                    ZipOutputStream zipOutputStream = new ZipOutputStream(System.IO.File.Create(strZipPath));//新建压缩文件流
                    zipOutputStream.SetLevel(intZipLevel);//设定压缩文件等级
                    zipOutputStream.Password = strPassword;//设定压缩包解压缩密码

                    for (int i = 0; i < AllFilesPath.Count; i++)//获取列表集合
                    {
                        string strFile = AllFilesPath[i].ToString();//取出单个文件
                        try
                        {
                            if (strFile.Substring(strFile.Length - 1) == "") //folder检测文件长度
                            {
                                string strFileName = strFile.Replace(strZipTopDirectoryPath, "");//取出文件名称 去除前面的文件路径
                                if (strFileName.StartsWith(""))
                                {
                                    strFileName = strFileName.Substring(1);
                                }
                                ZipEntry entry = new ZipEntry(strFileName);//
                                entry.DateTime = DateTime.Now;
                                zipOutputStream.PutNextEntry(entry);
                            }
                            else //file
                            {
                                FileStream fs = System.IO.File.OpenRead(strFile);//读取文件

                                byte[] buffer = new byte[fs.Length];
                                fs.Read(buffer, 0, buffer.Length);

                                string strFileName = strFile.Replace(strZipTopDirectoryPath, "");
                                if (strFileName.StartsWith(""))
                                {
                                    strFileName = strFileName.Substring(0);
                                }
                                ZipEntry entry = new ZipEntry(strFileName);//指定名称创建新的 ZIP 条目
                                entry.DateTime = DateTime.Now;//指定当前时间
                                zipOutputStream.PutNextEntry(entry);//为压缩文件流提供一个容器 
                                zipOutputStream.Write(buffer, 0, buffer.Length);//写入文件

                                fs.Close();
                                fs.Dispose();
                            }
                        }
                        catch
                        {
                            continue;
                        }
                    }

                    zipOutputStream.Finish();//结束压缩
                    zipOutputStream.Close();//关闭

                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }

  /// <summary>
       /// 文件解压
       /// </summary>
       /// <param name="strZipPath">压缩包路径</param>
       /// <param name="strOldPath">解压文件源目录</param>
       /// <returns></returns>
        private bool UnZipFile(string strZipPath, string strOldPath)
        {
            string un_dir = strOldPath + "user";
            Directory.CreateDirectory(un_dir);         //创建以解压时间为名称的文件夹 
            ZipInputStream f = new ZipInputStream(System.IO.File.OpenRead(strZipPath));   //读取压缩文件,并用此文件流新建   “ZipInputStream”对象 
            ZipEntry zp = null;
            while ((zp = f.GetNextEntry()) != null)
            {
                int i = 2048;
                byte[] b = new byte[i];     //每次缓冲   2048   字节 
                FileStream s = System.IO.File.Create(un_dir + "\\ " + zp.Name);   //(B)-新建文件流 
                while (true)   //持续读取字节,直到一个“ZipEntry”字节读完 
                {
                    i = f.Read(b, 0, b.Length);   //读取“ZipEntry”中的字节 
                    if (i > 0)
                        s.Write(b, 0, i);   //将字节写入新建的文件流 
                    else
                        break;   //读取的字节为   0   ,跳出循环 
                }
            }
            f.Close();
            return true;
        }


 /// <summary>
        /// 得到存放目录下的所有文件
        /// </summary>
        /// <param name="strParentDirectoryPath">源文件路径</param>
        /// <param name="AllFilesPath">所有文件路径</param>
        private void GetDirectoryFiles(string strParentDirectoryPath, List<string> AllFilesPath)
        {
            string[] files = Directory.GetFiles(strParentDirectoryPath);
            for (int i = 0; i < files.Length; i++)
            {
                AllFilesPath.Add(files[i]);
            }
            string[] directorys = Directory.GetDirectories(strParentDirectoryPath);
            for (int i = 0; i < directorys.Length; i++)
            {
                GetDirectoryFiles(directorys[i], AllFilesPath);
            }
            if (files.Length == 0 && directorys.Length == 0)
            {
                AllFilesPath.Add(strParentDirectoryPath);
            }
        }

//引用方式
在main方法中添加如下代码(可被触发的方法就行)

 string strZipPath =@"D:\HttpHandlers\UploadFile\user.zip";
            string strZipTopDirectoryPath = @"D:\HttpHandlers\UploadFile\";
            int intZipLevel = 6;
            string strPassword = "";
            string[] filesOrDirectoriesPaths = new string[] { @"D:\HttpHandlers\UploadFile\" };
            Zip(strZipPath, strZipTopDirectoryPath, intZipLevel, strPassword, filesOrDirectoriesPaths);//压缩方法
            //UnZipFile(strZipPath, strZipTopDirectoryPath);//解压方法

猜你喜欢

转载自blog.csdn.net/qq_31975127/article/details/75059079