/// <summary> /// Copyies 'sourceFilePath from the textStream system to the archive as 'targetArchivePath' /// It will overwrite any existing textStream. /// </summary> public void CopyFromFile(string sourceFilePath, string targetArchivePath) { using (Stream inFile = new FileStream(sourceFilePath, FileMode.Open, FileAccess.Read)) using (Stream outFile = Create(targetArchivePath)) ZipArchiveFile.CopyStream(inFile, outFile); this[targetArchivePath].LastWriteTime = File.GetLastWriteTime(sourceFilePath); }
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); }