-
Notifications
You must be signed in to change notification settings - Fork 169
/
Copy pathArchive.cs
789 lines (724 loc) · 30 KB
/
Archive.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using Nexus.Client.Util.Collections;
using SevenZip;
namespace Nexus.Client.Util
{
/// <summary>
/// Encapsulates the interactions with an archive file.
/// </summary>
public class Archive : IDisposable
{
/// <summary>
/// Raised when the files in the archive have changed.
/// </summary>
public event EventHandler FilesChanged = delegate { };
/// <summary>
/// The path prefix use to identify a file as being contained in an archive.
/// </summary>
protected const string ARCHIVE_PREFIX = "arch:";
/// <summary>
/// A list of well-known extensions of files that are not archives.
/// </summary>
/// <remarks>
/// This list is used to speed up determination of whether or not a given file is an archive.
/// </remarks>
protected static List<string> m_lstNonArchiveExtensions = new List<string> { ".esp", ".esm", ".txt", ".htm", ".html", ".nif", ".dds", ".png", ".rtf", ".jpg", ".bmp", ".cs", ".xml", ".xsd", ".ico" };
private string m_strPath = null;
private SevenZipCompressor m_szcCompressor = null;
private List<string> m_strFiles = null;
private Dictionary<string, ArchiveFileInfo> m_dicFileInfo = null;
private bool m_booCanEdit = false;
private bool m_booIsSolid = false;
private ThreadSafeSevenZipExtractor m_szeReadOnlyExtractor = null;
private string m_strReadOnlyTempDirectory = null;
#region Properties
/// <summary>
/// Gets whether or not the archive is read-only.
/// </summary>
/// <remarks>
/// RAR files are the only read-only archives. This is because FOMM isn't allow to create/edit RAR files
/// (from a licensing standpoint).
/// </remarks>
/// <value>Whether or not the archive is read-only.</value>
public bool ReadOnly
{
get
{
return !m_booCanEdit;
}
}
/// <summary>
/// Gets the path of the archive.
/// </summary>
/// <value>The path of the archive.</value>
public string ArchivePath
{
get
{
return m_strPath;
}
}
/// <summary>
/// Gets the names of the volumes that make up this archive.
/// </summary>
/// <value>The names of the volumes that make up this archive.</value>
public string[] VolumeFileNames
{
get
{
using (SevenZipExtractor szeExtractor = GetExtractor(m_strPath))
{
IList<string> lstVolumes = szeExtractor.VolumeFileNames;
string[] strVolumes = new string[lstVolumes.Count];
lstVolumes.CopyTo(strVolumes, 0);
return strVolumes;
}
}
}
/// <summary>
/// Gets whether the archive is solid.
/// </summary>
/// <value>Whether the archive is solid.</value>
public bool IsSolid
{
get
{
return m_booIsSolid;
}
}
#endregion
#region Utility Methods
/// <summary>
/// Gets a <see cref="SevenZipExtractor"/> for the given path.
/// </summary>
/// <remarks>
/// This builds a <see cref="SevenZipExtractor"/> for the given path. The path can
/// be to a nested archive (an archive in another archive).
/// </remarks>
/// <param name="p_strPath">The path to the archive for which to get a <see cref="SevenZipExtractor"/>.</param>
/// <returns>A <see cref="SevenZipExtractor"/> for the given path.</returns>
public static SevenZipExtractor GetExtractor(string p_strPath)
{
return (SevenZipExtractor)GetExtractor(p_strPath, false);
}
/// <summary>
/// Gets a <see cref="ThreadSafeSevenZipExtractor"/> for the given path.
/// </summary>
/// <remarks>
/// This builds a <see cref="ThreadSafeSevenZipExtractor"/> for the given path. The path can
/// be to a nested archive (an archive in another archive).
/// </remarks>
/// <param name="p_strPath">The path to the archive for which to get a <see cref="ThreadSafeSevenZipExtractor"/>.</param>
/// <returns>A <see cref="ThreadSafeSevenZipExtractor"/> for the given path.</returns>
public static ThreadSafeSevenZipExtractor GetThreadSafeExtractor(string p_strPath)
{
return (ThreadSafeSevenZipExtractor)GetExtractor(p_strPath, true);
}
/// <summary>
/// Gets a <see cref="SevenZipExtractor"/> for the given path.
/// </summary>
/// <remarks>
/// This builds a <see cref="SevenZipExtractor"/> for the given path. The path can
/// be to a nested archive (an archive in another archive).
/// </remarks>
/// <param name="p_strPath">The path to the archive for which to get a <see cref="SevenZipExtractor"/>.</param>
/// <param name="p_booThreadSafe">Indicates if the returned extractor need to be thread safe.</param>
/// <returns>A <see cref="SevenZipExtractor"/> for the given path if the extractor doesn't need to be
/// thread safe; a <see cref="ThreadSafeSevenZipExtractor"/> otherwise.</returns>
private static object GetExtractor(string p_strPath, bool p_booThreadSafe)
{
if (p_strPath.StartsWith(Archive.ARCHIVE_PREFIX))
{
Stack<KeyValuePair<string, string>> stkFiles = new Stack<KeyValuePair<string, string>>();
string strPath = p_strPath;
while (strPath.StartsWith(Archive.ARCHIVE_PREFIX))
{
stkFiles.Push(Archive.ParseArchivePath(strPath));
strPath = stkFiles.Peek().Key;
}
Stack<SevenZipExtractor> stkExtractors = new Stack<SevenZipExtractor>();
try
{
KeyValuePair<string, string> kvpArchive = stkFiles.Pop();
SevenZipExtractor szeArchive = new SevenZipExtractor(kvpArchive.Key);
stkExtractors.Push(szeArchive);
for (; stkFiles.Count > 0; kvpArchive = stkFiles.Pop())
{
MemoryStream msmArchive = new MemoryStream();
szeArchive.ExtractFile(kvpArchive.Value, msmArchive);
msmArchive.Position = 0;
szeArchive = new SevenZipExtractor(msmArchive);
stkExtractors.Push(szeArchive);
}
MemoryStream msmFile = new MemoryStream();
szeArchive.ExtractFile(kvpArchive.Value, msmFile);
msmFile.Position = 0;
if (p_booThreadSafe)
return new ThreadSafeSevenZipExtractor(msmFile);
return new SevenZipExtractor(msmFile);
}
finally
{
while (stkExtractors.Count > 0)
stkExtractors.Pop().Dispose();
}
}
else
{
if (p_booThreadSafe)
return new ThreadSafeSevenZipExtractor(p_strPath);
return new SevenZipExtractor(p_strPath);
}
}
/// <summary>
/// Determines whether or not the file specified by the given path
/// is an archive.
/// </summary>
/// <returns><c>true</c> if the specified file is an archive;
/// <c>false</c> otherwise.</returns>
public static bool IsArchive(string p_strPath)
{
if (!p_strPath.StartsWith(ARCHIVE_PREFIX) && !File.Exists(p_strPath))
return false;
bool booIsAchive = true;
if (m_lstNonArchiveExtensions.Contains(Path.GetExtension(p_strPath)))
return false;
try
{
using (SevenZipExtractor szeExtractor = GetExtractor(p_strPath))
{
UInt32 g = szeExtractor.FilesCount;
}
}
catch (Exception)
{
booIsAchive = false;
}
return booIsAchive;
}
/// <summary>
/// Determines whether or not the given path points to a file in an archive.
/// </summary>
/// <returns><c>true</c> if the given path points to a file in an archive;
/// <c>false</c> otherwise.</returns>
public static bool IsArchivePath(string p_strPath)
{
return !String.IsNullOrEmpty(p_strPath) && p_strPath.StartsWith(ARCHIVE_PREFIX, StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Changes the directory of the archive referenced in the given path to the specified
/// new directory.
/// </summary>
/// <remarks>
/// This changes something of the form:
/// arch:old\path\archive.zip//interior/path/file.txt
/// to:
/// arch:new\path\archive.zip//interior/path/file.txt
/// </remarks>
/// <param name="p_strArchivePath">The archive path whose directory is to be replaced.</param>
/// <param name="p_strNewArchiveDirectory">The new directory to put into the given archive path.</param>
/// <returns>The archive path with the new directory.</returns>
public static string ChangeArchiveDirectory(string p_strArchivePath, string p_strNewArchiveDirectory)
{
if (!p_strArchivePath.StartsWith(ARCHIVE_PREFIX))
throw new ArgumentException("The given path is not an archive path: " + p_strArchivePath, "p_strArchivePath");
string strNewDirectory = p_strNewArchiveDirectory ?? "";
KeyValuePair<string, string> kvpArchive = ParseArchivePath(p_strArchivePath);
Stack<string> stkArchives = new Stack<string>();
while (kvpArchive.Key.StartsWith(ARCHIVE_PREFIX))
{
stkArchives.Push(kvpArchive.Value);
kvpArchive = ParseArchivePath(kvpArchive.Key);
}
string strSource = GenerateArchivePath(Path.Combine(strNewDirectory, Path.GetFileName(kvpArchive.Key)), kvpArchive.Value);
while (stkArchives.Count > 0)
strSource = GenerateArchivePath(strSource, stkArchives.Pop());
return strSource;
}
/// <summary>
/// Parses the given path to extract the path to the archive file, and the path to
/// a file within said archive.
/// </summary>
/// <param name="p_strPath">The file path to parse.</param>
/// <returns>The path to an archive file, and the path to a file within said archive.</returns>
public static KeyValuePair<string, string> ParseArchivePath(string p_strPath)
{
if (!p_strPath.StartsWith(ARCHIVE_PREFIX))
return new KeyValuePair<string, string>(null, null);
Int32 intEndIndex = p_strPath.LastIndexOf("//");
if (intEndIndex < 0)
intEndIndex = p_strPath.Length;
string strArchive = p_strPath.Substring(ARCHIVE_PREFIX.Length, intEndIndex - ARCHIVE_PREFIX.Length);
string strPath = p_strPath.Substring(intEndIndex + 2);
return new KeyValuePair<string, string>(strArchive, strPath);
}
/// <summary>
/// Generates a path to a file in an archive.
/// </summary>
/// <param name="p_strArchivePath">The path of the archive file.</param>
/// <param name="p_strInternalPath">The path of the file in the archive.</param>
/// <returns></returns>
public static string GenerateArchivePath(string p_strArchivePath, string p_strInternalPath)
{
return String.Format("{0}{1}//{2}", Archive.ARCHIVE_PREFIX, p_strArchivePath, p_strInternalPath);
}
#endregion
#region Constructors
/// <summary>
/// A simple constructor the initializes the object with the given values.
/// </summary>
/// <param name="p_strPath">The path to the archive file.</param>
public Archive(string p_strPath)
{
m_strPath = p_strPath;
if (!p_strPath.StartsWith(ARCHIVE_PREFIX))
{
m_strPath = p_strPath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
using (SevenZipExtractor szeExtractor = new SevenZipExtractor(m_strPath))
{
if (Enum.IsDefined(typeof(OutArchiveFormat), szeExtractor.Format.ToString()))
{
m_szcCompressor = new SevenZipCompressor();
m_szcCompressor.CompressionMode = CompressionMode.Append;
m_szcCompressor.ArchiveFormat = (OutArchiveFormat)Enum.Parse(typeof(OutArchiveFormat), szeExtractor.Format.ToString());
m_booCanEdit = true;
}
}
}
m_dicFileInfo = new Dictionary<string, ArchiveFileInfo>(StringComparer.OrdinalIgnoreCase);
m_strFiles = new List<string>();
LoadFileIndices();
}
#endregion
#region Read Transactions
/// <summary>
/// Raised to update listeners on the progress of the read-only initialization.
/// </summary>
public event CancelProgressEventHandler ReadOnlyInitProgressUpdated = delegate { };
/// <summary>
/// Gets whether the archive is in read-only mode.
/// </summary>
/// <remarks>
/// Read-only mode maintains a single extractor for all operations, greatly increasing
/// extraction speed as the extractor isn't created/destroyed for each operation. While
/// in read-only mod the underlying file is left open (this class holds a handle to the file).
/// </remarks>
/// <value>Whether the archive is in read-only mode.</value>
protected bool IsReadonly
{
get
{
return ((object)m_strReadOnlyTempDirectory ?? m_szeReadOnlyExtractor) != null;
}
}
/// <summary>
/// Starts a read-only transaction.
/// </summary>
/// <remarks>
/// This puts the archive into read-only mode.
/// </remarks>
/// <param name="p_futFileUtil">An instance of a <see cref="FileUtil"/> class.</param>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="p_futFileUtil"/> is <c>null</c>.</exception>
public void BeginReadOnlyTransaction(FileUtil p_futFileUtil)
{
if (m_szeReadOnlyExtractor == null)
{
if (p_futFileUtil == null)
throw new ArgumentNullException("p_futFileUtil");
m_szeReadOnlyExtractor = GetThreadSafeExtractor(m_strPath);
if (m_szeReadOnlyExtractor.IsSolid)
{
m_szeReadOnlyExtractor.Dispose();
m_szeReadOnlyExtractor = null;
m_strReadOnlyTempDirectory = p_futFileUtil.CreateTempDirectory();
using (SevenZipExtractor szeExtractor = Archive.GetExtractor(m_strPath))
{
szeExtractor.FileExtractionFinished += new EventHandler<FileInfoEventArgs>(FileExtractionFinished);
szeExtractor.ExtractArchive(m_strReadOnlyTempDirectory);
}
}
}
}
#region Callbacks
/// <summary>
/// Called when a file has been extracted from a source archive.
/// </summary>
/// <remarks>
/// This notifies listeners that a read-only initialization step has finished.
/// </remarks>
/// <param name="sender">The object that raised the event.</param>
/// <param name="e">An <see cref="EventArgs"/> describing the event arguments.</param>
private void FileExtractionFinished(object sender, FileInfoEventArgs e)
{
CancelProgressEventArgs ceaArgs = new CancelProgressEventArgs((float)e.PercentDone / 100f);
ReadOnlyInitProgressUpdated(this, ceaArgs);
e.Cancel = ceaArgs.Cancel;
}
#endregion
/// <summary>
/// Ends a read-only transaction.
/// </summary>
/// <remarks>
/// This takes the archive out of read-only mode, and releases any used resources.
/// </remarks>
public void EndReadOnlyTransaction()
{
if (m_szeReadOnlyExtractor != null)
m_szeReadOnlyExtractor.Dispose();
m_szeReadOnlyExtractor = null;
if (!String.IsNullOrEmpty(m_strReadOnlyTempDirectory))
FileUtil.ForceDelete(m_strReadOnlyTempDirectory);
m_strReadOnlyTempDirectory = null;
}
#endregion
/// <summary>
/// Caches information about the files in the archive.
/// </summary>
protected void LoadFileIndices()
{
m_dicFileInfo.Clear();
m_strFiles.Clear();
using (SevenZipExtractor szeExtractor = GetExtractor(m_strPath))
{
try
{
m_booIsSolid = szeExtractor.IsSolid;
foreach (ArchiveFileInfo afiFile in szeExtractor.ArchiveFileData)
if (!afiFile.IsDirectory)
{
string afiFileName = afiFile.FileName.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
if (afiFileName.StartsWith(Path.DirectorySeparatorChar.ToString()) && afiFileName.Length > 1)
afiFileName = afiFileName.Substring(1);
m_dicFileInfo[afiFileName] = afiFile;
m_strFiles.Add(afiFileName);
}
}
catch { }
}
FilesChanged(this, new EventArgs());
}
/// <summary>
/// Determins if the given path is a directory in this archive.
/// </summary>
/// <param name="p_strPath">The path to examine.</param>
/// <returns><c>true</c> if the given path is a directory in this archive;
/// <c>false</c> otherwise.</returns>
public bool IsDirectory(string p_strPath)
{
string strPath = p_strPath.Trim(new char[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar });
strPath = strPath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
string strPathWithSep = strPath + Path.DirectorySeparatorChar;
if (m_dicFileInfo.ContainsKey(strPath))
return false;
foreach (string strFile in m_dicFileInfo.Keys)
if (strFile.StartsWith(strPathWithSep, StringComparison.InvariantCultureIgnoreCase))
return true;
ArchiveFileInfo afiFile = default(ArchiveFileInfo);
string strArchiveFileName = null;
using (SevenZipExtractor szeExtractor = GetExtractor(m_strPath))
foreach (ArchiveFileInfo afiTmp in szeExtractor.ArchiveFileData)
{
strArchiveFileName = afiTmp.FileName.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
if (strArchiveFileName.Equals(strPath, StringComparison.InvariantCultureIgnoreCase))
{
afiFile = afiTmp;
break;
}
}
return (afiFile == null) ? false : afiFile.IsDirectory;
}
/// <summary>
/// Gets a list of directories that are in the specified directory in this archive.
/// </summary>
/// <param name="p_strDirectory">The directory in the archive whose descendents are to be returned.</param>
/// <returns>A list of directories that are in the specified directory in this archive.</returns>
public string[] GetDirectories(string p_strDirectory)
{
string strPrefix = p_strDirectory;
if (String.IsNullOrEmpty(p_strDirectory))
strPrefix = "";
strPrefix = strPrefix.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
strPrefix = strPrefix.Trim(Path.DirectorySeparatorChar);
if (strPrefix.Length > 0)
strPrefix += Path.DirectorySeparatorChar;
Set<string> lstFolders = new Set<string>(StringComparer.InvariantCultureIgnoreCase);
Int32 intStopIndex = 0;
foreach (string strFile in m_strFiles)
{
if (strFile.StartsWith(strPrefix, StringComparison.InvariantCultureIgnoreCase))
{
intStopIndex = strFile.IndexOf(Path.DirectorySeparatorChar, strPrefix.Length);
if (intStopIndex < 0)
continue;
lstFolders.Add(String.Copy(strFile.Substring(0, intStopIndex)));
}
}
return lstFolders.ToArray();
}
/// <summary>
/// Gets a list of files that are in the specified directory in this archive.
/// </summary>
/// <param name="p_strDirectory">The directory in the archive whose descendents are to be returned.</param>
/// <param name="p_booRecurse">Whether to return files that are in subdirectories of the given directory.</param>
/// <returns>A list of files that are in the specified directory in this archive.</returns>
public string[] GetFiles(string p_strDirectory, bool p_booRecurse)
{
Set<string> lstFiles = new Set<string>(StringComparer.InvariantCultureIgnoreCase);
if (String.IsNullOrEmpty(p_strDirectory))
{
m_strFiles.ForEach((s) => { lstFiles.Add(String.Copy(s)); });
}
else
{
string strPrefix = p_strDirectory;
strPrefix = strPrefix.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
strPrefix = strPrefix.Trim(Path.DirectorySeparatorChar);
if (strPrefix.Length > 0)
strPrefix += Path.DirectorySeparatorChar;
Int32 intStopIndex = 0;
foreach (string strFile in m_strFiles)
{
if (strFile.StartsWith(strPrefix, StringComparison.InvariantCultureIgnoreCase))
{
if (!p_booRecurse)
{
intStopIndex = strFile.IndexOf(Path.DirectorySeparatorChar, strPrefix.Length);
if (intStopIndex > 0)
continue;
}
lstFiles.Add(String.Copy(strFile));
}
}
}
return lstFiles.ToArray();
}
/// <summary>
/// Gets a list of files that are in the specified directory and match the given pattern in this archive.
/// </summary>
/// <param name="p_strDirectory">The directory in the archive whose descendents are to be returned.</param>
/// <param name="p_strPattern">The filename pattern of the files to be returned.</param>
/// <param name="p_booRecurse">Whether to return files that are in subdirectories of the given directory.</param>
/// <returns>A list of files that are in the specified directory and match the given pattern in this archive.</returns>
public string[] GetFiles(string p_strDirectory, string p_strPattern, bool p_booRecurse)
{
Set<string> lstFiles = new Set<string>(StringComparer.InvariantCultureIgnoreCase);
string[] strFiles = GetFiles(p_strDirectory, p_booRecurse);
string strSeparatorChar = Path.DirectorySeparatorChar.Equals('\\') ? @"\\" : Path.DirectorySeparatorChar.ToString();
string strPattern = p_strPattern.Replace(".", "\\.").Replace("*", ".*").Replace(Path.AltDirectorySeparatorChar.ToString(), strSeparatorChar);
Regex rgxPattern = new Regex(strPattern);
foreach (string strFile in strFiles)
if (rgxPattern.IsMatch(strFile))
lstFiles.Add(strFile);
return lstFiles.ToArray();
}
/// <summary>
/// Gets a list of files that are in the specified directory and match the given pattern in this archive.
/// </summary>
/// <param name="p_strDirectory">The directory in the archive whose descendents are to be returned.</param>
/// <param name="p_strPattern">The filename pattern of the files to be returned.</param>
/// <param name="p_booRecurse">Whether to return files that are in subdirectories of the given directory.</param>
/// <returns>A list of files that are in the specified directory and match the given pattern in this archive.</returns>
public string[] GetFilesWithExtension(string p_strDirectory, string p_strPattern, bool p_booRecurse)
{
Set<string> lstFiles = new Set<string>(StringComparer.InvariantCultureIgnoreCase);
string[] strFiles = GetFiles(p_strDirectory, p_booRecurse);
foreach (string strFile in strFiles)
if (!string.IsNullOrEmpty(strFile) && !string.IsNullOrEmpty(Path.GetExtension(strFile)))
if (Path.GetExtension(strFile).EndsWith(p_strPattern))
lstFiles.Add(strFile);
return lstFiles.ToArray();
}
/// <summary>
/// Determins if the archive contains the specified file.
/// </summary>
/// <param name="p_strPath">The path of the file whose presence in the archive is to be determined.</param>
/// <returns><c>true</c> if the file is in the archive;
/// <c>false</c> otherwise.</returns>
public bool ContainsFile(string p_strPath)
{
string strPath = p_strPath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
return m_dicFileInfo.ContainsKey(strPath);
}
/// <summary>
/// Gets the contents of the specified file in the archive.
/// </summary>
/// <param name="p_strPath">The file whose contents are to be retrieved.</param>
/// <returns>The contents of the specified file in the archive.</returns>
public byte[] GetFileContents(string p_strPath)
{
string strPath = p_strPath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
if (!m_dicFileInfo.ContainsKey(strPath))
throw new FileNotFoundException("The requested file does not exist in the archive.", p_strPath);
byte[] bteFile = null;
ArchiveFileInfo afiFile = m_dicFileInfo[strPath];
bteFile = new byte[afiFile.Size];
using (MemoryStream msmFile = new MemoryStream(bteFile))
{
//check to see if we are on the same thread as the extractor
// if not, then marshall the call to the extractor's thread.
// this needs to be done as the 7zip dll cannot handle calls from other
// threads.
if (IsReadonly)
{
if (m_szeReadOnlyExtractor == null)
{
string path = Path.Combine(m_strReadOnlyTempDirectory, strPath);
if (!File.Exists(path))
throw new FileNotFoundException("The requested file does not exist in the temp archive.", path);
bteFile = File.ReadAllBytes(path);
}
else
m_szeReadOnlyExtractor.ExtractFile(afiFile.Index, msmFile);
}
else
{
using (SevenZipExtractor szeExtractor = GetExtractor(m_strPath))
szeExtractor.ExtractFile(afiFile.Index, msmFile);
}
msmFile.Close();
}
if (bteFile.LongLength != (Int64)afiFile.Size)
{
//if I understand things correctly, this block should never execute
// as bteFile should always be exactly the right size to hold the extracted file
//however, just to be safe, I've included this code to make sure we only return
// valid bytes
byte[] bteReal = new byte[afiFile.Size];
Array.Copy(bteFile, bteReal, bteReal.LongLength);
bteFile = bteReal;
}
return bteFile;
}
/// <summary>
/// Gets a FileStream to the specified file in the archive (will be temporarily unpacked).
/// </summary>
/// <param name="p_strPath">The file whose contents are to be retrieved.</param>
/// <param name="p_strTemporaryDirectory">Temporary directory of the application.</param>
/// <returns></returns>
public FileStream GetFileStream(string p_strPath, string p_strTemporaryDirectory)
{
string strPath = p_strPath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
string strTempFile = Path.Combine(p_strTemporaryDirectory, "tempfile_" + Path.GetRandomFileName());
if (!Directory.Exists(p_strTemporaryDirectory))
{
Directory.CreateDirectory(p_strTemporaryDirectory);
}
if (!m_dicFileInfo.ContainsKey(strPath))
{
throw new FileNotFoundException("The requested file does not exist in the archive.", p_strPath);
}
ArchiveFileInfo afiFile = m_dicFileInfo[strPath];
string strFilePath;
if (IsReadonly)
{
if (m_szeReadOnlyExtractor == null)
{
strFilePath = Path.Combine(m_strReadOnlyTempDirectory, strPath);
}
else
{
using (var sw = new StreamWriter(strTempFile, false))
{
m_szeReadOnlyExtractor.ExtractFile(afiFile.Index, sw.BaseStream);
}
strFilePath = strTempFile;
}
}
else
{
using (SevenZipExtractor szeExtractor = GetExtractor(m_strPath))
{
using (var sw = new StreamWriter(strTempFile, false))
{
szeExtractor.ExtractFile(afiFile.Index, sw.BaseStream);
}
strFilePath = strTempFile;
}
}
return new FileStream(strFilePath, FileMode.Open);
}
/// <summary>
/// Replaces the specified file in the archive with the given data.
/// </summary>
/// <remarks>
/// If the specified file doesn't exist in the archive, the file is added.
/// </remarks>
/// <param name="p_strFileName">The path to the file to replace in the archive.</param>
/// <param name="p_strData">The new file data.</param>
public void ReplaceFile(string p_strFileName, string p_strData)
{
ReplaceFile(p_strFileName, Encoding.Default.GetBytes(p_strData));
}
/// <summary>
/// Replaces the specified file in the archive with the given data.
/// </summary>
/// <remarks>
/// If the specified file doesn't exist in the archive, the file is added.
/// </remarks>
/// <param name="p_strFileName">The path to the file to replace in the archive.</param>
/// <param name="p_bteData">The new file data.</param>
/// <exception cref="InvalidOperationException">Thrown if modification of archives of the current
/// archive type is not supported.</exception>
/// <exception cref="InvalidOperationException">Thrown if modification of archive is attempted
/// while the archive is in a ready only transaction.</exception>
public void ReplaceFile(string p_strFileName, byte[] p_bteData)
{
if (IsReadonly)
throw new InvalidOperationException("Cannot replace a file while Archive is in a Read Only Transaction.");
if (!m_booCanEdit)
using (SevenZipExtractor szeExtractor = GetExtractor(m_strPath))
throw new InvalidOperationException("Cannot modify archive of type: " + szeExtractor.Format);
string strPath = p_strFileName.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
if (m_dicFileInfo.ContainsKey(strPath))
{
Dictionary<int, string> dicDelete = new Dictionary<int, string>() { { m_dicFileInfo[strPath].Index, null } };
m_szcCompressor.ModifyArchive(m_strPath, dicDelete);
}
using (MemoryStream msmData = new MemoryStream(p_bteData))
{
m_szcCompressor.CompressStreamDictionary(new Dictionary<string, Stream>() { { p_strFileName, msmData } }, m_strPath);
msmData.Close();
}
LoadFileIndices();
}
/// <summary>
/// Deletes the specified file from the archive.
/// </summary>
/// <remarks>
/// If the specified file doesn't exist in the archive, nothing is done.
/// </remarks>
/// <param name="p_strFileName">The path to the file to delete from the archive.</param>
/// <exception cref="InvalidOperationException">Thrown if modification of archives of the current
/// archive type is not supported.</exception>
/// <exception cref="InvalidOperationException">Thrown if modification of archive is attempted
/// while the archive is in a ready only transaction.</exception>
public void DeleteFile(string p_strFileName)
{
if (IsReadonly)
throw new InvalidOperationException("Cannot delete a file while Archive is in a Read Only Transaction.");
if (!m_booCanEdit)
using (SevenZipExtractor szeExtractor = GetExtractor(m_strPath))
throw new InvalidOperationException("Cannot modify archive of type: " + szeExtractor.Format);
string strPath = p_strFileName.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
if (m_dicFileInfo.ContainsKey(strPath))
{
Dictionary<int, string> dicDelete = new Dictionary<int, string>() { { m_dicFileInfo[strPath].Index, null } };
m_szcCompressor.ModifyArchive(m_strPath, dicDelete);
}
LoadFileIndices();
}
#region IDisposable Members
/// <summary>
/// Disposes of the resources used by the object.
/// </summary>
public void Dispose()
{
EndReadOnlyTransaction();
}
#endregion
}
}