Files
duplicati/Duplicati/Library/Compression/TarZstdCompression/FileArchiveTarGzip.cs
T
Kenneth Skovhede 3ec07aaee1 Added zstd compression
This PR adds experimental support for using either ZStandard or GZip compression. Unlike the Zip module, which also supports these methods, the new module does a full-volume compression which is both much faster and compresses better with modern compression algorithms.

Because the compressor can see the full volume (and not just the blocks) it can compress data across blocks.

The downside to this is that it is not possible to take a single entry out of the compressed stream, but instead, Duplicati needs to decompress the whole stream and can then access the contents.

To make the implementation compatible with standard tools, the inner format is Tar (using ustar format). To allow faster reading, a small end-of-file header is added to the Tar file, emulating the concept from Zip files.

This small addition allows Duplicati to have random access to files in a Tar volume without needing to scan the whole thing.

To make sure data is always recoverable, the format is 100% compatible with regular tools, so `untar -xf` will work on the created volumes.

The downside is an extra pass for decompression when reading the volumes. When writing, each entry (a block of data most commonly) is written to a temporary file before being added to the output stream. It is possible to toggle this to use a memory buffer for even more speed up.

The ZStandard compression is from `ZstdSharp.Port` which has excellent performance.

When we move to .NET11 this can easily be changed to the new built-in module.

Since this is the first attempt to add this, it will log a warning on each use, explaining that the feature is currently just for testing.

Co-authored-by: Copilot <copilot@github.com>
2026-04-30 13:03:24 +02:00

110 lines
4.2 KiB
C#

// Copyright (C) 2026, The Duplicati Team
// https://duplicati.com, hello@duplicati.com
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
#nullable enable
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using Duplicati.Library.Interface;
namespace Duplicati.Library.Compression.TarZstdCompression;
/// <summary>
/// An ICompression implementation using Tar+GZip with EOF header for fast random access.
/// File format: [GZip Stream] containing [Tar Archive] + [.eof-header with dictionary]
/// </summary>
public class FileArchiveTarGzip : FileArchiveTarBased
{
private const string COMPRESSION_LEVEL_OPTION = "tgz-compression-level";
private const string MEMORY_BUFFER_OPTION = "tgz-memory-buffer";
private const int DEFAULT_COMPRESSION_LEVEL = 2;
private const int MIN_COMPRESSION_LEVEL = 0;
private const int MAX_COMPRESSION_LEVEL = 3;
/// <summary>
/// Default constructor for module discovery
/// </summary>
public FileArchiveTarGzip() : base()
{
}
/// <summary>
/// Constructor with stream and options
/// </summary>
public FileArchiveTarGzip(Stream stream, ArchiveMode mode, IReadOnlyDictionary<string, string?> options)
: base(stream, mode, options)
{
}
public override string FilenameExtension => "tgz";
public override string DisplayName => Strings.FileArchiveTarGzip.DisplayName;
public override string Description => Strings.FileArchiveTarGzip.Description;
protected override string CompressionLevelOption => COMPRESSION_LEVEL_OPTION;
protected override int DefaultCompressionLevel => DEFAULT_COMPRESSION_LEVEL;
protected override int MinCompressionLevel => MIN_COMPRESSION_LEVEL;
protected override int MaxCompressionLevel => MAX_COMPRESSION_LEVEL;
protected override string MemoryBufferOption => MEMORY_BUFFER_OPTION;
public override IList<ICommandLineArgument> SupportedCommands =>
[
new CommandLineArgument(
COMPRESSION_LEVEL_OPTION,
CommandLineArgument.ArgumentType.Enumeration,
Strings.FileArchiveTarGzip.CompressionlevelShort,
Strings.FileArchiveTarGzip.CompressionlevelLong,
DEFAULT_COMPRESSION_LEVEL.ToString(),
null,
["0", "1", "2", "3"]
),
new CommandLineArgument(
MEMORY_BUFFER_OPTION,
CommandLineArgument.ArgumentType.Boolean,
Strings.FileArchiveTarGzip.MemorybufferShort,
Strings.FileArchiveTarGzip.MemorybufferLong,
"false"
)
];
protected override Stream CreateDecompressorStream(Stream inputStream)
=> new GZipStream(inputStream, CompressionMode.Decompress);
protected override Stream CreateCompressorStream(Stream outputStream, int compressionLevel)
=> new GZipStream(outputStream, MapToGZipLevel(compressionLevel), leaveOpen: true);
private static CompressionLevel MapToGZipLevel(int level)
=> level switch
{
0 => CompressionLevel.NoCompression,
1 => CompressionLevel.Fastest,
2 => CompressionLevel.Optimal,
3 => CompressionLevel.SmallestSize,
_ => CompressionLevel.Optimal
};
}