ZipArchive incorrect path recognition

0 Answers 20 Views
General Discussions
Kostiantyn
Top achievements
Rank 1
Iron
Kostiantyn asked on 06 Feb 2024, 09:19 AM

In my code I have a implemented method which in some cases can append new text for existing archive entry. But when I tried this code it gives me a System.ArgumentException: 'An item with the same key has already been added. Key: dir/file.csv'. This exception occurs in situations when given entryName will be something like this 'dir//file.csv''.  This small difference does not allow archive.GetEntry(entryName) return something else then null.

public void Insert(Stream stream, string entryName, bool append = false)
{
    using var baseStream = FileSystem.FileStream.New(ArchivePath, FileMode.Open, FileAccess.ReadWrite);
    using var archive = new ZipArchive(baseStream, ZipArchiveMode.Update, false, null, compressionSettings, encryptionSettings);

    var entry = archive.GetEntry(entryName); // null
    var entryStream = append
        ? entry?.Open() ?? archive.CreateEntry(entryName).Open() //exception cause already exists
        : archive.CreateEntry(entryName).Open(); 
    stream.Position = 0;
    if (append) entryStream.Seek(0, SeekOrigin.End);
    stream.CopyTo(entryStream);
    entryStream.Flush();
}

 

 

Yoan
Telerik team
commented on 09 Feb 2024, 07:54 AM

Hello Kostiantyn,

In order for me to get a better context and understanding of this case, would you be able to share with us a sample, runnable project that reproduces this behavior? That way I can try to achieve the same scenario you are experiencing, examine the resources, and get back to you with details and specifics.

Please keep in mind that this is a public Q&A Forum thread so if you are going to go through with the request make sure you omit any sensitive data from the project that you would like to keep private and replace it with dummy data if it is still possible to reproduce the issue after that. I can assure you that anything shared by our clients is treated with strict confidentiality and is used for testing purposes only (as per our EULA section 11).

Thank you in advance for your cooperation.

Regards,

Yoan

No answers yet. Maybe you can help?

Tags
General Discussions
Asked by
Kostiantyn
Top achievements
Rank 1
Iron
Share this question
or