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


C# ZipArchive.Create方法代码示例

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


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

示例1: Execute

        public override bool Execute()
        {
            // There's nothing to do if we have no files or not Xap name given
            if (XapFile == null || Files == null || Files.Length == 0)
            {
                return true;
            }

            string xapPath = XapFile.ItemSpec;
            if (!File.Exists(xapPath))
            {
                Log.LogError("The Xap file {0} could not be found.", xapPath);
                return false;
            }

            bool succeeded = true;

            try
            {
                ZipArchive xap = new ZipArchive(xapPath, FileAccess.ReadWrite);

                // Process the files
                for (int i = 0; i < Files.Length; i++)
                {
                    string sourcePath = Files[i].ItemSpec;
                    FileInfo sourceInfo = new FileInfo(sourcePath);

                    string saveFileAs = sourceInfo.Name;
                    if (!string.IsNullOrEmpty(FileReplacementToken))
                    {
                        saveFileAs = saveFileAs.Replace(FileReplacementToken, string.Empty);
                    }

                    // Make sure they didn't pass a directory as an item
                    if (Directory.Exists(sourcePath))
                    {
                        Log.LogError("Cannot process item \"{0}\" because it is a directory!", sourcePath);
                        succeeded = false;
                        continue;
                    }

                    // Make sure the source exists
                    if (!sourceInfo.Exists)
                    {
                        Log.LogError("Cannot process file \"{0}\" that does not exist!", sourcePath);
                        succeeded = false;
                        continue;
                    }

                    ZipArchiveFile zaf = xap[saveFileAs];
                    if (zaf == null)
                    {
                        Log.LogError(
                            "The file \"{0}\" was not found inside the Xap file \"{1}\"",
                            saveFileAs,
                            xapPath);
                        succeeded = false;
                    }
                    else
                    {
                        zaf.Delete();

                        // Overwrite the contents inside the Xap
                        using (Stream fileInsideXap = xap.Create(saveFileAs))
                        {
                            using (Stream newFile = sourceInfo.OpenRead())
                            {
                                ZipArchiveFile.CopyStream(newFile, fileInsideXap);
                            }
                        }

                        Log.LogMessage(
                            MessageImportance.High,
                            "File \"{0}\" inside the Xap file \"{1}\" was replaced with the contents of \"{2}\"",
                            saveFileAs,
                            xapPath,
                            sourcePath);
                    }
                }

                // Close the Xap file, saving any changes
                xap.Close();

                Log.LogMessage("Xap file \"{0}\" saved.", xapPath);
            }
            catch (Exception ex)
            {
                Log.LogErrorFromException(ex);
                succeeded = false;
            }

            return succeeded;
        }
开发者ID:garyjohnson,项目名称:wpnest,代码行数:93,代码来源:XapFileReplacementTask.cs


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