zoukankan      html  css  js  c++  java
  • C#使用SharpZipLib编辑zip包中内容

    小猪最近在使用SharpZipLib进行zip包的操作,编写了下列测试代码。

    static void Main(string[] args)
    {
        Console.WriteLine("---------------------Zip包中的文件并解压测试-------------------------");
        string content;
      
        string filename = @"E:wamp.zip";//需要测试的zip包地址
        string searchname = @"Web.config";
        using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite))
        using (ZipFile zf = new ZipFile(fs))
        {
            Console.WriteLine("开始查找:" + searchname + System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
            Console.WriteLine("Zip包大小:" + fs.Length);
            var ze = zf.GetEntry(searchname);
            if (ze == null)
            {
            }
            else
            {
                Console.WriteLine("文件长度:" + ze.Size);
                Console.WriteLine("开始修改:" + System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
                using (Stream s = zf.GetInputStream(ze))
                {
                    StreamReader inputReader = new StreamReader(s);
                    content = inputReader.ReadToEnd();
                    content += "
    this is SmallerPig Test 测试下中文";//在原流中增加字符串
                }
                zf.BeginUpdate();
                zf.Add(new StateDataSource(content), searchname);
                zf.CommitUpdate();
                Console.WriteLine("结束:" + System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
            }
        }
    }
      
      
    class StateDataSource:IStaticDataSource//自定义实现IStaticDataSource接口的类
    {
        string source;
      
        public StateDataSource(string source)
        {
            this.source = source;
        }
      
        public Stream GetSource()
        {
            byte[] array = Encoding.ASCII.GetBytes(source);
            MemoryStream stream = new MemoryStream(array);
            return stream;
        }
      
    }

    由测试结果知道:编辑一个zip中文件所需时间主要是由zip的大小来决定的,整个提交过程需要重新打包。

  • 相关阅读:
    hive_case
    hive_group
    linux-搭建ngnix
    Nfs服务器搭建
    几种常见的启动脚本
    linux 修改本机的端口映射
    oracle-sql计算
    linux 磁盘大小查看
    postMan测试接口的几种方式
    oracle迁移到12c的时列转行 wm_concat 报错解决
  • 原文地址:https://www.cnblogs.com/smallerpig/p/3646129.html
Copyright © 2011-2022 走看看