• 使用ICSharpCode.SharpZipLib(C#)实现解压缩文件的操作类


    在处理后台附件上载由于文件较多,需要每个文件单独上传关键是有些文件数据量比较少 也需要单独上传,这样导致后台数据流量较大而且用户操作麻烦.

    在处理这方面业务时,可以简化:首先验证用户上传文件的大小,设定不超过1M文件为限制并记录,当用户点击一次操作时后台程序把所有小数据量文件进行压缩成一个单独文件来上传,这样简化用户操作难度 增强用户体验,在获得上载文件时同样把这个文件进行解压本地即可...

    使用ICSharpCode.SharpZipLib-(C#)实现解压缩文件的操作类: 完整代码如下
    A:ICSharpCode.SharpZipLib.DLL组件下载地址,如果要实现必须在项目中引用该组件DLL
    下载地址:http://good.gd/203866.htm

    B:完整的操作类代码实例:
      1using System;
      2using System.Collections.Generic;
      3using System.Linq;
      4using System.Web;
      5
      6//using the Compent Comspac
      7using System.IO;
      8using System.Text;
      9using System.Threading;
     10using ICSharpCode.SharpZipLib;
     11using ICSharpCode.SharpZipLib.Zip;
     12using ICSharpCode.SharpZipLib.Checksums;
     13
     14namespace TestJqueryAjax
     15{
     16    /// <summary>
     17    /// 使用ICSharpZipCode.Dll实现解压缩
     18    /// Author:chenkai  Time:2009年7月13日22:03:27
     19    /// Version:Beta1.0.0-(测试版)
     20    /// </summary>

     21    public class ICSharpZipCodeTest
     22    {
     23
     24        /// <summary>
     25        /// 实现压缩功能
     26        /// </summary>
     27        /// <param name="filenameToZip">要压缩文件(绝对文件路径)</param>
     28        /// <param name="Zipedfiledname">压缩(绝对文件路径)</param>
     29        /// <param name="CompressionLevel">压缩比</param>
     30        /// <param name="password">加密密码</param>
     31        /// <param name="comment">压缩文件描述</param>
     32        /// <returns>异常信息</returns>

     33        public static string MakeZipFile(string[] filenameToZip, string Zipedfiledname, int CompressionLevel, 
     34            string password, string comment)
     35        {
     36            try
     37            {
     38              //使用正则表达式-判断压缩文件路径
     39                System.Text.RegularExpressions.Regex newRegex = new System.Text.
     40                    RegularExpressions.Regex(@"^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w   ]*.*))");
     41
     42                if (!newRegex.Match(Zipedfiledname).Success)
     43                {
     44                    File.Delete(Zipedfiledname);
     45                    return "压缩文件的路径有误!";
     46                }

     47
     48               //创建ZipFileOutPutStream
     49                ZipOutputStream newzipstream = new ZipOutputStream(File.Open(Zipedfiledname,
     50                    FileMode.OpenOrCreate));
     51                
     52               //判断Password
     53                if (password != null && password.Length > 0)
     54                {
     55                    newzipstream.Password = password;
     56                }

     57
     58                if (comment != null && comment.Length > 0)
     59                {
     60                    newzipstream.SetComment(comment);
     61                }

     62
     63                //设置CompressionLevel
     64                newzipstream.SetLevel(CompressionLevel); //-查看0 - means store only to 9 - means best compression 
     65                 
     66                //执行压缩
     67                foreach (string filename in filenameToZip)
     68                {
     69                    FileStream newstream = File.OpenRead(filename);//打开预压缩文件
     70
     71                    //判断路径
     72                    if (!newRegex.Match(Zipedfiledname).Success)
     73                    {
     74                        File.Delete(Zipedfiledname);
     75                        return "压缩文件目标路径不存在!";
     76                    }

     77
     78                   byte[] setbuffer=new byte[newstream.Length];
     79                   newstream.Read(setbuffer,0,setbuffer.Length);//读入文件
     80
     81                   //新建ZipEntrity
     82                   ZipEntry newEntry = new ZipEntry(filename);
     83
     84                   //设置时间-长度
     85                   newEntry.DateTime = DateTime.Now;
     86                   newEntry.Size = newstream.Length;
     87
     88                   newstream.Close();
     89
     90                   newzipstream.PutNextEntry(newEntry);//压入
     91
     92                   newzipstream.Write(setbuffer,0,setbuffer.Length);
     93                    
     94                }

     95                //重复压入操作
     96                newzipstream.Finish();
     97                newzipstream.Close();
     98                  
     99            }
    catch (Exception e)
    100            {
    101              //出现异常
    102                File.Delete(Zipedfiledname);
    103                return e.Message.ToString();
    104            }

    105            
    106            return "";
    107        }

    108
    109        /// <summary>
    110        /// 实现解压操作
    111        /// </summary>
    112        /// <param name="zipfilename">要解压文件Zip(物理路径)</param>
    113        /// <param name="UnZipDir">解压目的路径(物理路径)</param>
    114        /// <param name="password">解压密码</param>
    115        /// <returns>异常信息</returns>

    116        public static string UnMakeZipFile(string zipfilename,string UnZipDir,string password)
    117        {
    118            //判断待解压文件路径
    119            if (!File.Exists(zipfilename))
    120            {
    121                File.Delete(UnZipDir);
    122                return "待解压文件路径不存在!";
    123            }

    124            
    125            //创建ZipInputStream
    126            ZipInputStream newinStream = new ZipInputStream(File.OpenRead(zipfilename));
    127            
    128            //判断Password
    129            if (password != null && password.Length > 0)
    130            {
    131                newinStream.Password = password;
    132            }

    133
    134            //执行解压操作
    135            try
    136            {
    137                ZipEntry theEntry;
    138
    139                //获取Zip中单个File
    140                while ((theEntry = newinStream.GetNextEntry()) != null)
    141                {
    142                    //判断目的路径
    143                    if (Directory.Exists(UnZipDir))
    144                    {
    145                        Directory.CreateDirectory(UnZipDir);//创建目的目录
    146                    }

    147
    148                    //获得目的目录信息
    149                    string Driectoryname = Path.GetDirectoryName(UnZipDir);
    150                    string pathname = Path.GetDirectoryName(theEntry.Name);//获得子级目录
    151                    string filename = Path.GetFileName(theEntry.Name);//获得子集文件名
    152
    153                    //处理文件盘符问题
    154                    pathname = pathname.Replace(":""$");//处理当前压缩出现盘符问题
    155                    Driectoryname = Driectoryname + "\\" + pathname;
    156
    157                    //创建
    158                    Directory.CreateDirectory(Driectoryname);
    159
    160                    //解压指定子目录
    161                    if (filename != string.Empty)
    162                    {
    163                        FileStream newstream = File.Create(Driectoryname + "\\" + pathname);
    164
    165                        int size = 2048;
    166
    167                        byte[] newbyte = new byte[size];
    168
    169                        while (true)
    170                        {
    171                            size = newinStream.Read(newbyte, 0, newbyte.Length);
    172
    173                            if (size > 0)
    174                            {
    175                                //写入数据
    176                                newstream.Write(newbyte, 0, size);
    177                            }

    178                            else
    179                            {
    180                                break;
    181                            }

    182                            newstream.Close();
    183                        }

    184                    }

    185
    186                }

    187                newinStream.Close();
    188            }

    189            catch (Exception se)
    190            {
    191
    192                return se.Message.ToString();
    193            }

    194            finally
    195            {
    196                newinStream.Close();
    197            }

    198
    199
    200            return "";
    201        }

    202    }

    203}

    204


  • 相关阅读:
    连接ESP32失败:等待包头 #226超时
    题王网
    高可用架构模式——CAP
    高性能负载均衡
    单服务器高性能模式
    虚拟支付
    高性能架构设计——数据库篇
    MySQL
    shell 命令: MD5
    HTTPS
  • 原文地址:https://www.cnblogs.com/chenkai/p/1526970.html
Copyright © 2020-2023  润新知