2016-09-29 10:55:04 +01:00
# SharpCompress Usage
2026-06-06 15:50:49 +01:00
## Async/Await Support
2025-10-27 09:00:38 +00:00
SharpCompress now provides full async/await support for all I/O operations. All `Read` , `Write` , and extraction operations have async equivalents ending in `Async` that accept an optional `CancellationToken` . This enables better performance and scalability for I/O-bound operations.
**Key Async Methods:**
- `reader.WriteEntryToAsync(stream, cancellationToken)` - Extract entry asynchronously
2026-03-02 07:00:33 +00:00
- `reader.WriteAllToDirectoryAsync(path, cancellationToken: cancellationToken)` - Extract all asynchronously
2025-10-27 09:00:38 +00:00
- `writer.WriteAsync(filename, stream, modTime, cancellationToken)` - Write entry asynchronously
- `writer.WriteAllAsync(directory, pattern, searchOption, cancellationToken)` - Write directory asynchronously
- `entry.OpenEntryStreamAsync(cancellationToken)` - Open entry stream asynchronously
See [Async Examples ](#async-examples ) section below for usage patterns.
2026-01-08 15:34:48 +00:00
## Stream Rules
2018-03-15 08:55:06 +00:00
2018-05-16 08:51:33 +01:00
When dealing with Streams, the rule should be that you don't close a stream you didn't create. This, in effect, should mean you should always put a Stream in a using block to dispose it.
2016-09-29 10:55:04 +01:00
However, the .NET Framework often has classes that will dispose streams by default to make things "easy" like the following:
```C#
using (var reader = new StreamReader(File.Open("foo")))
{
...
}
```
2018-03-15 08:55:06 +00:00
In this example, reader should get disposed. However, stream rules should say the the `FileStream` created by `File.Open` should remain open. However, the .NET Framework closes it for you by default unless you override the constructor. In general, you should be writing Stream code like this:
2016-09-29 10:55:04 +01:00
```C#
using (var fileStream = File.Open("foo"))
using (var reader = new StreamReader(fileStream))
{
...
}
```
2018-05-16 08:51:33 +01:00
To deal with the "correct" rules as well as the expectations of users, I've decided to always close wrapped streams as of 0.21.
2016-09-29 10:55:04 +01:00
To be explicit though, consider always using the overloads that use `ReaderOptions` or `WriterOptions` and explicitly set `LeaveStreamOpen` the way you want.
2026-02-10 10:48:42 +00:00
Default behavior in factory APIs:
- File path / `FileInfo` overloads set `LeaveStreamOpen = false` .
- Caller-provided `Stream` overloads set `LeaveStreamOpen = true` .
2024-08-29 12:11:19 +03:00
If using Compression Stream classes directly and you don't want the wrapped stream to be closed. Use the `NonDisposingStream` as a wrapper to prevent the stream being disposed. The change in 0.21 simplified a lot even though the usage is a bit more convoluted.
2018-05-16 08:51:33 +01:00
2016-09-29 10:55:04 +01:00
## Samples
2018-05-05 15:08:56 +01:00
Also, look over the tests for more thorough [examples ](https://github.com/adamhathcock/sharpcompress/tree/master/tests/SharpCompress.Test )
2016-09-29 11:10:11 +01:00
2023-06-09 19:38:42 -03:00
### Create Zip Archive from multiple files
```C#
2026-01-15 13:04:09 +00:00
using(var archive = ZipArchive.CreateArchive())
2023-06-09 19:38:42 -03:00
{
archive.AddEntry("file01.txt", "C:\\file01.txt");
archive.AddEntry("file02.txt", "C:\\file02.txt");
...
archive.SaveTo("C:\\temp.zip", CompressionType.Deflate);
}
```
2016-09-29 10:55:04 +01:00
### Create Zip Archive from all files in a directory to a file
```C#
2026-01-15 13:04:09 +00:00
using (var archive = ZipArchive.CreateArchive())
2016-09-29 10:55:04 +01:00
{
archive.AddAllFromDirectory("D:\\temp");
archive.SaveTo("C:\\temp.zip", CompressionType.Deflate);
}
```
### Create Zip Archive from all files in a directory and save in memory
```C#
var memoryStream = new MemoryStream();
2026-01-15 13:04:09 +00:00
using (var archive = ZipArchive.CreateArchive())
2016-09-29 10:55:04 +01:00
{
archive.AddAllFromDirectory("D:\\temp");
archive.SaveTo(memoryStream, new WriterOptions(CompressionType.Deflate)
{
LeaveStreamOpen = true
});
}
//reset memoryStream to be usable now
memoryStream.Position = 0;
```
2025-03-22 16:50:15 +01:00
### Extract all files from a rar file to a directory using RarArchive
Note: Extracting a solid rar or 7z file needs to be done in sequential order to get acceptable decompression speed.
2025-12-23 15:38:00 +00:00
`ExtractAllEntries` is primarily intended for solid archives (like solid Rar) or 7Zip archives, where sequential extraction provides the best performance. For general/simple extraction with any supported archive type, use `archive.WriteToDirectory()` instead.
2016-09-29 10:55:04 +01:00
```C#
2026-03-02 07:00:33 +00:00
// Use ReaderOptions for open-time behavior and ExtractionOptions for extract-time behavior
2026-03-03 12:40:58 +00:00
using (var archive = RarArchive.OpenArchive("Test.rar", ReaderOptions.ForFilePath))
2016-09-29 10:55:04 +01:00
{
2025-12-23 15:37:28 +00:00
// Simple extraction with RarArchive; this WriteToDirectory pattern works for all archive types
2026-03-02 07:00:33 +00:00
archive.WriteToDirectory(
@"D:\temp",
2026-06-11 17:24:42 +01:00
new ExtractionOptions
{
ExtractFullPath = true,
Overwrite = true,
BufferSize = 131072,
2026-06-15 09:17:05 +01:00
CheckCrc = true, // Default: validate payload checksums when available
2026-06-11 17:24:42 +01:00
}
2026-03-02 07:00:33 +00:00
);
2016-09-29 10:55:04 +01:00
}
```
2025-03-22 16:50:15 +01:00
### Iterate over all files from a Rar file using RarArchive
```C#
2026-01-15 13:29:57 +00:00
using (var archive = RarArchive.OpenArchive("Test.rar"))
2025-03-22 16:50:15 +01:00
{
foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory))
{
Console.WriteLine($"{entry.Key}: {entry.Size} bytes");
}
}
```
2026-01-07 15:30:26 +00:00
### Extract solid Rar or 7Zip archives with progress reporting
2025-12-19 11:49:35 +00:00
2026-06-06 15:50:49 +01:00
For optimal performance with solid Rar and 7Zip archives, extract entries sequentially. `WriteToDirectory` handles that internally for simple extraction. Use `ExtractAllEntries` when you need to manually iterate in sequential order.
2025-12-19 11:49:35 +00:00
```C#
2026-01-07 15:30:26 +00:00
using SharpCompress.Common;
using SharpCompress.Readers;
var progress = new Progress<ProgressReport>(report =>
{
Console.WriteLine($"Extracting {report.EntryPath}: {report.PercentComplete}%");
});
2026-02-10 11:10:04 +00:00
using (var archive = RarArchive.OpenArchive("archive.rar",
2026-03-03 12:40:58 +00:00
ReaderOptions.ForFilePath
2026-06-06 15:50:49 +01:00
.WithProgress(progress)))
2026-02-10 10:48:42 +00:00
{
2026-03-02 07:00:33 +00:00
archive.WriteToDirectory(
@"D:\output",
2026-06-15 09:17:05 +01:00
new ExtractionOptions { ExtractFullPath = true, Overwrite = true, CheckCrc = true }
2026-03-02 07:00:33 +00:00
);
2025-12-19 11:49:35 +00:00
}
```
2026-06-06 15:50:49 +01:00
Manual sequential extraction:
```C#
using (var archive = RarArchive.OpenArchive("archive.rar",
ReaderOptions.ForFilePath.WithProgress(progress)))
using (var reader = archive.ExtractAllEntries())
{
while (reader.MoveToNextEntry())
{
if (!reader.Entry.IsDirectory)
{
reader.WriteEntryToDirectory(@"D:\output");
}
}
}
```
2016-09-29 10:55:04 +01:00
### Use ReaderFactory to autodetect archive type and Open the entry stream
```C#
2016-10-07 19:33:41 +03:30
using (Stream stream = File.OpenRead("Tar.tar.bz2"))
2026-01-15 13:29:57 +00:00
using (var reader = ReaderFactory.OpenReader(stream))
2016-09-29 10:55:04 +01:00
{
while (reader.MoveToNextEntry())
{
if (!reader.Entry.IsDirectory)
{
Console.WriteLine(reader.Entry.Key);
2026-02-10 10:48:42 +00:00
reader.WriteEntryToDirectory(@"C:\temp");
2016-09-29 10:55:04 +01:00
}
}
}
```
### Use ReaderFactory to autodetect archive type and Open the entry stream
```C#
2016-10-07 19:33:41 +03:30
using (Stream stream = File.OpenRead("Tar.tar.bz2"))
2026-01-15 13:29:57 +00:00
using (var reader = ReaderFactory.OpenReader(stream))
2016-09-29 10:55:04 +01:00
{
while (reader.MoveToNextEntry())
{
if (!reader.Entry.IsDirectory)
{
using (var entryStream = reader.OpenEntryStream())
{
entryStream.CopyTo(...);
}
}
}
}
```
### Use WriterFactory to write all files from a directory in a streaming manner.
```C#
using (Stream stream = File.OpenWrite("C:\\temp.tgz"))
2026-02-10 11:10:04 +00:00
using (var writer = WriterFactory.OpenWriter(
stream,
ArchiveType.Tar,
WriterOptions.ForTar(CompressionType.GZip).WithLeaveStreamOpen(true)))
2016-09-29 10:55:04 +01:00
{
writer.WriteAll("D:\\temp", "*", SearchOption.AllDirectories);
}
2016-10-07 19:33:41 +03:30
```
2018-05-23 09:46:36 +09:00
2026-06-06 15:50:49 +01:00
### Use ArchiveFactory to autodetect and open archives
```C#
if (ArchiveFactory.IsArchive("archive.zip", out var archiveType))
{
Console.WriteLine($"Detected {archiveType}");
}
using (var archive = ArchiveFactory.OpenArchive("archive.zip"))
{
archive.WriteToDirectory(@"D:\output");
}
```
### Use ArchiveInformation to choose the right API
```C#
var archivePath = "archive.arc";
var info = ArchiveFactory.GetArchiveInformation(archivePath);
if (info is null)
{
Console.WriteLine("Not a supported archive");
}
else if (info.SupportsRandomAccess)
{
using var archive = ArchiveFactory.OpenArchive(archivePath);
archive.WriteToDirectory(@"D:\output");
}
else
{
using var reader = ReaderFactory.OpenReader(archivePath);
reader.WriteAllToDirectory(@"D:\output");
}
```
`SupportsRandomAccess` is `false` for reader-only formats such as Ace, Arc, Arj, and standalone LZW. Use the Reader API for those formats.
### Open multi-volume archives
```C#
var parts = ArchiveFactory.GetFileParts("archive.part1.rar")
.Select(path => new FileInfo(path))
.ToArray();
using (var archive = ArchiveFactory.OpenArchive(parts))
{
archive.WriteToDirectory(@"D:\output");
}
```
### Use ReaderOptions for self-extracting archives and detection hints
```C#
var sfxOptions = ReaderOptions.ForSelfExtractingArchive("password");
using (var archive = RarArchive.OpenArchive("setup.exe", sfxOptions))
{
archive.WriteToDirectory(@"D:\output");
}
using (Stream stream = File.OpenRead("backup"))
using (var reader = ReaderFactory.OpenReader(
stream,
ReaderOptions.ForExternalStream.WithExtensionHint("tar.gz")))
{
while (reader.MoveToNextEntry())
{
if (!reader.Entry.IsDirectory)
{
reader.WriteEntryToDirectory(@"D:\output");
}
}
}
```
### Write ZIP entries with per-entry options
```C#
using Stream archiveStream = File.Create("output.zip");
using var writer = new ZipWriter(
archiveStream,
new ZipWriterOptions(CompressionType.Deflate)
{
ArchiveComment = "Archive comment",
UseZip64 = true,
});
using Stream source = File.OpenRead("input.txt");
writer.Write("entry.txt", source, new ZipWriterEntryOptions
{
CompressionType = CompressionType.ZStandard,
CompressionLevel = 3,
EntryComment = "Entry comment",
ModificationDateTime = DateTime.UtcNow,
EnableZip64 = true,
});
```
### Write a 7z archive
```C#
using Stream stream = File.Create("output.7z");
using var writer = WriterFactory.OpenWriter(
stream,
ArchiveType.SevenZip,
new SevenZipWriterOptions(CompressionType.LZMA2)
{
CompressHeader = true,
});
using Stream source = File.OpenRead("input.txt");
writer.Write("input.txt", source, DateTime.UtcNow);
```
2018-05-23 09:46:36 +09:00
### Extract zip which has non-utf8 encoded filename(cp932)
```C#
var encoding = Encoding.GetEncoding(932);
2026-02-10 11:10:04 +00:00
var opts = new ReaderOptions()
.WithArchiveEncoding(new ArchiveEncoding
{
CustomDecoder = (data, x, y) => encoding.GetString(data)
});
using var archive = ZipArchive.OpenArchive("test.zip", opts);
foreach(var entry in archive.Entries)
2018-05-23 09:46:36 +09:00
{
Console.WriteLine($"{entry.Key}");
}
2023-06-09 19:38:42 -03:00
```
2025-10-27 09:00:38 +00:00
2026-02-10 15:51:50 +00:00
## Custom Compression Providers
By default `ReaderOptions` and `WriterOptions` already include `CompressionProviderRegistry.Default` via their `Providers` property, so you can read and write without touching the registry yet still get SharpCompress’ s built-in implementations.
2026-02-12 15:09:21 +00:00
The configured registry is used consistently across Reader APIs, Writer APIs, Archive APIs, and async entry-stream extraction, including compressed TAR wrappers and ZIP async decompression.
2026-06-07 11:20:34 +01:00
To replace specific algorithms (for example to use `System.IO.Compression` for GZip or Deflate), create a modified registry and pass it through the same options:
2026-02-10 15:51:50 +00:00
```C#
2026-06-07 11:20:34 +01:00
var customRegistry = CompressionProviderRegistry.Default
.With(new SystemGZipCompressionProvider())
.With(new SystemDeflateCompressionProvider());
2026-02-10 15:51:50 +00:00
2026-03-03 12:40:58 +00:00
var readerOptions = ReaderOptions.ForFilePath
2026-02-10 15:51:50 +00:00
.WithProviders(customRegistry);
using var reader = ReaderFactory.OpenReader(stream, readerOptions);
var writerOptions = new WriterOptions(CompressionType.GZip)
.WithProviders(customRegistry);
using var writer = WriterFactory.OpenWriter(outputStream, ArchiveType.GZip, writerOptions);
```
2026-06-07 11:20:34 +01:00
The registry is immutable. `With(provider)` returns a new registry and replaces any existing provider for that provider's `CompressionType` . You can inspect or use providers directly with `GetProvider` , `CreateCompressStream` , `CreateDecompressStream` , and their async/context overloads, but most application code should flow the registry through `ReaderOptions` or `WriterOptions` so archive readers and writers can supply the right `CompressionContext` .
To implement a custom provider, implement `ICompressionProvider` directly or derive from `CompressionProviderBase` for default async methods. Derive from `DecompressionOnlyProviderBase` for read-only codecs. Providers can use `CompressionContext` for stream size, seekability, reader options, compression properties, and format-specific metadata.
2026-02-10 15:51:50 +00:00
The registry also exposes `GetCompressingProvider` (now returning `ICompressionProviderHooks` ) when a compression format needs pre- or post-stream data (e.g., LZMA/PPMd). Implementations that need extra headers can supply those bytes through the `ICompressionProviderHooks` members while the rest of the API still works through the `Providers` property.
2025-10-27 09:00:38 +00:00
## Async Examples
### Async Reader Examples
**Extract single entry asynchronously:**
```C#
2026-04-10 18:34:51 +02:00
using Stream stream = File.OpenRead("archive.zip");
await using var reader = await ReaderFactory.OpenAsyncReader(stream, cancellationToken: cancellationToken);
while (await reader.MoveToNextEntryAsync(cancellationToken))
2025-10-27 09:00:38 +00:00
{
2026-04-10 18:34:51 +02:00
if (!reader.Entry.IsDirectory)
2025-10-27 09:00:38 +00:00
{
2026-04-10 18:34:51 +02:00
using var outputStream = File.Create("output.bin");
await reader.WriteEntryToAsync(outputStream, cancellationToken);
2025-10-27 09:00:38 +00:00
}
}
```
**Extract all entries asynchronously:**
```C#
2026-04-10 18:34:51 +02:00
using Stream stream = File.OpenRead("archive.tar.gz");
await using var reader = await ReaderFactory.OpenAsyncReader(stream, cancellationToken: cancellationToken);
await reader.WriteAllToDirectoryAsync(
@"D:\temp",
cancellationToken: cancellationToken
);
2025-10-27 09:00:38 +00:00
```
**Open and process entry stream asynchronously:**
```C#
2026-04-10 17:47:33 +01:00
await using var archive = await ZipArchive.OpenAsyncArchive("archive.zip", cancellationToken: cancellationToken);
await foreach (var entry in archive.EntriesAsync)
2025-10-27 09:00:38 +00:00
{
2026-04-10 17:47:33 +01:00
if (!entry.IsDirectory)
{
using var entryStream = await entry.OpenEntryStreamAsync(cancellationToken);
// Process the decompressed stream asynchronously
await ProcessStreamAsync(entryStream, cancellationToken);
}
2025-10-27 09:00:38 +00:00
}
```
### Async Writer Examples
**Write single file asynchronously:**
```C#
2026-04-10 18:34:51 +02:00
using Stream archiveStream = File.OpenWrite("output.zip");
await using var writer = await WriterFactory.OpenAsyncWriter(archiveStream, ArchiveType.Zip, new WriterOptions(CompressionType.Deflate), cancellationToken);
using Stream fileStream = File.OpenRead("input.txt");
await writer.WriteAsync("entry.txt", fileStream, DateTime.Now, cancellationToken);
2025-10-27 09:00:38 +00:00
```
**Write entire directory asynchronously:**
```C#
2026-04-10 18:34:51 +02:00
using Stream stream = File.OpenWrite("backup.tar.gz");
await using var writer = await WriterFactory.OpenAsyncWriter(stream, ArchiveType.Tar, new WriterOptions(CompressionType.GZip), cancellationToken);
await writer.WriteAllAsync(
@"D:\files",
"*",
SearchOption.AllDirectories,
cancellationToken
);
2025-10-27 09:00:38 +00:00
```
**Write with progress tracking and cancellation:**
```C#
var cts = new CancellationTokenSource();
// Set timeout or cancel from UI
cts.CancelAfter(TimeSpan.FromMinutes(5));
2026-04-10 18:34:51 +02:00
using Stream stream = File.OpenWrite("archive.zip");
await using var writer = await WriterFactory.OpenAsyncWriter(stream, ArchiveType.Zip, new WriterOptions(CompressionType.Deflate), cts.Token);
try
2025-10-27 09:00:38 +00:00
{
2026-04-10 18:34:51 +02:00
await writer.WriteAllAsync(@"D:\data", "*", SearchOption.AllDirectories, cts.Token);
}
catch (OperationCanceledException)
{
Console.WriteLine("Operation was cancelled");
2025-10-27 09:00:38 +00:00
}
```
### Archive Async Examples
**Extract from archive asynchronously:**
```C#
2026-04-10 18:34:51 +02:00
await using var archive = await ZipArchive.OpenAsyncArchive("archive.zip", cancellationToken: cancellationToken);
// Simple async extraction - works for all archive types
await archive.WriteToDirectoryAsync(
@"C:\output",
cancellationToken: cancellationToken
);
2025-10-27 09:00:38 +00:00
```
**Benefits of Async Operations:**
- Non-blocking I/O for better application responsiveness
- Improved scalability for server applications
- Support for cancellation via CancellationToken
- Better resource utilization in async/await contexts
- Compatible with modern .NET async patterns