zoukankan      html  css  js  c++  java
  • 利用ICSharpCode.SharpZipLib对压缩文件添加新文件

        看了很多关于ICSharpCode.SharpZipLib进行压缩的文章,都是将一个文件和目录压缩成一个全新的ZIP文件,即ZipOutputStream s = new ZipOutputStream(File.Create("c:""d.zip")))。而我要实现的是对存在的zip文件添加新的文件,我采用ZipOutputStream s = new ZipOutputStream(File.OpenCreate("c:\\d.zip")))方式,程序执行没有问题,但打开d.zip并没有我添加的文件。

        想了个变通的方法,产生个新的zip文件,把原来zip中的文件和要添加的新文件,统一添加到新的zip中,代码如下:

            public void AddZip(string from,string toZip)
            {
                try
                {
                    using (ZipOutputStream s = new ZipOutputStream(File.Create(toZip)))
                    {
                        //s.SetLevel(9);
                        ZipEntry entry = null;
                        ZipInputStream p = new ZipInputStream(File.OpenRead(from));
                        while((entry = p.GetNextEntry()) != null)
                        {
                            ZipEntry zipEntry = new ZipEntry(entry.Name);
                            zipEntry.DateTime = DateTime.Now;
                            s.PutNextEntry(zipEntry);
                            byte[] data = new byte[204800];
                            int sourceBytes;
                            do
                            {
                                sourceBytes = p.Read(data, 0, data.Length);
                                s.Write(data, 0, sourceBytes);
                            } while (sourceBytes > 0);

                            //p.Read(data, 0, data.Length);http://www.joymo.cn
                            //s.Write(data, 0, data.Length);
                        }
                        s.Finish();
                        s.Close();
                        p.Close();
                    }
                }
                catch (Exception ex)
                {
                    throw new System.NotImplementedException(ex.Message);
                }
            }

         功能是实现了,感觉上不如我用VC做的joymobiler中ZipArchive压缩速度快。


     




  • 相关阅读:
    LeetCode Path Sum II
    LeetCode Longest Palindromic Substring
    LeetCode Populating Next Right Pointers in Each Node II
    LeetCode Best Time to Buy and Sell Stock III
    LeetCode Binary Tree Maximum Path Sum
    LeetCode Find Peak Element
    LeetCode Maximum Product Subarray
    LeetCode Intersection of Two Linked Lists
    一天一个设计模式(1)——工厂模式
    PHP迭代器 Iterator
  • 原文地址:https://www.cnblogs.com/freemobile/p/1297538.html
Copyright © 2011-2022 走看看