Skip to content

Commit c246a4b

Browse files
committed
worked on tar.gz support
1 parent 73613e4 commit c246a4b

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

src/ArchiveFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ internal static IArchive GetArchive(ArchiveFormat format, string archivePath, Ar
2222
{
2323
ArchiveFormat.Zip => new ZipArchive(archivePath, archiveMode, archiveFileStream, compressionLevel),
2424
ArchiveFormat.Tar => new TarArchive(archivePath, archiveMode, archiveFileStream),
25-
// TODO: Add Tar.gz here
25+
ArchiveFormat.Tgz => new TarGzArchive(archivePath, archiveMode, archiveFileStream, compressionLevel),
2626
_ => throw new ArgumentOutOfRangeException(nameof(archiveMode))
2727
};
2828
}

src/TarGzArchive.cs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ internal class TarGzArchive : IArchive
2323
private readonly CompressionLevel _compressionLevel;
2424

2525
// Use a tar archive because .tar.gz file is a compressed tar file
26-
private TarArchive _tarArchive;
26+
private TarArchive? _tarArchive;
27+
28+
private string? _tarFilePath;
2729

2830
ArchiveMode IArchive.Mode => _mode;
2931

@@ -39,17 +41,22 @@ public TarGzArchive(string path, ArchiveMode mode, FileStream fileStream, Compre
3941

4042
void IArchive.AddFileSystemEntry(ArchiveAddition entry)
4143
{
44+
if (_mode == ArchiveMode.Extract) {
45+
throw new ArgumentException("Adding entries to the archive is not supported in extract mode");
46+
}
47+
4248
if (_mode == ArchiveMode.Create) {
43-
49+
if (_tarArchive is null) {
50+
_tarArchive = new TarArchive(_path, ArchiveMode.Create, _fileStream);
51+
}
52+
(_tarArchive as IArchive).AddFileSystemEntry(entry);
4453
}
4554
}
4655

4756
IEntry? IArchive.GetNextEntry()
4857
{
49-
// Gzip has no concept of entries
50-
if (!_didCallGetNextEntry) {
51-
_didCallGetNextEntry = true;
52-
return new TarGzArchiveEntry(this);
58+
if (_mode == ArchiveMode.Create || _mode == ArchiveMode.Update) {
59+
throw new ArgumentException("Getting the entries in an archive is not supported in Create or Update mode");
5360
}
5461
return null;
5562
}
@@ -62,6 +69,7 @@ protected virtual void Dispose(bool disposing)
6269
{
6370
// TODO: dispose managed state (managed objects)
6471
_fileStream.Dispose();
72+
CompressArchive();
6573
}
6674

6775
// TODO: free unmanaged resources (unmanaged objects) and override finalizer
@@ -82,6 +90,14 @@ bool IArchive.HasTopLevelDirectory()
8290
throw new NotSupportedException();
8391
}
8492

93+
// Performs gzip compression on _path
94+
private void CompressArchive() {
95+
//using var destinationFileStream = new FileStream(destinationPath, FileMode.CreateNew, FileAccess.Write, FileShare.None);
96+
_fileStream.Position = 0;
97+
using var gzipDecompressor = new GZipStream(_fileStream, _compressionLevel, true);
98+
_fileStream.CopyTo(gzipDecompressor);
99+
}
100+
85101
internal class TarGzArchiveEntry : IEntry {
86102

87103
private TarGzArchive _gzipArchive;

0 commit comments

Comments
 (0)