Search for entries in zip archive #482

Closed
opened 2026-01-29 22:12:46 +00:00 by claunia · 2 comments
Owner

Originally created by @neumatho on GitHub (Sep 26, 2021).

If you open an entry in a zip archive and while you have it opened, search for another entry, the first entry is not complete. Here is an example:

using System;
using System.IO;
using System.Linq;
using SharpCompress.Archives.Zip;

namespace Archive
{
	class Program
	{
		static void Main(string[] args)
		{
			using (FileStream fs = new FileStream(@"P:\\m\\rw\\sfx2.angel_harp.zip", FileMode.Open))
			{
				ZipArchive archive = ZipArchive.Open(fs);

				ZipArchiveEntry entry = archive.Entries.FirstOrDefault(e => e.Key == "SOAMC/SOAMC_AMIGA/SoundFX_2.0/Roman_Werner/sfx2.angel_harp");
				using (Stream s = entry.OpenEntryStream())
				{
					ZipArchiveEntry entry1 = archive.Entries.FirstOrDefault(e => e.Key == "SOAMC/SOAMC_AMIGA/SoundFX_2.0/Roman_Werner/sfx2.angel_harp.txt");

					using (MemoryStream ms = new MemoryStream())
					{
						s.CopyTo(ms);
						long len = ms.Length;
					}
				}
			}
		}
	}
}

It this case, the len variable is zero, which means nothing has been copied to the MemoryStream. If you uncomment the entry1 search (which do not find anything), then it works fine. Also if I change the entry1 search to search for the same entry (or some other entry that does exists), it also work. It seems to fail, if the search did not find anything.

I have attached the test archive, but I guess you can use any archive.

sfx2.angel_harp.zip

Originally created by @neumatho on GitHub (Sep 26, 2021). If you open an entry in a zip archive and while you have it opened, search for another entry, the first entry is not complete. Here is an example: ``` using System; using System.IO; using System.Linq; using SharpCompress.Archives.Zip; namespace Archive { class Program { static void Main(string[] args) { using (FileStream fs = new FileStream(@"P:\\m\\rw\\sfx2.angel_harp.zip", FileMode.Open)) { ZipArchive archive = ZipArchive.Open(fs); ZipArchiveEntry entry = archive.Entries.FirstOrDefault(e => e.Key == "SOAMC/SOAMC_AMIGA/SoundFX_2.0/Roman_Werner/sfx2.angel_harp"); using (Stream s = entry.OpenEntryStream()) { ZipArchiveEntry entry1 = archive.Entries.FirstOrDefault(e => e.Key == "SOAMC/SOAMC_AMIGA/SoundFX_2.0/Roman_Werner/sfx2.angel_harp.txt"); using (MemoryStream ms = new MemoryStream()) { s.CopyTo(ms); long len = ms.Length; } } } } } } ``` It this case, the len variable is zero, which means nothing has been copied to the MemoryStream. If you uncomment the entry1 search (which do not find anything), then it works fine. Also if I change the entry1 search to search for the same entry (or some other entry that does exists), it also work. It seems to fail, if the search did not find anything. I have attached the test archive, but I guess you can use any archive. [sfx2.angel_harp.zip](https://github.com/adamhathcock/sharpcompress/files/7230918/sfx2.angel_harp.zip)
claunia added the question label 2026-01-29 22:12:46 +00:00
Author
Owner

@adamhathcock commented on GitHub (Sep 27, 2021):

Don't do that then.

Opening an entry stream is manipulating the stream's location to read the correct bytes. The Entries collection is loaded lazily behind the scenes which is also manipulating the stream's location. Doing both at the same time is bad.

Generally don't do anything else with the archive while using OpenEntryStream until you close it.

@adamhathcock commented on GitHub (Sep 27, 2021): Don't do that then. Opening an entry stream is manipulating the stream's location to read the correct bytes. The Entries collection is loaded lazily behind the scenes which is also manipulating the stream's location. Doing both at the same time is bad. Generally don't do anything else with the archive while using OpenEntryStream until you close it.
Author
Owner

@neumatho commented on GitHub (Sep 27, 2021):

Ok, I will then update my code.

@neumatho commented on GitHub (Sep 27, 2021): Ok, I will then update my code.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#482