|
2 | 2 | // Licensed under the MIT License. |
3 | 3 |
|
4 | 4 | using System; |
5 | | -using System.Collections.Generic; |
6 | | -using System.Formats.Tar; |
7 | 5 | using System.IO; |
8 | 6 | using System.IO.Compression; |
9 | 7 | using System.Diagnostics; |
10 | 8 |
|
11 | 9 | namespace Microsoft.PowerShell.Archive |
12 | 10 | { |
13 | | - internal class TarGzArchive : GzipArchive |
| 11 | + internal class TarGzArchive : IArchive |
14 | 12 | { |
15 | 13 |
|
16 | 14 | // Use a tar archive because .tar.gz file is a compressed tar file |
17 | 15 | private TarArchive? _tarArchive; |
18 | 16 |
|
| 17 | + private FileStream? _tarFileStream; |
| 18 | + |
19 | 19 | private string? _tarFilePath; |
20 | 20 |
|
21 | | - private FileStream? _tarFileStream; |
| 21 | + private string _path; |
| 22 | + |
| 23 | + private bool _disposedValue; |
| 24 | + |
| 25 | + private bool _didCallGetNextEntry; |
| 26 | + |
| 27 | + private readonly ArchiveMode _mode; |
| 28 | + |
| 29 | + private readonly FileStream _fileStream; |
| 30 | + |
| 31 | + private readonly CompressionLevel _compressionLevel; |
| 32 | + |
| 33 | + public string Path => _path; |
22 | 34 |
|
23 | | - public TarGzArchive(string path, ArchiveMode mode, FileStream fileStream, CompressionLevel compressionLevel) : base(path, mode, fileStream, compressionLevel) |
| 35 | + ArchiveMode IArchive.Mode => _mode; |
| 36 | + |
| 37 | + string IArchive.Path => _path; |
| 38 | + |
| 39 | + public TarGzArchive(string path, ArchiveMode mode, FileStream fileStream, CompressionLevel compressionLevel) |
24 | 40 | { |
| 41 | + _path = path; |
| 42 | + _mode = mode; |
| 43 | + _fileStream = fileStream; |
| 44 | + _compressionLevel = compressionLevel; |
25 | 45 | } |
26 | 46 |
|
27 | | - public override void AddFileSystemEntry(ArchiveAddition entry) |
| 47 | + public void AddFileSystemEntry(ArchiveAddition entry) |
28 | 48 | { |
29 | | - if (_mode == ArchiveMode.Extract || _mode == ArchiveMode.Update) { |
| 49 | + if (_mode == ArchiveMode.Extract || _mode == ArchiveMode.Update) |
| 50 | + { |
30 | 51 | throw new ArgumentException("Adding entries to the archive is not supported in extract or update mode"); |
31 | 52 | } |
32 | 53 |
|
33 | 54 | if (_tarArchive is null) |
34 | 55 | { |
35 | | - var outputDirectory = Path.GetDirectoryName(_path); |
36 | | - var tarFilename = Path.GetRandomFileName(); |
37 | | - _tarFilePath = Path.Combine(outputDirectory, tarFilename); |
38 | | - _tarFileStream = new FileStream(_tarFilePath, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None); |
| 56 | + // This will create a temp file and return the path |
| 57 | + _tarFilePath = System.IO.Path.GetTempFileName(); |
| 58 | + // When creating the stream, the file already exists |
| 59 | + _tarFileStream = new FileStream(_tarFilePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None); |
39 | 60 | _tarArchive = new TarArchive(_tarFilePath, ArchiveMode.Create, _tarFileStream); |
40 | 61 |
|
41 | 62 | } |
42 | 63 | _tarArchive.AddFileSystemEntry(entry); |
43 | 64 | } |
44 | 65 |
|
45 | | - protected override void Dispose(bool disposing) |
| 66 | + public IEntry? GetNextEntry() |
| 67 | + { |
| 68 | + if (_mode == ArchiveMode.Create) |
| 69 | + { |
| 70 | + throw new ArgumentException("Getting next entry is not supported when the archive is in Create mode"); |
| 71 | + } |
| 72 | + |
| 73 | + if (_tarArchive is null) |
| 74 | + { |
| 75 | + // Create a Gzip archive |
| 76 | + using var gzipArchive = new GzipArchive(_path, _mode, _fileStream, _compressionLevel); |
| 77 | + // Where to put the tar file when expanding the tar.gz archive |
| 78 | + _tarFilePath = System.IO.Path.GetTempFileName(); |
| 79 | + // Expand the gzip portion |
| 80 | + var entry = gzipArchive.GetNextEntry(); |
| 81 | + Debug.Assert(entry is not null); |
| 82 | + entry.ExpandTo(_tarFilePath); |
| 83 | + // Create a TarArchive pointing to the newly expanded out tar file from the tar.gz file |
| 84 | + FileStream tarFileStream = new FileStream(_tarFilePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None); |
| 85 | + _tarArchive = new TarArchive(_tarFilePath, ArchiveMode.Extract, tarFileStream); |
| 86 | + } |
| 87 | + return _tarArchive?.GetNextEntry(); |
| 88 | + } |
| 89 | + |
| 90 | + public void Dispose() |
| 91 | + { |
| 92 | + // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method |
| 93 | + Dispose(disposing: true); |
| 94 | + GC.SuppressFinalize(this); |
| 95 | + } |
| 96 | + |
| 97 | + // Performs gzip compression on _path |
| 98 | + private void CompressArchive() { |
| 99 | + Debug.Assert(_tarFilePath is not null); |
| 100 | + _tarFileStream = new FileStream(_tarFilePath, FileMode.Open, FileAccess.Read); |
| 101 | + using var gzipCompressor = new GZipStream(_fileStream, _compressionLevel, true); |
| 102 | + _tarFileStream.CopyTo(gzipCompressor); |
| 103 | + _tarFileStream.Dispose(); |
| 104 | + } |
| 105 | + |
| 106 | + private void Dispose(bool disposing) |
46 | 107 | { |
47 | 108 | if (!_disposedValue) |
48 | 109 | { |
49 | 110 | if (disposing) |
50 | 111 | { |
51 | | - // TODO: dispose managed state (managed objects) |
| 112 | + // Do this before compression because disposing a tar archive will add necessary EOF markers |
| 113 | + _tarArchive?.Dispose(); |
| 114 | + if (_mode == ArchiveMode.Create) { |
| 115 | + CompressArchive(); |
| 116 | + } |
52 | 117 | _fileStream.Dispose(); |
53 | | - CompressArchive(); |
| 118 | + if (_tarFilePath is not null) { |
| 119 | + // Delete the tar file created in the process of created the tar.gz file |
| 120 | + File.Delete(_tarFilePath); |
| 121 | + } |
54 | 122 | } |
55 | 123 |
|
56 | 124 | // TODO: free unmanaged resources (unmanaged objects) and override finalizer |
57 | 125 | // TODO: set large fields to null |
58 | 126 | _disposedValue = true; |
59 | 127 | } |
60 | 128 | } |
61 | | - |
62 | | - // Performs gzip compression on _path |
63 | | - private void CompressArchive() { |
64 | | - Debug.Assert(_tarFileStream is not null); |
65 | | - _tarFileStream.Position = 0; |
66 | | - using var gzipCompressor = new GZipStream(_fileStream, _compressionLevel, true); |
67 | | - _tarFileStream.CopyTo(gzipCompressor); |
68 | | - } |
69 | 129 | } |
70 | 130 | } |
0 commit comments