Skip to content
This repository has been archived by the owner on Dec 6, 2024. It is now read-only.

Commit

Permalink
Fixes #423: Add Copy-DirectoryWithProgress.ps1 demonstrating director…
Browse files Browse the repository at this point in the history
…y copy with progress report.
  • Loading branch information
Yomodo committed Feb 25, 2018
1 parent b878d6e commit 4945958
Show file tree
Hide file tree
Showing 8 changed files with 411 additions and 68 deletions.
101 changes: 101 additions & 0 deletions AlphaFS.UnitTest/File Class/File.Copy/File.Copy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Globalization;
using System.Reflection;

namespace AlphaFS.UnitTest
Expand All @@ -38,6 +39,14 @@ public void File_Copy_LocalAndNetwork_Success()
}


[TestMethod]
public void File_Copy_WithProgress_LocalAndNetwork_Success()
{
File_Copy_WithProgress(false);
File_Copy_WithProgress(true);
}


private void File_Copy(bool isNetwork)
{
UnitTestConstants.PrintUnitTestHeader(isNetwork);
Expand Down Expand Up @@ -78,5 +87,97 @@ private void File_Copy(bool isNetwork)

Console.WriteLine();
}


private void File_Copy_WithProgress(bool isNetwork)
{
UnitTestConstants.PrintUnitTestHeader(isNetwork);
Console.WriteLine();


var tempPath = UnitTestConstants.TempFolder;
if (isNetwork)
tempPath = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(tempPath);


using (var rootDir = new TemporaryDirectory(tempPath, MethodBase.GetCurrentMethod().Name))
{
// Min: 1 bytes, Max: 10485760 = 10 MB.
var fileLength = new Random().Next(1, 10485760);
var fileSource = UnitTestConstants.CreateFile(rootDir.Directory.FullName, fileLength);
var fileCopy = rootDir.RandomFileFullPath;

Console.WriteLine("Src File Path: [{0:N0} ({1}) [{2}]", fileLength, Alphaleonis.Utils.UnitSizeToText(fileLength), fileSource);
Console.WriteLine("Dst File Path: [{0}]", fileCopy);

Assert.IsTrue(System.IO.File.Exists(fileSource.FullName), "The file does not exists, but is expected to.");


// Allow copy to overwrite an existing file.
const Alphaleonis.Win32.Filesystem.CopyOptions copyOptions = Alphaleonis.Win32.Filesystem.CopyOptions.None;

// The copy progress handler.
var callback = new Alphaleonis.Win32.Filesystem.CopyMoveProgressRoutine(FileCopyProgressHandler);

Console.WriteLine();


// You can pass any piece of data to userProgressData. This data can be accessed from the callback method.
// Specify file length for assertion.
var userProgressData = fileLength;


var cmr = Alphaleonis.Win32.Filesystem.File.Copy(fileSource.FullName, fileCopy, copyOptions, callback, userProgressData);

UnitTestConstants.Dump(cmr, -18);


Assert.IsTrue(System.IO.File.Exists(fileCopy), "The file does not exists, but is expected to.");


var fileLen = new System.IO.FileInfo(fileCopy).Length;

Assert.AreEqual(fileLength, fileLen, "The file copy is: {0} bytes, but is expected to be: {1} bytes.", fileLen, fileLength);

Assert.IsTrue(System.IO.File.Exists(fileSource.FullName), "The original file does not exist, but is expected to.");
}


Console.WriteLine();
}


