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的大小来决定的,整个提交过程需要重新打包。

  • 相关阅读:
    HDU_oj_2048 错排问题
    HDU_oj_2047 阿牛的EOF牛肉面
    HDU_oj_2046 骨牌铺方格
    HDU_oj_2045 不容易系列之RPG问题
    拷贝构造函数(三)——重载赋值运算符
    拷贝构造函数(二)——深拷贝与浅拷贝
    拷贝构造函数(一)——哲学三连
    HDU_oj_2044 一只小蜜蜂
    【转发】【linux】【ftp】CentOS 7.0安装配置Vsftp服务器
    【编码】【转发】enca 转换编码
  • 原文地址:https://www.cnblogs.com/smallerpig/p/3646129.html
Copyright © 2011-2022 走看看