public static Int64 CopyFile(SteamDepotFile file, String destinationFileFullName, Int64 filesSize, Int64 copiedFilesSize, BackgroundWorker worker) { if (file == null) throw new ArgumentNullException("file"); if (String.IsNullOrEmpty(destinationFileFullName)) throw new ArgumentNullException("destinationFileFullName"); if (worker != null && worker.CancellationPending) return 0L; FileInfo sourceFile = new FileInfo(file.FullName); Byte[] buffer = new Byte[1024 * 1024 * 16]; // 16 MB buffer FileInfo destFile = new FileInfo(destinationFileFullName); FileStream source; FileStream destination; Int64 offset = 0; Int32 blockSize; Int32 progressPercent = 0; // Create directory for destination file if not exist if (!destFile.Directory.Exists) { destFile.Directory.Create(); destFile.Directory.Attributes = sourceFile.Directory.Attributes; destFile.Directory.CreationTimeUtc = sourceFile.Directory.CreationTimeUtc; } // If destination file allready exists if (destFile.Exists) { if (file.FileType == SteamDepotFileTypes.Fix) { FileInfo originalFile = new FileInfo(destinationFileFullName + ".original"); if (originalFile.Exists) { if (originalFile.IsReadOnly) originalFile.IsReadOnly = false; originalFile.Delete(); } File.Move(destinationFileFullName, originalFile.FullName); } else if (destFile.IsReadOnly) destFile.IsReadOnly = false; } source = sourceFile.OpenRead(); destination = destFile.Create(); if (filesSize > 0) progressPercent = (Int32)((float)copiedFilesSize / filesSize * 100); while (offset < sourceFile.Length) { if (worker != null && worker.CancellationPending) break; if (offset + buffer.Length <= sourceFile.Length) blockSize = buffer.Length; else blockSize = (Int32)(sourceFile.Length - offset); source.Read(buffer, 0, blockSize); destination.Write(buffer, 0, blockSize); offset += blockSize; copiedFilesSize += blockSize; // Avoid spaming parent thread with report messages if (worker != null && filesSize > 0 && (Int32)((float)copiedFilesSize / filesSize * 100) > progressPercent) { progressPercent = (Int32)((float)copiedFilesSize / filesSize * 100); worker.ReportProgress(progressPercent); } } source.Close(); destination.Close(); destFile.Attributes = sourceFile.Attributes; destFile.CreationTimeUtc = sourceFile.CreationTimeUtc; destFile.LastAccessTimeUtc = sourceFile.LastAccessTimeUtc; destFile.LastWriteTimeUtc = sourceFile.LastWriteTimeUtc; return offset; }
public void AddFile(SteamDepotFile depotFile) { if (depotFile == null) throw new ArgumentNullException("depotFile"); String relativeName = depotFile.RelativeName; if (files.ContainsKey(relativeName)) files[relativeName].Add(depotFile); else { List<SteamDepotFile> filesList = new List<SteamDepotFile>(); filesList.Add(depotFile); files.Add(relativeName, filesList); } }