0

I've created a method that reads .jpg files and displays them on my screen without extracting.

The var looks like this

var imageName = content.contentid + ".jpg";

content.contentid is too id number of the number + .jpg

But now I want that if there is, for example, a png or jfif file in the zip that it also just shows it. How do I handle this in this method?

This is my code so far

    private void getImage()
    {
        try
        {
            var folderName = "protocol-" + _protocol.id + "-" + _protocol.versionnr + ".zip";
            var extractPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
            var zipPath = $"{extractPath}" + "/" + $"{folderName}";
            var currentIndex = getCurrentIndex();
            var content = _protocol.contents[currentIndex];
            List<string> allowedExtensions = new List<string>() { "jpg", "png", "jfif" };

            using (var archive = ZipFile.OpenRead(zipPath))
            {
                foreach (var pictureEntry in archive.Entries)
                    if (Path.GetFileNameWithoutExtension(pictureEntry.Name).Equals(content.contentid) && allowedExtensions.Contains(Path.GetExtension(pictureEntry.Name)))
                    {
                        byte[] buffer;
                        var length = pictureEntry.Length;
                        buffer = new byte[length];
                        pictureEntry.Open().Read(buffer, 0, (int)length);
                        myImage.Source = ImageSource.FromStream(() => new MemoryStream(buffer));
                    }
            }
        }
        catch (Exception)
        {
        }
    }
Teun
  • 161
  • 1
  • 1
  • 8
  • Create an array of image extensions string[] imageTypes = {"jpg", "png"}; The check each type in the array. – jdweng Feb 03 '22 at 11:25
  • and how should my var imageName be then? – Teun Feb 03 '22 at 11:28
  • I'm fairly sure I saw this yesterday (now-deleted) – canton7 Feb 03 '22 at 11:34
  • There are a few ways and didn't want to give one solution. You either need another loop or use Contains() method. I didn't like recommending contains incase the jpg was in middle of the filename. You could add periods in the mageTypes like ".jpg" and then use ENDWith. – jdweng Feb 03 '22 at 11:35

1 Answers1

0

Well you can try this approach:

First define a List of extensions that you want to check against:

List<string> allowedExtensions = new List<string>() {"jpg", "png", "jfif" };

then change your if (pictureEntry.Name == imageName) for

if (Path.GetFileNameWithoutExtension(pictureEntry.Name) == content.contentid && 
      allowedExtensions.Contains(Path.GetExtension(pictureEntry.Name)))

Also this line var imageName = content.contentid + ".jpg"; is innecesary as imageName didn't be used:

J.Salas
  • 1,268
  • 1
  • 8
  • 15
  • i updated my question code based on your'e answer if i debug it it wil show 1.jpgat pictureEntry.name but the only problem is the image.source don't show any image @J.Salas – Teun Feb 03 '22 at 11:59
  • And before the change, you get a image? it's strange because we only changed the if and 1.jpg is a value that with the previous code would get into the ImageSource too And that piece of code didn't changed – J.Salas Feb 03 '22 at 12:10
  • yes before i got the image – Teun Feb 03 '22 at 12:18