public void InsertFileAsync(GameFile sourceFile, CancellationToken ct, UpdateProgressDelegate callback) { callback.Invoke(0, string.Format("Inserting {0}", sourceFile.Name)); int offset = CalculateOffsetForNewEntry(); GameFile addedEntry = AddNewFileEntry(offset, sourceFile); Stream sourceStream = sourceFile.GetStream(FileMode.Open, FileAccess.Read); Stream destinationStream = GetStream(FileMode.Open, FileAccess.Write); byte[] buf; destinationStream.Seek(offset, SeekOrigin.Begin); while (sourceStream.Position < sourceStream.Length) { if (ct.IsCancellationRequested) { sourceStream.Close(); destinationStream.Close(); DeleteFileEntry(addedEntry); return; } buf = new byte[SECTOR_SIZE]; int read = sourceStream.Read(buf, 0, SECTOR_SIZE); destinationStream.Write(buf, 0, SECTOR_SIZE); callback.Invoke((int)((float)sourceStream.Position / sourceStream.Length * 100), string.Format("Inserting {0}", sourceFile.Name)); } destinationStream.Flush(); destinationStream.Close(); sourceStream.Close(); }
public void ExtractFileAsync(GameFile entry, string destination, CancellationToken ct, UpdateProgressDelegate callback) { callback.Invoke(0, string.Format("Extracting {0}", entry.Name)); Stream stream = entry.GetStream(FileMode.Open, FileAccess.Read); FileStream destinationFile = new FileStream(destination, FileMode.Create, FileAccess.Write); byte[] buf = new byte[SECTOR_SIZE]; while (stream.Position < stream.Length) { if (ct.IsCancellationRequested) { destinationFile.Close(); File.Delete(destination); return; } int read = stream.Read(buf, 0, SECTOR_SIZE); destinationFile.Write(buf, 0, read); callback.Invoke((int)((float)stream.Position / stream.Length * 100), string.Format("Extracting {0}", entry.Name)); } destinationFile.Flush(); destinationFile.Close(); stream.Close(); }
/// <summary> /// Экспортирует файл из архива /// </summary> /// <param name="entry"></param> /// <param name="destination"></param> public void ExtractFile(GameFile entry, string destination) { Stream stream = entry.GetStream(FileMode.Open, FileAccess.Read); FileStream destinationFile = new FileStream(destination, FileMode.Create, FileAccess.Write); byte[] buf = new byte[SECTOR_SIZE]; while (stream.Position < stream.Length) { int read = stream.Read(buf, 0, SECTOR_SIZE); destinationFile.Write(buf, 0, read); } destinationFile.Flush(); destinationFile.Close(); stream.Close(); }
/// <summary> /// Inserts new <see cref="GameFile"/> into current <see cref="ArchiveFile"/> /// </summary> /// <summary xml:lang="ru"> /// Добавляет новый файл <see cref="GameFile"/> в текущий архив <see cref="ArchiveFile"/> /// </summary> /// <param name="sourceFile"><see cref="GameFile"/> to be inserted into the <see cref="ArchiveFile"/></param> /// <param name="sourceFile" xml:lang="ru">Файл <see cref="GameFile"/>, который необходимо добавить в архив <see cref="ArchiveFile"/></param> public void InsertFile(GameFile sourceFile) { int offset = CalculateOffsetForNewEntry(); AddNewFileEntry(offset, sourceFile); Stream sourceStream = sourceFile.GetStream(FileMode.Open, FileAccess.Read); Stream destinationStream = GetStream(FileMode.Open, FileAccess.Write); byte[] buf; destinationStream.Seek(offset, SeekOrigin.Begin); while (sourceStream.Position < sourceStream.Length) { buf = new byte[SECTOR_SIZE]; int read = sourceStream.Read(buf, 0, SECTOR_SIZE); destinationStream.Write(buf, 0, SECTOR_SIZE); } destinationStream.Flush(); destinationStream.Close(); sourceStream.Close(); }