An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
Hi @Alin A .
In C# 10 you’re typically on .NET 6, so it’s important to separate:
1. What .NET supports
Built-in (BCL):
- .zip:
System.IO.Compression(ZipFile,ZipArchive) - .gz:
GZipStream(compression only, not a multi-file “folder archive”)
Not built-in in .NET 6:
- .tar / .tgz, .bz2, .xz, .7z, .rar (RAR creation in particular is usually not available)
Also note: GZ/BZ2/XZ are compression formats, not container archives. If you want “folder -> single file”, you normally use TAR + compression (e.g. folder.tar.gz aka .tgz), or ZIP/7Z.
2. If you need many formats: use a library
A common choice is SharpCompress (NuGet). It can create ZIP/TAR and compress with GZip/BZip2/XZ, and supports 7z (RAR is typically extract-only, not create).
If the requirement is “must be able to create .rar”, you’ll likely need an external tool (WinRAR/rar.exe) and appropriate licensing, because most .NET libraries do not generate RAR archives.
3. Minimal working implementation for ZIP (sync + async)
Below creates the archive, then deletes everything else in the output directory (the directory containing archiveFilePath), leaving only the archive. It writes to a temp file first so you don’t accidentally delete the archive while it’s being created.
using System.IO.Compression;
public static class Archiver
{
public static bool ArchiveFolder(string archiveFilePath, string sourceFolderPath)
{
try
{
var ext = Path.GetExtension(archiveFilePath);
if (!ext.Equals(".zip", StringComparison.OrdinalIgnoreCase))
throw new NotSupportedException("Only .zip is supported by this in-box example.");
Directory.CreateDirectory(Path.GetDirectoryName(archiveFilePath)!);
var temp = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".zip");
ZipFile.CreateFromDirectory(sourceFolderPath, temp, CompressionLevel.Optimal, includeBaseDirectory: false);
File.Move(temp, archiveFilePath, overwrite: true);
CleanDirectoryExceptFile(Path.GetDirectoryName(archiveFilePath)!, archiveFilePath);
return true;
}
catch
{
return false;
}
}
public static async Task<bool> ArchiveFolderAsync(string archiveFilePath, string sourceFolderPath, CancellationToken ct = default)
{
try
{
var ext = Path.GetExtension(archiveFilePath);
if (!ext.Equals(".zip", StringComparison.OrdinalIgnoreCase))
throw new NotSupportedException("Only .zip is supported by this in-box example.");
Directory.CreateDirectory(Path.GetDirectoryName(archiveFilePath)!);
var temp = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".zip");
await Task.Run(() =>
{
ct.ThrowIfCancellationRequested();
ZipFile.CreateFromDirectory(sourceFolderPath, temp, CompressionLevel.Optimal, includeBaseDirectory: false);
}, ct);
File.Move(temp, archiveFilePath, overwrite: true);
CleanDirectoryExceptFile(Path.GetDirectoryName(archiveFilePath)!, archiveFilePath);
return true;
}
catch
{
return false;
}
}
private static void CleanDirectoryExceptFile(string directoryPath, string keepFilePath)
{
var keepFull = Path.GetFullPath(keepFilePath);
foreach (var file in Directory.EnumerateFiles(directoryPath))
{
if (!Path.GetFullPath(file).Equals(keepFull, StringComparison.OrdinalIgnoreCase))
File.Delete(file);
}
foreach (var dir in Directory.EnumerateDirectories(directoryPath))
{
Directory.Delete(dir, recursive: true);
}
}
}
To support .tar, .tgz, .bz2, .xz, .7z, etc., you can implement the same “temp then move + clean” pattern, but swap out the archive writer with SharpCompress (or another archiving library).
I hope this helps.
If you found my response helpful or informative, I would greatly appreciate it if you could follow this guidance provide feedback. Thank you.