Skip to content

Commit e1df655

Browse files
authored
Merge pull request #1193 from adamhathcock/adam/cleanup-options
Change and clean up options
2 parents 756cb7b + 34f4314 commit e1df655

128 files changed

Lines changed: 2168 additions & 1057 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/API.md

Lines changed: 84 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,18 @@ using (var archive = RarArchive.OpenArchive("file.rar"))
2020
using (var archive = SevenZipArchive.OpenArchive("file.7z"))
2121
using (var archive = GZipArchive.OpenArchive("file.gz"))
2222

23-
// With options
24-
var options = new ReaderOptions
23+
// With fluent options (preferred)
24+
var options = ReaderOptions.ForEncryptedArchive("password")
25+
.WithArchiveEncoding(new ArchiveEncoding { Default = Encoding.GetEncoding(932) });
26+
using (var archive = ZipArchive.OpenArchive("encrypted.zip", options))
27+
28+
// Alternative: object initializer
29+
var options2 = new ReaderOptions
2530
{
2631
Password = "password",
2732
LeaveStreamOpen = true,
2833
ArchiveEncoding = new ArchiveEncoding { Default = Encoding.GetEncoding(932) }
2934
};
30-
using (var archive = ZipArchive.OpenArchive("encrypted.zip", options))
3135
```
3236

3337
### Creating Archives
@@ -44,16 +48,21 @@ using (var archive = ZipArchive.CreateArchive())
4448
using (var archive = TarArchive.CreateArchive())
4549
using (var archive = GZipArchive.CreateArchive())
4650

47-
// With options
48-
var options = new WriterOptions(CompressionType.Deflate)
49-
{
50-
CompressionLevel = 9,
51-
LeaveStreamOpen = false
52-
};
51+
// With fluent options (preferred)
52+
var options = WriterOptions.ForZip()
53+
.WithCompressionLevel(9)
54+
.WithLeaveStreamOpen(false);
5355
using (var archive = ZipArchive.CreateArchive())
5456
{
5557
archive.SaveTo("output.zip", options);
5658
}
59+
60+
// Alternative: constructor with object initializer
61+
var options2 = new WriterOptions(CompressionType.Deflate)
62+
{
63+
CompressionLevel = 9,
64+
LeaveStreamOpen = false
65+
};
5766
```
5867

5968
---
@@ -72,16 +81,11 @@ using (var archive = ZipArchive.OpenArchive("file.zip"))
7281
var entry = archive.Entries.FirstOrDefault(e => e.Key == "file.txt");
7382

7483
// Extract all
75-
archive.WriteToDirectory(@"C:\output", new ExtractionOptions
76-
{
77-
ExtractFullPath = true,
78-
Overwrite = true
79-
});
84+
archive.WriteToDirectory(@"C:\output");
8085

8186
// Extract single entry
8287
var entry = archive.Entries.First();
8388
entry.WriteToFile(@"C:\output\file.txt");
84-
entry.WriteToFile(@"C:\output\file.txt", new ExtractionOptions { Overwrite = true });
8589

