• c#文件压缩解压


    压缩

    private void skinButton1_Click(object sender, EventArgs e)
            {
                FilesUploadFor.ZipDirectory(foldertozip.Text,zipedfilename.Text);
                filesUploadFor.DisplaylistboxMsg("压缩完成");
            }
    

    ZipDirectory

      //===================================================压缩用的是库函数
           /// <summary>
           /// 压缩文件夹
           /// </summary>
           /// <param name="folderToZip">需要压缩的文件夹</param>
           /// <param name="zipedFileName">压缩后的Zip完整文件名</param>
            public static void ZipDirectory(string folderToZip, string zipedFileName)
            {
                ZipDirectory(folderToZip, zipedFileName, string.Empty, true, string.Empty, string.Empty, true);
            }
     
     
            /// <summary>
            /// 压缩文件夹
            /// </summary>
            /// <param name="folderToZip">需要压缩的文件夹</param>
            /// <param name="zipedFileName">压缩后的Zip完整文件名(如D:	est.zip)</param>
            /// <param name="isRecurse">如果文件夹下有子文件夹,是否递归压缩</param>
            /// <param name="password">解压时需要提供的密码</param>
            /// <param name="fileRegexFilter">文件过滤正则表达式</param>
            /// <param name="directoryRegexFilter">文件夹过滤正则表达式</param>
            /// <param name="isCreateEmptyDirectories">是否压缩文件中的空文件夹</param>
     
            public static void ZipDirectory(string folderToZip, string zipedFileName, string password, bool isRecurse, string fileRegexFilter, string directoryRegexFilter, bool isCreateEmptyDirectories)
            {
                FastZip fastZip = new FastZip();
                fastZip.CreateEmptyDirectories = isCreateEmptyDirectories;
                fastZip.Password = password;
                fastZip.CreateZip(zipedFileName, folderToZip, isRecurse, fileRegexFilter, directoryRegexFilter);
            }
    

    解压缩

      private void skinButton2_Click(object sender, EventArgs e)
            {
                filesUploadFor.UnZip(zipedfilename.Text,"");
                 filesUploadFor.DisplaylistboxMsg("解压缩完成");
            }
    

    UnZip

      //===================================================解压用的是库函数
            /// <summary>  
            /// 功能:解压zip格式的文件。  
            /// </summary>  
            /// <param name="zipFilePath">压缩文件路径</param>  
            /// <param name="unZipDir">解压文件存放路径,为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹</param>  
            /// <returns>解压是否成功</returns>  
            public void UnZip(string zipFilePath, string unZipDir)
            {
                if (zipFilePath == string.Empty)
                {
                    throw new Exception("压缩文件不能为空!");
                }
                if (!File.Exists(zipFilePath))
                {
                    throw new FileNotFoundException("压缩文件不存在!");
                }
                //解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹  
                if (unZipDir == string.Empty)
                    unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));
                if (!unZipDir.EndsWith("/"))
                    unZipDir += "/";
                if (!Directory.Exists(unZipDir))
                    Directory.CreateDirectory(unZipDir);
     
                using (var s = new ZipInputStream(File.OpenRead(zipFilePath)))
                {
     
                    ZipEntry theEntry;
                    while ((theEntry = s.GetNextEntry()) != null)
                    {
                        string directoryName = Path.GetDirectoryName(theEntry.Name);
                        string fileName = Path.GetFileName(theEntry.Name);
                        if (!string.IsNullOrEmpty(directoryName))
                        {
                            Directory.CreateDirectory(unZipDir + directoryName);
                        }
                        if (directoryName != null && !directoryName.EndsWith("/"))
                        {
                        }
                        if (fileName != String.Empty)
                        {
                            using (FileStream streamWriter = File.Create(unZipDir + theEntry.Name))
                            {
     
                                int size;
                                byte[] data = new byte[2048];
                                while (true)
                                {
                                    size = s.Read(data, 0, data.Length);
                                    if (size > 0)
                                    {
                                        streamWriter.Write(data, 0, size);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
    
  • 相关阅读:
    [转] 微服务架构
    vue拦截器实现统一token,并兼容IE9验证
    一句话 道出设计模式
    老话闲说,关于身份证号码的验证
    值得珍藏的HTTP协议详解
    微信内嵌H5网页 解决js倒计时失效
    使用JavaScript重定向URL参数
    Nodejs --我自己的学习笔记
    ONLY三行脚本 SQL数据恢复到指定时间点
    [转] 数据库加锁 sql加锁的
  • 原文地址:https://www.cnblogs.com/ouyangkai/p/12503390.html
Copyright © 2020-2023  润新知