Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ using (var archive = ZipArchive.OpenArchive("file.zip"))
archive.WriteToDirectory(@"C:\output");

// Extract single entry
var entry = archive.Entries.First();
entry.WriteToFile(@"C:\output\file.txt");
var firstEntry = archive.Entries.First();
firstEntry.WriteToFile(@"C:\output\file.txt");

// Get entry stream
using (var stream = entry.OpenEntryStream())
Comment on lines +87 to 91
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this example block, entry is obtained via FirstOrDefault(...) and can be null, but it’s used unconditionally in entry.OpenEntryStream(). Also, after renaming the later var entry to firstEntry, the “Get entry stream” section now refers back to the potentially-null entry, which makes the sample prone to runtime failure. Consider either (a) changing the lookup to First(...) (or otherwise guaranteeing a non-null entry) or (b) adding a null-check before opening the stream; and ensure the stream example uses the intended variable (firstEntry vs the key-matched entry).

Copilot uses AI. Check for mistakes.
Expand All @@ -97,14 +97,23 @@ using (var archive = ZipArchive.OpenArchive("file.zip"))
// Async extraction (requires IAsyncArchive)
await using (var asyncArchive = await ZipArchive.OpenAsyncArchive("file.zip"))
{
// Extract all entries asynchronously
await asyncArchive.WriteToDirectoryAsync(
@"C:\output",
cancellationToken: cancellationToken
);
}
using (var stream = await entry.OpenEntryStreamAsync(cancellationToken))

// Open a specific entry stream asynchronously
await using (var asyncArchive = await ZipArchive.OpenAsyncArchive("file.zip"))
{
// ...
await foreach (var entry in asyncArchive.EntriesAsync)
{
using (var stream = await entry.OpenEntryStreamAsync(cancellationToken))
{
// ...
}
}
}
```

Expand Down Expand Up @@ -214,15 +223,18 @@ using (var writer = WriterFactory.OpenWriter(stream, ArchiveType.Zip, Compressio
// Write directory
writer.WriteAll("C:\\source", "*", SearchOption.AllDirectories);
writer.WriteAll("C:\\source", "*.txt", SearchOption.TopDirectoryOnly);

// Async variants
using (var fileStream = File.OpenRead("source.txt"))
{
await writer.WriteAsync("entry.txt", fileStream, DateTime.Now, cancellationToken);
}

await writer.WriteAllAsync("C:\\source", "*", SearchOption.AllDirectories, cancellationToken);
}

// Async variants: use OpenAsyncWriter to get IAsyncWriter
await using var stream = File.Create("output.zip");
await using var writer = await WriterFactory.OpenAsyncWriter(stream, ArchiveType.Zip, new WriterOptions(CompressionType.Deflate), cancellationToken);

using (var fileStream = File.OpenRead("source.txt"))
{
await writer.WriteAsync("entry.txt", fileStream, DateTime.Now, cancellationToken);
}

await writer.WriteAllAsync("C:\\source", "*", SearchOption.AllDirectories, cancellationToken);
```

---
Expand Down Expand Up @@ -390,7 +402,7 @@ ArchiveType.ZStandard
```csharp
try
{
using (var archive = ZipArchive.Open("archive.zip",
using (var archive = ZipArchive.OpenArchive("archive.zip",
ReaderOptions.ForEncryptedArchive("password")))
{
archive.WriteToDirectory(@"C:\output");
Expand Down
14 changes: 8 additions & 6 deletions docs/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ while (await reader.MoveToNextEntryAsync(cancellationToken))
{
if (!reader.Entry.IsDirectory)
{
using var entryStream = await reader.OpenEntryStreamAsync(cancellationToken);
using var outputStream = File.Create("output.bin");
await reader.WriteEntryToAsync(outputStream, cancellationToken);
}
Expand All @@ -261,12 +260,15 @@ await reader.WriteAllToDirectoryAsync(

**Open and process entry stream asynchronously:**
```C#
using var archive = ZipArchive.OpenArchive("archive.zip");
foreach (var entry in archive.Entries.Where(e => !e.IsDirectory))
await using var archive = await ZipArchive.OpenAsyncArchive("archive.zip", cancellationToken: cancellationToken);
await foreach (var entry in archive.EntriesAsync)
{
using var entryStream = await entry.OpenEntryStreamAsync(cancellationToken);
// Process the decompressed stream asynchronously
await ProcessStreamAsync(entryStream, cancellationToken);
if (!entry.IsDirectory)
{
using var entryStream = await entry.OpenEntryStreamAsync(cancellationToken);
// Process the decompressed stream asynchronously
await ProcessStreamAsync(entryStream, cancellationToken);
}
}
```

Expand Down
Loading