当前位置: 首页>>代码示例>>C#>>正文


C# ZipFile.UpdateItem方法代码示例

本文整理汇总了C#中Ionic.Zip.ZipFile.UpdateItem方法的典型用法代码示例。如果您正苦于以下问题:C# ZipFile.UpdateItem方法的具体用法?C# ZipFile.UpdateItem怎么用?C# ZipFile.UpdateItem使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Ionic.Zip.ZipFile的用法示例。


在下文中一共展示了ZipFile.UpdateItem方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main


//.........这里部分代码省略.........
                            case "-UTnewest":
                                _UseUniformTimestamp = 2;
                                break;

                            case "-UToldest":
                                _UseUniformTimestamp = 3;
                                break;

                            case "-UT":
                                i++;
                                if (args.Length <= i) Usage();
                                _UseUniformTimestamp = 4;
                                try
                                {
                                    _fixedTimestamp= System.DateTime.Parse(args[i]);
                                }
                                catch
                                {
                                    throw new ArgumentException("-UT");
                                }
                                break;

                            case "-utf8":
                                zip.AlternateEncoding = System.Text.Encoding.UTF8;
                                zip.AlternateEncodingUsage = ZipOption.Always;
                                break;

#if NOT
                            case "-c":
                                i++;
                                if (args.Length <= i) Usage();
                                entryComment = args[i];  // for the next entry
                                break;
#endif

                            case "-zc":
                                i++;
                                if (args.Length <= i) Usage();
                                zip.Comment = args[i];
                                break;

                            default:
                                // UpdateItem will add Files or Dirs,
                                // recurses subdirectories
                                actualItem = Path.Combine(directoryOnDisk ?? ".", args[i]);
                                zip.UpdateItem(actualItem, entryDirectoryPathInArchive);
                                break;
                        }
                    }

                    if (_UseUniformTimestamp > 0)
                    {
                        if (_UseUniformTimestamp==2)
                        {
                            // newest
                            _fixedTimestamp = new System.DateTime(1601,1,1,0,0,0);
                            foreach(var entry in zip)
                            {
                                if (entry.LastModified > _fixedTimestamp)
                                    _fixedTimestamp = entry.LastModified;
                            }
                        }
                        else if (_UseUniformTimestamp==3)
                        {
                            // oldest
                            foreach(var entry in zip)
                            {
                                if (entry.LastModified < _fixedTimestamp)
                                    _fixedTimestamp = entry.LastModified;
                            }
                        }

                        foreach(var entry in zip)
                        {
                            entry.LastModified = _fixedTimestamp;
                        }
                    }

                    if (!flavor.HasValue)
                    {
                        if (saveToStdout)
                            zip.Save(Console.OpenStandardOutput());
                        else
                            zip.Save();
                    }
                    else
                    {
                        if (saveToStdout)
                            throw new Exception("Cannot save SFX to stdout, sorry! See http://dotnetzip.codeplex.com/WorkItem/View.aspx?WorkItemId=7246");
                        zip.SaveSelfExtractor(args[0], flavor.Value);

                    }

                }
            }
            catch (System.Exception ex1)
            {
                System.Console.WriteLine("Exception: " + ex1);
            }
        }
开发者ID:ahjohannessen,项目名称:DotNetZip.Semverd,代码行数:101,代码来源:ZipIt.cs

示例2: UpdateZip_UpdateItem

        public void UpdateZip_UpdateItem()
        {
            string filename = null;
            int entriesAdded = 0;
            string repeatedLine = null;
            int j;

            // select the name of the zip file
            string zipFileToCreate = Path.Combine(TopLevelDir, "UpdateZip_UpdateItem.zip");

            // create the subdirectory
            string subdir = Path.Combine(TopLevelDir, "A");
            Directory.CreateDirectory(subdir);

            // create a bunch of files
            int numFilesToCreate = _rnd.Next(10) + 8;
            for (j = 0; j < numFilesToCreate; j++)
            {
                filename = Path.Combine(subdir, String.Format("file{0:D3}.txt", j));
                repeatedLine = String.Format("Content for Original file {0}",
                    Path.GetFileName(filename));
                TestUtilities.CreateAndFillFileText(filename, repeatedLine, _rnd.Next(34000) + 5000);
                entriesAdded++;
            }

            // Create the zip file
            Directory.SetCurrentDirectory(TopLevelDir);
            using (ZipFile zip1 = new ZipFile())
            {
                String[] filenames = Directory.GetFiles("A");
                foreach (String f in filenames)
                    zip1.AddFile(f, "");
                zip1.Comment = "UpdateTests::UpdateZip_UpdateItem(): This archive will be updated.";
                zip1.Save(zipFileToCreate);
            }

            // Verify the files are in the zip
            Assert.AreEqual<int>(TestUtilities.CountEntries(zipFileToCreate), entriesAdded,
                "The Zip file has the wrong number of entries.");

            // create another subdirectory
            subdir = Path.Combine(TopLevelDir, "B");
            Directory.CreateDirectory(subdir);

            // create a bunch more files
            int newFileCount = numFilesToCreate + _rnd.Next(3) + 3;
            for (j = 0; j < newFileCount; j++)
            {
                filename = Path.Combine(subdir, String.Format("file{0:D3}.txt", j));
                repeatedLine = String.Format("Content for the updated file {0} {1}",
                    Path.GetFileName(filename),
                    System.DateTime.Now.ToString("yyyy-MM-dd"));
                TestUtilities.CreateAndFillFileText(filename, repeatedLine, _rnd.Next(1000) + 2000);
                entriesAdded++;
            }

            // Update those files in the zip file
            Directory.SetCurrentDirectory(TopLevelDir);
            using (ZipFile zip1 = FileSystemZip.Read(zipFileToCreate))
            {
                String[] filenames = Directory.GetFiles("B");
                foreach (String f in filenames)
                    zip1.UpdateItem(f, "");
                zip1.Comment = "UpdateTests::UpdateZip_UpdateItem(): This archive has been updated.";
                zip1.Save(zipFileToCreate);
            }

            // Verify the number of files in the zip
            Assert.AreEqual<int>(TestUtilities.CountEntries(zipFileToCreate), newFileCount,
                "The Zip file has the wrong number of entries.");

            // now extract the files and verify their contents
            using (ZipFile zip3 = FileSystemZip.Read(zipFileToCreate))
            {
                foreach (string s in zip3.EntryFileNames)
                {
                    repeatedLine = String.Format("Content for the updated file {0} {1}",
                        s,
                        System.DateTime.Now.ToString("yyyy-MM-dd"));
                    zip3[s].Extract("extract");

                    // verify the content of the updated file.
                    var sr = new StreamReader(Path.Combine("extract", s));
                    string sLine = sr.ReadLine();
                    sr.Close();

                    Assert.AreEqual<string>(repeatedLine, sLine,
                            String.Format("The content of the Updated file ({0}) in the zip archive is incorrect.", s));
                }
            }
        }
开发者ID:mattleibow,项目名称:Zip.Portable,代码行数:91,代码来源:UpdateTests.cs


注:本文中的Ionic.Zip.ZipFile.UpdateItem方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。