zip压缩------wpf

    

     /// <summary>

         /// ZIP:压缩文件夹
         /// </summary>
         /// <param name="DirectoryToZip">需要压缩的文件夹(绝对路径)</param>
         /// <param name="ZipedPath">压缩后的文件路径(绝对路径)</param>
         /// <param name="ZipedFileName">压缩后的文件名称(文件名,默认 同源文件夹同名)</param>
         /// <param name="IsEncrypt">是否加密(默认 加密 false 不加密)</param>
         public static void ZipDirectory(string DirectoryToZip, string ZipedPath, string ZipedFileName = "", bool IsEncrypt = false)
         {
             //如果目录不存在,则报错
             if (!System.IO.Directory.Exists(DirectoryToZip))
             {
                 throw new System.IO.FileNotFoundException("指定的目录: " + DirectoryToZip + " 不存在!");
             }
 
             //文件名称(默认同源文件名称相同)
             string ZipFileName = string.IsNullOrEmpty(ZipedFileName) ? ZipedPath + "\\" + new DirectoryInfo(DirectoryToZip).Name + ".zip" : ZipedPath + "\\" + ZipedFileName + ".zip";
 
             using (System.IO.FileStream ZipFile = System.IO.File.Create(ZipFileName))
             {
                 using (ZipOutputStream s = new ZipOutputStream(ZipFile))
                 {
                    //bool IsEncrypt = true需要下面的设置密码
                    //if (IsEncrypt)
                    //{
                    //    //压缩文件加密
                    //    s.Password = "123";
                    //}
                    ZipSetp(DirectoryToZip, s, "");
                 }
             }
         }
         /// <summary>
         /// 递归遍历目录
         /// </summary>
         private static void ZipSetp(string strDirectory, ZipOutputStream s, string parentPath)
         {
             if (strDirectory[strDirectory.Length - 1] != System.IO.Path.DirectorySeparatorChar)
             {
                 strDirectory += System.IO.Path.DirectorySeparatorChar;
             }
             Crc32 crc = new Crc32();
 
             string[] filenames = Directory.GetFileSystemEntries(strDirectory);
 
             foreach (string file in filenames)// 遍历所有的文件和目录
             {
 
                 if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
                 {
                     string pPath = parentPath;
                     pPath += file.Substring(file.LastIndexOf("\\") + 1);
                     pPath += "\\";
                     ZipSetp(file, s, pPath);
                 }
 
                 else // 否则直接压缩文件
                 {
                     //打开压缩文件
                     using (FileStream fs = File.OpenRead(file))
                     {
 
                         byte[] buffer = new byte[fs.Length];
                         fs.Read(buffer, 0, buffer.Length);
 
                         string fileName = parentPath + file.Substring(file.LastIndexOf("\\") + 1);
                         ZipEntry entry = new ZipEntry(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);
                     }
                 }
             }
        }
        


        private void btn_Ok_Click(object sender, RoutedEventArgs e)
        {
            //s1文件路径,s2加密后文件要放的路径
            string s1 = tb_ZipFrontPath.Text.Trim();
            string s2 = tb_ZipAfterPath.Text.Trim();
            if (!Directory.Exists(s1))
            {
                System.Windows.MessageBox.Show("压缩文件路径不正确");
            }
            else
            {
                if (!Directory.Exists(s2))
                {
                    try
                    {
                        Directory.CreateDirectory(s2);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("创建压缩后路径失败:" + ex.Message);
                    }
                }
                //确认弹框
                if (System.Windows.MessageBox.Show("是否压缩?", "caption", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    //弹出输入框输入相应的解压密码


                    //string passWordZip = Interaction.InputBox("");
                    //压缩方法调用
                    ZipDirectory(s1, s2, "123");
                    System.Windows.MessageBox.Show("文件压缩已经完成!");
                    tb_ZipFrontPath.Text = "";
                    tb_ZipAfterPath.Text = "";
                }
            }


        }

猜你喜欢

转载自blog.csdn.net/yatou_buku/article/details/79224481