8690
// Get entry stream
8791
using (var stream = entry.OpenEntryStream())
@@ -95,7 +99,6 @@ using (var asyncArchive = await ZipArchive.OpenAsyncArchive("file.zip"))
9599
{
96100
await asyncArchive.WriteToDirectoryAsync(
97101
@"C:\output",
98-
new ExtractionOptions { ExtractFullPath = true, Overwrite = true },
99102
cancellationToken: cancellationToken
100103
);
101104
}
@@ -187,7 +190,6 @@ using (var reader = await ReaderFactory.OpenAsyncReader(stream))
187190
// Async extraction of all entries
188191
await reader.WriteAllToDirectoryAsync(
189192
@"C:\output",
190-
new ExtractionOptions { ExtractFullPath = true, Overwrite = true },
191193
cancellationToken
192194
);
193195
}
@@ -229,43 +231,91 @@ using (var writer = WriterFactory.OpenWriter(stream, ArchiveType.Zip, Compressio
229231

230232
### ReaderOptions
231233

234+
Use factory presets and fluent helpers for common configurations:
235+
232236
```csharp
233-
var options = new ReaderOptions
234-
{
235-
Password = "password", // For encrypted archives
236-
LeaveStreamOpen = true, // Don't close wrapped stream
237-
ArchiveEncoding = new ArchiveEncoding // Custom character encoding
238-
{
239-
Default = Encoding.GetEncoding(932)
240-
}
241-
};
237+
// External stream with password and custom encoding
238+
var options = ReaderOptions.ForExternalStream()
239+
.WithPassword("password")
240+
.WithArchiveEncoding(new ArchiveEncoding { Default = Encoding.GetEncoding(932) });
241+
242242
using (var archive = ZipArchive.OpenArchive("file.zip", options))
243243
{
244244
// ...
245245
}
246+
247+
// Common presets
248+
var safeOptions = ReaderOptions.SafeExtract; // No overwrite
249+
var flatOptions = ReaderOptions.FlatExtract; // No directory structure
250+
251+
// Factory defaults:
252+
// - file path / FileInfo overloads use LeaveStreamOpen = false
253+
// - stream overloads use LeaveStreamOpen = true
254+
```
255+
256+
Alternative: traditional object initializer:
257+
258+
```csharp
259+
var options = new ReaderOptions
260+
{
261+
Password = "password",
262+
LeaveStreamOpen = true,
263+
ArchiveEncoding = new ArchiveEncoding { Default = Encoding.GetEncoding(932) }
264+
};
246265
```
247266

248267
### WriterOptions
249268

269+
Factory methods provide a clean, discoverable way to create writer options:
270+
271+
```csharp
272+
// Factory methods for common archive types
273+
var zipOptions = WriterOptions.ForZip() // ZIP with Deflate
274+
.WithCompressionLevel(9) // 0-9 for Deflate
275+
.WithLeaveStreamOpen(false); // Close stream when done
276+
277+
var tarOptions = WriterOptions.ForTar(CompressionType.GZip) // TAR with GZip
278+
.WithLeaveStreamOpen(false);
279+
280+
var gzipOptions = WriterOptions.ForGZip() // GZip file
281+
.WithCompressionLevel(6);
282+
283+
archive.SaveTo("output.zip", zipOptions);
284+
```
285+
286+
Alternative: traditional constructor with object initializer:
287+
250288
```csharp
251289
var options = new WriterOptions(CompressionType.Deflate)
252290
{
253-
CompressionLevel = 9, // 0-9 for Deflate
254-
LeaveStreamOpen = true, // Don't close stream
291+
CompressionLevel = 9,
292+
LeaveStreamOpen = true,
255293
};
256294
archive.SaveTo("output.zip", options);
257295
```
258296

259-
### ExtractionOptions
297+
### Extraction behavior
260298

261299
```csharp
262-
var options = new ExtractionOptions
300+
var options = new ReaderOptions
263301
{
264302
ExtractFullPath = true, // Recreate directory structure
265303
Overwrite = true, // Overwrite existing files
266304
PreserveFileTime = true // Keep original timestamps
267305
};
268-
archive.WriteToDirectory(@"C:\output", options);
306+
307+
using (var archive = ZipArchive.OpenArchive("file.zip", options))
308+
{
309+
archive.WriteToDirectory(@"C:\output");
310+
}
311+
```
312+
313+
### Options matrix
314+
315+
```text
316+
ReaderOptions: open-time behavior (password, encoding, stream ownership, extraction defaults)
317+
WriterOptions: write-time behavior (compression type/level, encoding, stream ownership)
318+
ZipWriterEntryOptions: per-entry ZIP overrides (compression, level, timestamps, comments, zip64)
269319
```
270320

271321
---
@@ -317,13 +367,9 @@ ArchiveType.ZStandard
317367
try
318368
{
319369
using (var archive = ZipArchive.Open("archive.zip",
320-
new ReaderOptions { Password = "password" }))
370+
ReaderOptions.ForEncryptedArchive("password")))
321371
{
322-
archive.WriteToDirectory(@"C:\output", new ExtractionOptions
323-
{
324-
ExtractFullPath = true,
325-
Overwrite = true
326-
});
372+
archive.WriteToDirectory(@"C:\output");
327373
}
328374
}
329375
catch (PasswordRequiredException)
@@ -348,7 +394,7 @@ var progress = new Progress<ProgressReport>(report =>
348394
Console.WriteLine($"Extracting {report.EntryPath}: {report.PercentComplete}%");
349395
});
350396