private static Alphaleonis.Win32.Filesystem.CopyMoveProgressResult FileCopyProgressHandler(long totalFileSize, long totalBytesTransferred, long streamSize,
long streamBytesTransferred, int streamNumber, Alphaleonis.Win32.Filesystem.CopyMoveProgressCallbackReason callbackReason, object userData)
{
if (callbackReason == Alphaleonis.Win32.Filesystem.CopyMoveProgressCallbackReason.StreamSwitch)

Assert.AreEqual(0, totalBytesTransferred);


else
{
var pct = Convert.ToDouble(totalBytesTransferred, CultureInfo.InvariantCulture) / totalFileSize * 100;

Console.WriteLine("\tCallback: Copied: [{0}%] --> [{1:N0}] bytes.", pct.ToString("N2", CultureInfo.CurrentCulture), totalBytesTransferred);

Assert.IsTrue(totalBytesTransferred > 0);


var bytes = Convert.ToInt64(userData);

if (pct.Equals(100))
{
Assert.AreEqual(totalBytesTransferred, bytes);
Assert.AreEqual(totalFileSize, bytes);
}

else
Assert.IsTrue(totalBytesTransferred < bytes);
}


return Alphaleonis.Win32.Filesystem.CopyMoveProgressResult.Continue;
}
}
}
1 change: 1 addition & 0 deletions AlphaFS.sln
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Project("{7CF6DF6D-3B04-46F8-A40B-537D21BCA0B4}") = "AlphaFS.Doc", "AlphaFS.Doc\
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "PowerShell", "PowerShell", "{9D8AAD55-4083-454F-BBA9-1AB58D2B5FB9}"
ProjectSection(SolutionItems) = preProject
PowerShell\Copy-DirectoryWithProgress.ps1 = PowerShell\Copy-DirectoryWithProgress.ps1
PowerShell\Copy-FileWithProgress.ps1 = PowerShell\Copy-FileWithProgress.ps1
PowerShell\Enumerate-FileSystemEntryInfos.ps1 = PowerShell\Enumerate-FileSystemEntryInfos.ps1
EndProjectSection
Expand Down
14 changes: 6 additions & 8 deletions AlphaFS/Filesystem/CopyMoveResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,9 @@ public sealed class CopyMoveResult

#region Constructors

/// <summary>Create a CopyMoveResult class instance for the Copy or Move action.
/// <remarks>Normally there is no need to manually call this constructor.</remarks>
/// </summary>
/// <param name="source">Indicates the source file or directory.</param>
/// <param name="destination">Indicates the destination file or directory.</param>
/// <summary>Initializes a CopyMoveResult instance for the Copy or Move action.</summary>
/// <param name="source">Indicates the full path to the source file or directory.</param>
/// <param name="destination">Indicates the full path to the destination file or directory.</param>
private CopyMoveResult(string source, string destination)
{
Source = source;
Expand All @@ -57,11 +55,11 @@ private CopyMoveResult(string source, string destination)
}


/// <summary>Create a CopyMoveResult class instance for the Copy or Move action.
/// <summary>Initializes a CopyMoveResult instance for the Copy or Move action.
/// <remarks>Normally there is no need to manually call this constructor.</remarks>
/// </summary>
/// <param name="source">Indicates the source file or directory.</param>
/// <param name="destination">Indicates the destination file or directory.</param>
/// <param name="source">Indicates the full path to the source file or directory.</param>
/// <param name="destination">Indicates the full path to the destination file or directory.</param>
/// <param name="isCopy">>When <see langword="true"/> the action is a Copy, Move otherwise.</param>
/// <param name="isDirectory">When <see langword="true"/> indicates the sources is a directory; file otherwise.</param>
/// <param name="preserveDates"><see langword="true"/> if original Timestamps must be preserved, <see langword="false"/> otherwise. This parameter is ignored for move operations.</param>
Expand Down
19 changes: 9 additions & 10 deletions AlphaFS/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,21 @@ public static string UnitSizeToText<T>(T numberOfBytes)
/// <summary>Converts a number of type T to string formated using the specified <paramref name="cultureInfo"/>, suffixed with a unit size.</summary>
public static string UnitSizeToText<T>(T numberOfBytes, CultureInfo cultureInfo)
{
const int kb = 1024;

var sizeFormats = new[] {"B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
var formatLength = sizeFormats.Length;
const int kb = 1024;
var index = 0;

var bytes = Convert.ToDouble(numberOfBytes, CultureInfo.InvariantCulture);

if (bytes < 0)
bytes = 0;

var index = 0;
while (index < formatLength && bytes > kb)
{
index++;
bytes /= kb;
}
else
while (bytes > kb)
{
bytes /= kb;
index++;
}


// Will return "512 B" instead of "512,00 B".
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Version 2.2 (2018-02-xx)
- Issue #414: Add additional `Network.Host` methods.
- Issue #415: Added `ProcessContext` static class to determine the context of the current process.
- Issue #422: Add `Copy-FileWithProgress.ps1` demonstrating file copy with progress report.
- Issue #423: Add `Copy-DirectoryWithProgress.ps1` demonstrating directory copy with progress report.

### Improvements

Expand Down
Loading

0 comments on commit 4945958

Please sign in to comment.