• C#操作Zip


    Zip格式是开源的压缩格式,在.NET4.0下微软只提供了gzip的相关操作类,在.NET4.5之后才直接提供了操作Zip的类。

    在4.0之前想要操作Zip只有借鉴第三方的类库,比较著名的是:ShareZipLib。

    直接解压与压缩Zip的操作比较简单,这里小猪分享的是在不解压所有文件的情况下只解压Zip包中的文件

    情况一:知道Zip包中有某文件且知道在什么地方,解压Zip包中特定文件。

    情况二:不知道文件在Zip包中的位置,需求搜索。

    先在项目中引用第三方类库

    在类代码前面插入对类库的引用:

    using ICSharpCode.SharpZipLib.Zip;
    using ICSharpCode.SharpZipLib.Core;

    编写对应的代码:

                ZipInputStream zis = new ZipInputStream(System.IO.File.Open(integralName, FileMode.Open));
     
                ZipEntry entry = zis.GetNextEntry();
     
                while (entry != null)
                {//这里是情况二
                    #region 搜索 search.png
                    if (entry.Name.Contains("search.png")) 
                    {
                        FileStream writer = System.IO.File.Create(this.Request.PhysicalApplicationPath + "/Zips/" + datetime + "search.png"); 
    //解压后的文件
     
                            int bufferSize = 1024 * 2; //缓冲区大小
                            int readCount = 0; //读入缓冲区的实际字节
                            byte[] buffer = new byte[bufferSize];
                            readCount = zis.Read(buffer, 0, bufferSize);
                            while (readCount > 0)
                            {
                                writer.Write(buffer, 0, readCount);
                                readCount = zis.Read(buffer, 0, bufferSize);
                            }
                            writer.Close();
                            writer.Dispose();
                    }
                    #endregion
                    entry = zis.GetNextEntry();
                }
                zis.Close();
                zis.Dispose();
     
                using (FileStream fs = new FileStream(integralName, FileMode.Open, FileAccess.Read))
                using (ZipFile zf = new ZipFile(fs))
                {
                    var ze = zf.GetEntry("face.png");//情况一
                    if (ze == null)
                    {
                    }
                    else
                    {
                        #region 指定 face.png
                        using (var s = zf.GetInputStream(ze))
                        {
                            FileStream writer = System.IO.File.Create(this.Request.PhysicalApplicationPath + "/Zips/" + datetime + "face.png"); //解压后的文件
     
                            int bufferSize = 1024*2; //缓冲区大小
                            int readCount = 0; //读入缓冲区的实际字节
                            byte[] buffer = new byte[bufferSize];
                            readCount = s.Read(buffer, 0, bufferSize);
                            while (readCount > 0)
                            {
                                writer.Write(buffer, 0, readCount);
                                readCount = s.Read(buffer, 0, bufferSize);
                            }
     
                            writer.Close();
                            writer.Dispose();
     
                            // do something with ZipInputStream
                        }
                        #endregion
                    }
                }
  • 相关阅读:
    delphi ios grid BindSourceDB bug
    RAD 10 C++Builder的bug
    Delphi Berlin 窗体代码分离风格 回到Delphi7传统风格
    delphi const的用法
    mysql的sql优化
    mysql如何使用索引index提升查询效率?
    移动端mobiscroll无法滑动、无法划动选值的问题
    html css的内联样式 内部样式表 外部样式表的优先级
    jfinal如何获取参数为数组的值
    jquery如何让checkbox如何取消勾选
  • 原文地址:https://www.cnblogs.com/smallerpig/p/3646122.html
Copyright © 2020-2023  润新知