351-
var options = new ReaderOptions { Progress = progress };
397+
var options = ReaderOptions.ForOwnedFile().WithProgress(progress);
352398
using (var archive = ZipArchive.OpenArchive("archive.zip", options))
353399
{
354400
archive.WriteToDirectory(@"C:\output");
@@ -367,7 +413,6 @@ try
367413
{
368414
await archive.WriteToDirectoryAsync(
369415
@"C:\output",
370-
new ExtractionOptions { ExtractFullPath = true, Overwrite = true },
371416
cancellationToken: cts.Token
372417
);
373418
}

docs/ARCHITECTURE.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ Common types, options, and enumerations used across formats.
9090
- `ArchiveType.cs` - Enum for archive formats
9191
- `CompressionType.cs` - Enum for compression methods
9292
- `ArchiveEncoding.cs` - Character encoding configuration
93-
- `ExtractionOptions.cs` - Extraction configuration
93+
- `IExtractionOptions.cs` - Extraction configuration exposed through `ReaderOptions`
9494
- Format-specific headers: `Zip/Headers/`, `Tar/Headers/`, `Rar/Headers/`, etc.
9595

9696
#### `Compressors/` - Compression Algorithms
@@ -215,13 +215,13 @@ using (var compressor = new DeflateStream(nonDisposingStream))
215215
public abstract class AbstractArchive : IArchive
216216
{
217217
// Template methods
218-
public virtual void WriteToDirectory(string destinationDirectory, ExtractionOptions options)
218+
public virtual void WriteToDirectory(string destinationDirectory)
219219
{
220220
// Common extraction logic
221221
foreach (var entry in Entries)
222222
{
223223
// Call subclass method
224-
entry.WriteToFile(destinationPath, options);
224+
entry.WriteToFile(destinationPath);
225225
}
226226
}
227227

@@ -267,8 +267,7 @@ public interface IArchive : IDisposable
267267
{
268268
IEnumerable<IEntry> Entries { get; }
269269

270-
void WriteToDirectory(string destinationDirectory,
271-
ExtractionOptions options = null);
270+
void WriteToDirectory(string destinationDirectory);
272271

273272
IEntry FirstOrDefault(Func<IEntry, bool> predicate);
274273

@@ -287,8 +286,7 @@ public interface IReader : IDisposable
287286

288287
bool MoveToNextEntry();
289288

290-
void WriteEntryToDirectory(string destinationDirectory,
291-
ExtractionOptions options = null);
289+
void WriteEntryToDirectory(string destinationDirectory);
292290

293291
Stream OpenEntryStream();
294292

@@ -327,7 +325,7 @@ public interface IEntry
327325
DateTime? LastModifiedTime { get; }
328326
CompressionType CompressionType { get; }
329327

330-
void WriteToFile(string fullPath, ExtractionOptions options = null);
328+
void WriteToFile(string fullPath);
331329
void WriteToStream(Stream destinationStream);
332330
Stream OpenEntryStream();
333331

docs/ENCODING.md

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,9 @@ Most archive formats store filenames and metadata as bytes. SharpCompress must c
1818
using SharpCompress.Common;
1919
using SharpCompress.Readers;
2020

21-
// Configure encoding before opening archive
22-
var options = new ReaderOptions
23-
{
24-
ArchiveEncoding = new ArchiveEncoding
25-
{
26-
Default = Encoding.GetEncoding(932) // cp932 for Japanese
27-
}
28-
};
21+
// Configure encoding using fluent factory method (preferred)
22+
var options = ReaderOptions.ForEncoding(
23+
new ArchiveEncoding { Default = Encoding.GetEncoding(932) }); // cp932 for Japanese
2924
3025
using (var archive = ZipArchive.OpenArchive("japanese.zip", options))
3126
{
@@ -34,6 +29,12 @@ using (var archive = ZipArchive.OpenArchive("japanese.zip", options))
3429
Console.WriteLine(entry.Key); // Now shows correct characters
3530
}
3631
}
32+
33+
// Alternative: object initializer
34+
var options2 = new ReaderOptions
35+
{
36+
ArchiveEncoding = new ArchiveEncoding { Default = Encoding.GetEncoding(932) }
37+
};
3738
```
3839

3940
### ArchiveEncoding Properties
@@ -47,10 +48,8 @@ using (var archive = ZipArchive.OpenArchive("japanese.zip", options))
4748

4849
**Archive API:**
4950
```csharp
50-
var options = new ReaderOptions
51-
{
52-
ArchiveEncoding = new ArchiveEncoding { Default = Encoding.GetEncoding(932) }
53-
};
51+
var options = ReaderOptions.ForEncoding(
52+
new ArchiveEncoding { Default = Encoding.GetEncoding(932) });
5453
using (var archive = ZipArchive.OpenArchive("file.zip", options))
5554
{
5655
// Use archive with correct encoding
@@ -59,10 +58,8 @@ using (var archive = ZipArchive.OpenArchive("file.zip", options))
5958

6059
**Reader API:**
6160
```csharp
62-
var options = new ReaderOptions
63-
{
64-
ArchiveEncoding = new ArchiveEncoding { Default = Encoding.GetEncoding(932) }
65-
};
61+
var options = ReaderOptions.ForEncoding(
62+
new ArchiveEncoding { Default = Encoding.GetEncoding(932) });
6663
using (var stream = File.OpenRead("file.zip"))
6764
using (var reader = ReaderFactory.OpenReader(stream, options))
6865
{
@@ -390,11 +387,7 @@ var options = new ReaderOptions
390387

391388
using (var archive = ZipArchive.OpenArchive("japanese_files.zip", options))
392389
{
393-
archive.WriteToDirectory(@"C:\output", new ExtractionOptions
394-
{
395-
ExtractFullPath = true,
396-
Overwrite = true
397-
});
390+
archive.WriteToDirectory(@"C:\output");
398391
}
399392
// Files extracted with correct Japanese names
400393
```

docs/PERFORMANCE.md

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,7 @@ using (var archive = RarArchive.OpenArchive("solid.rar"))
213213
using (var archive = RarArchive.OpenArchive("solid.rar"))
214214
{
215215
// Method 1: Use WriteToDirectory (recommended)
216-
archive.WriteToDirectory(@"C:\output", new ExtractionOptions
217-
{
218-
ExtractFullPath = true,
219-
Overwrite = true
220-
});
216+
archive.WriteToDirectory(@"C:\output");
221217

222218
// Method 2: Use ExtractAllEntries
223219
archive.ExtractAllEntries();
@@ -337,7 +333,6 @@ using (var archive = ZipArchive.OpenArchive("archive.zip"))
337333
{
338334
await archive.WriteToDirectoryAsync(
339335
@"C:\output",
340-
new ExtractionOptions { ExtractFullPath = true, Overwrite = true },
341336
cancellationToken
342337
);
343338
}
@@ -355,10 +350,7 @@ Async doesn't improve performance for:
355350
// Sync extraction (simpler, same performance on fast I/O)
356351
using (var archive = ZipArchive.OpenArchive("archive.zip"))
357352
{
358-
archive.WriteToDirectory(
359-
@"C:\output",
360-
new ExtractionOptions { ExtractFullPath = true, Overwrite = true }
361-
);
353+
archive.WriteToDirectory(@"C:\output");
362354
}
363355
// Simple and fast - no async needed
364356
```
@@ -377,7 +369,6 @@ try
377369
{
378370
await archive.WriteToDirectoryAsync(
379371
@"C:\output",
380-
new ExtractionOptions { ExtractFullPath = true, Overwrite = true },
381372
cts.Token
382373
);
383374
}

0 commit comments

Comments
 (0)