Skip to content

Can not delete directory from zip archive bug #82

@VladimirSmall

Description

@VladimirSmall

ICSharpCode.SharpZipLib version - 0.86.0.518.

Code to demonstrate the bug:

namespace SharpZipLibDeleteFolderBug
{
    using ICSharpCode.SharpZipLib.Zip;

    using NUnit.Framework;

    [TestFixture]
    public class SharpZipLibDeleteFolderBugDemo
    {
        [Test]
        public void Bug()
        {
            var zipFile = ZipFile.Create("SomeFile.zip");
            CreateDirectory(zipFile, "SomeDirectory");

            DeleteDirectory(zipFile, "SomeDirectory");
        }

        private void DeleteDirectory(ZipFile zipFile, string directoryName)
        {
            zipFile.BeginUpdate();

            var zipEntry = zipFile.GetEntry(directoryName + "/");
            Assert.That(zipEntry, Is.Not.Null); // passed - directory was successfully create

            zipFile.Delete(zipEntry); // !!! throws ICSharpCode.SharpZipLib.Zip.ZipException : Cannot find entry to delete

            zipFile.CommitUpdate();            
        }

        private static void CreateDirectory(ZipFile zipFile, string directoryName)
        {
            zipFile.BeginUpdate();
            zipFile.AddDirectory(directoryName);
            zipFile.CommitUpdate();
        }
    }
}

The buggy method is ZipFile.FindExistingUpdate:

        int FindExistingUpdate(ZipEntry entry)
        {
            int result = -1;
            string convertedName = GetTransformedFileName(entry.Name); // !!! BUG is here

            if (updateIndex_.ContainsKey(convertedName)) {
                result = (int)updateIndex_[convertedName];
            }

                        ...
            return result;
        }

Method FindExistingUpdate ignores the fact that entry can be not only file entry but also a directory entry. Right implementation should be:

        int FindExistingUpdate(ZipEntry entry)
        {
            int result = -1;
            string convertedName;
            if (entry.IsDirectory)
                convertedName = GetTransformedDirectoryName(entry.Name);
            else 
                convertedName = GetTransformedFileName(entry.Name);

            if (updateIndex_.ContainsKey(convertedName)) {
                result = (int)updateIndex_[convertedName];
            }

                        ...
            return result;
        }

Method ZipFile.Delete(string fileName) also has the same bug.

Metadata

Metadata

Assignees

Labels

bugzipRelated to ZIP file format

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions