forked from squid-box/SevenZipSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SevenZipCompressorAsynchronousTests.cs
189 lines (149 loc) · 6.28 KB
/
SevenZipCompressorAsynchronousTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
namespace SevenZip.Tests
{
using System.Collections.Generic;
using System.IO;
using System.Threading;
using NUnit.Framework;
[TestFixture]
public class SevenZipCompressorAsynchronousTests : TestBase
{
[Test]
public void AsynchronousCompressDirecoryAndEventsTest()
{
var filesFoundInvoked = 0;
var fileCompressionStartedInvoked = 0;
var fileCompressionFinishedInvoked = 0;
var compressingInvoked = 0;
var compressionFinishedInvoked = 0;
var compressor = new SevenZipCompressor();
compressor.FilesFound += (o, e) => filesFoundInvoked++;
compressor.FileCompressionStarted += (o, e) => fileCompressionStartedInvoked++;
compressor.FileCompressionFinished += (o, e) => fileCompressionFinishedInvoked++;
compressor.Compressing += (o, e) => compressingInvoked++;
compressor.CompressionFinished += (o, e) => compressionFinishedInvoked++;
compressor.BeginCompressDirectory(@"TestData", TemporaryFile);
var timeToWait = 1000;
while (compressionFinishedInvoked == 0)
{
if (timeToWait <= 0)
{
break;
}
Thread.Sleep(25);
timeToWait -= 25;
}
var numberOfTestDataFiles = Directory.GetFiles("TestData").Length;
Assert.AreEqual(1, filesFoundInvoked);
Assert.AreEqual(numberOfTestDataFiles, fileCompressionStartedInvoked);
Assert.AreEqual(numberOfTestDataFiles, fileCompressionFinishedInvoked);
Assert.AreEqual(numberOfTestDataFiles, compressingInvoked);
Assert.AreEqual(1, compressionFinishedInvoked);
Assert.IsTrue(File.Exists(TemporaryFile));
}
[Test]
public void AsynchronousCompressFilesTest()
{
var compressionFinishedInvoked = false;
var compressor = new SevenZipCompressor {DirectoryStructure = false};
compressor.CompressionFinished += (o, e) => compressionFinishedInvoked = true;
compressor.BeginCompressFiles(TemporaryFile, @"TestData\zip.zip", @"TestData\tar.tar");
var timeToWait = 1000;
while (!compressionFinishedInvoked)
{
if (timeToWait <= 0)
{
break;
}
Thread.Sleep(25);
timeToWait -= 25;
}
Assert.IsTrue(File.Exists(TemporaryFile));
using (var extractor = new SevenZipExtractor(TemporaryFile))
{
Assert.AreEqual(2, extractor.FilesCount);
Assert.IsTrue(extractor.ArchiveFileNames.Contains("zip.zip"));
Assert.IsTrue(extractor.ArchiveFileNames.Contains("tar.tar"));
}
}
[Test]
public void AsynchronousCompressStreamTest()
{
var compressionFinishedInvoked = false;
var compressor = new SevenZipCompressor { DirectoryStructure = false };
compressor.CompressionFinished += (o, e) => compressionFinishedInvoked = true;
using (var inputStream = File.OpenRead(@"TestData\zip.zip"))
{
using (var outputStream = new FileStream(TemporaryFile, FileMode.Create))
{
compressor.BeginCompressStream(inputStream, outputStream);
var timeToWait = 1000;
while (!compressionFinishedInvoked)
{
if (timeToWait <= 0)
{
break;
}
Thread.Sleep(25);
timeToWait -= 25;
}
}
}
Assert.IsTrue(File.Exists(TemporaryFile));
using (var extractor = new SevenZipExtractor(TemporaryFile))
{
Assert.AreEqual(1, extractor.FilesCount);
}
}
[Test]
public void AsynchronousModifyArchiveTest()
{
var compressor = new SevenZipCompressor { DirectoryStructure = false };
compressor.CompressFiles(TemporaryFile, @"TestData\tar.tar");
var compressionFinishedInvoked = false;
compressor.CompressionFinished += (o, e) => compressionFinishedInvoked = true;
compressor.BeginModifyArchive(TemporaryFile, new Dictionary<int, string>{{0, @"tartar"}});
var timeToWait = 1000;
while (!compressionFinishedInvoked)
{
if (timeToWait <= 0)
{
break;
}
Thread.Sleep(25);
timeToWait -= 25;
}
Assert.IsTrue(File.Exists(TemporaryFile));
using (var extractor = new SevenZipExtractor(TemporaryFile))
{
Assert.AreEqual(1, extractor.FilesCount);
Assert.AreEqual("tartar", extractor.ArchiveFileNames[0]);
}
}
[Test]
public void AsynchronousCompressFilesEncryptedTest()
{
var compressionFinishedInvoked = false;
var compressor = new SevenZipCompressor { DirectoryStructure = false };
compressor.CompressionFinished += (o, e) => compressionFinishedInvoked = true;
compressor.BeginCompressFilesEncrypted(TemporaryFile, "secure", @"TestData\zip.zip", @"TestData\tar.tar");
var timeToWait = 1000;
while (!compressionFinishedInvoked)
{
if (timeToWait <= 0)
{
break;
}
Thread.Sleep(25);
timeToWait -= 25;
}
Assert.IsTrue(File.Exists(TemporaryFile));
using (var extractor = new SevenZipExtractor(TemporaryFile))
{
Assert.AreEqual(2, extractor.FilesCount);
Assert.IsTrue(extractor.ArchiveFileNames.Contains("zip.zip"));
Assert.IsTrue(extractor.ArchiveFileNames.Contains("tar.tar"));
Assert.Throws<ExtractionFailedException>(() => extractor.ExtractArchive(OutputDirectory));
}
}
}
}