本文整理汇总了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;
}