Copy keep.txt to a new ZIP without decompressing or recompressing it.
The snippet needs an existing source.zip with keep.txt first; the download includes one, and the full program creates one.
dotnet add package RecursiveAI.OneArchiveRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneArchive.Zip; using var input = File.OpenRead("source.zip"); using var zip = new ZipReader(input); var entry = zip.GetNextEntry() ?? throw new InvalidDataException(); using var raw = zip.OpenRawEntryStream(entry); using var file = File.Create("subset.zip"); using (var writer = new ZipWriter(file)) writer.WriteRawEntry( ZipRawEntry.FromEntry(entry), raw); Console.WriteLine(entry.Name);
Unzip the example, then dotnet run.
Writes subset.zip and prints the name it copied:
keep.txtThe full program
The Program.cs in the download creates the source ZIP itself. keep.txt contains two keep me lines; skip.txt contains one skip me line. Each line ends in \n. Both entries use the writer’s default Deflate method. The program finds keep.txt by name, writes it to a new archive, then reopens that archive to compare the compressed payloads.
using RecursiveAI.OneArchive; using RecursiveAI.OneArchive.Zip; using (var source = new ZipWriter(File.Create("source.zip"))) { Add(source, "keep.txt", "keep me\nkeep me\n"u8.ToArray()); Add(source, "skip.txt", "skip me\n"u8.ToArray()); } using var reader = new ZipReader(File.OpenRead("source.zip")); ZipEntry? entry; do { entry = reader.GetNextEntry(); } while (entry is not null && entry.Name != "keep.txt"); if (entry is null) throw new InvalidDataException("Missing keep.txt"); using (var writer = new ZipWriter(File.Create("subset.zip"))) using (var raw = reader.OpenRawEntryStream(entry)) writer.WriteRawEntry(ZipRawEntry.FromEntry(entry), raw); using var copy = new ZipReader(File.OpenRead("subset.zip")); var copied = copy.GetNextEntry(); if (copied?.Name != entry.Name) throw new InvalidDataException("Wrong entry in subset.zip"); using (var originalRaw = reader.OpenRawEntryStream(entry)) using (var copiedRaw = copy.OpenRawEntryStream(copied)) { if (!ReadBytes(originalRaw).AsSpan().SequenceEqual(ReadBytes(copiedRaw))) throw new InvalidDataException("The compressed bytes changed"); } if (copy.GetNextEntry() is not null) throw new InvalidDataException("Unexpected second entry"); Console.WriteLine("subset.zip: keep.txt only"); Console.WriteLine("Compressed bytes match"); static void Add(ZipWriter writer, string name, byte[] bytes) => writer.WriteEntry(new ArchiveEntry { Name = name, Size = bytes.Length }, bytes); static byte[] ReadBytes(Stream stream) { using var buffer = new MemoryStream(); stream.CopyTo(buffer); return buffer.ToArray(); }
subset.zip: keep.txt only Compressed bytes match
If a forward-only input has a data descriptor (sizes and CRC follow the entry data), opening that entry raw throws ArchiveUnsupportedException; reopen from a seekable stream like File.OpenRead.
Variations
To copy the other entry, select skip.txt instead. Replace the snippet’s GetNextEntry() assignment with this selection; the rest of the snippet stays the same:
ZipEntry? entry; do { entry = zip.GetNextEntry(); } while (entry is not null && entry.Name != "skip.txt"); if (entry is null) throw new InvalidDataException("Missing skip.txt");
skip.txtRelated recipes
The short answer
Use OpenRawEntryStream on the source ZipReader, then give that stream and ZipRawEntry.FromEntry(entry) to WriteRawEntry on a separate ZipWriter. You can leave out entries you don’t want without extracting any files to disk. Open the source ZIP for reading and use a different path for the output: File.Create truncates a file if it already exists.
How do you copy files between ZIP archives without uncompressing them?
Copy the bytes that ZIP stored for the entry rather than opening the entry as decoded content. OpenRawEntryStream reads those bytes; FromEntry supplies the metadata the destination needs to describe them, including the compression method, CRC-32 and sizes. Use the two together: raw bytes alone don’t tell the writer how to describe the original data. For a Deflate entry, the output entry still uses Deflate. For an entry stored without compression, there’s no compression step on either side.
The compressed payload is copied unchanged, but the ZIP file as a whole isn’t. The writer builds a new directory and writes new offsets for the entries you keep. Disposing the writer finishes the archive before you reopen it. If you need a byte-for-byte duplicate of the whole ZIP, copy the file instead. If you need to change an entry’s contents, write that entry normally and reserve the raw copy path for the entries you didn’t edit.
Can you read a ZIP file without unzipping it?
You can list the entry names without extracting them, or pass the stored data to a writer as this recipe does. Neither gives you the decoded contents of a compressed entry. Raw copy does not check the payload against its CRC-32, so a successful copy doesn’t tell you the source entry can be extracted intact. Writer options such as the compression level don’t re-encode a raw entry. See raw entry copy in the ZIP docs for which options still apply.
For selective copies, start with a seekable ZIP: the reader can return to an entry it has already listed. A forward-only reader can open only its current entry, once, and only when the local header already carries its sizes and CRC-32. If your source comes from a non-seekable stream and uses data descriptors, save it to a seekable file first.