public static string SaveAsFile(this ZipArchiveEntry entry, string fileFullPath, ILogger logger)
        {
            using (var inputStream = entry.Open())
            {
                inputStream.CopyToFile(fileFullPath);
            }

            entry.UpdateFileTimeFromEntry(fileFullPath, logger);

            return(fileFullPath);
        }
Пример #2
0
        public async Task UpdateFileTimeFromEntry_FileBusyForLongTime_Throws()
        {
            // Arrange
            string tempFile = Path.GetTempFileName();

            try
            {
                Mock <IEnvironmentVariableReader> environmentVariableReader = new Mock <IEnvironmentVariableReader>();
                ZipArchiveExtensions.Testable     zipArchiveExtensions      = new ZipArchiveExtensions.Testable(environmentVariableReader.Object);

                using MemoryStream memoryStream = new();
                using ZipArchive zipArchive     = new(memoryStream, ZipArchiveMode.Create);

                ZipArchiveEntry zipEntry     = zipArchive.CreateEntry("test.file");
                DateTime        expectedTime = DateTime.UtcNow.AddHours(-5);
                zipEntry.LastWriteTime = expectedTime;

                using FileStream fileStream = File.Open(tempFile, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete);

                // Act & Assert
                await Assert.ThrowsAsync <IOException>(async() =>
                {
                    Task task = Task.Run(() => zipEntry.UpdateFileTimeFromEntry(tempFile, NullLogger.Instance));

                    // If the above deadlocks until the file is unlocked, then we need another way to fail the test within a reasonable time.
                    using CancellationTokenSource cts = new();
                    Task delay    = Task.Delay(TimeSpan.FromSeconds(10), cts.Token);
                    var completed = await Task.WhenAny(task, delay);
                    cts.Cancel();
                    if (completed == delay)
                    {
                        throw new TimeoutException();
                    }
                    await completed;
                });
            }
            finally
            {
                File.Delete(tempFile);
            }
        }