Quantcast
Channel: SharpDevelop Community
Viewing all articles
Browse latest Browse all 1764

ZipFile.Delete fails when called on a folder

$
0
0

When ZipFile.Delete is called on a Folder, a ZipException is thrown.

The call to FindExistingUpdate changes the name of what is being looked for and it fails to find the key.

I attached a sample program below but I did not see a way to upload a zip file. (Let me know if there is a way to do this.) The zip file was very basic and it contained a folder named "root_directory" which contained a sub folder named "sub_directory".

Should ZipFile.Delete handle folders? If not, is this forum post how I submit a bug report?

-----------------------------------------------------------------------------------

    internal class DeleteFileBugSampleProgram
    {
        private const string DeleteFileZipPath = @"C:\...\DeleteFiles.zip";
        private ZipFile _zipFile;
 
        public static void Test()
        {
            var program = new DeleteFileBugSampleProgram();
            program.TestZip(DeleteFileZipPath);
        }
 
        private void TestZip(string zipPath)
        {
            InitializeZip(zipPath);
 
            DeleteFromZip("root_directory/sub_directory/");
            DeleteFromZip("root_empty_directory/");
        }
        
        private void InitializeZip(string zipPath)
        {
            FileStream zipFileStream = new FileStream(zipPath, FileMode.Open);
            MemoryStream zipMemoryStream = GetMemoryStream(zipFileStream);
            _zipFile = new ZipFile(zipMemoryStream);
            zipFileStream.Close();
        }
 
        private void DeleteFromZip(string name)
        {
            _zipFile.BeginUpdate();
 
            ZipEntry entryToDelete = _zipFile.GetEntry(name);
            if (entryToDelete != null)
            {
                // !!! THIS IS WHERE THE FAILURE WILL OCCUR !!!
                _zipFile.Delete(entryToDelete);
            }
 
            _zipFile.CommitUpdate();
        }
 
        private static MemoryStream GetMemoryStream(Stream stream)
        {
            MemoryStream ms = null;
            MemoryStream allocatedStream = null;
            try
            {
                if (stream.CanSeek)
                {
                    int size = (int)stream.Length;
                    byte[ buf = new byte[size];
                    stream.Read(buf, 0, size);
                    allocatedStream = new MemoryStream(size); // Not using the byte[ constructors because we need it to be resizable
                    allocatedStream.Write(buf, 0, size);
                    allocatedStream.Position = 0;
                    ms = allocatedStream;
                    allocatedStream = null;
                }
                else
                {
                    allocatedStream = new MemoryStream();
                    byte[ buf = new byte[4096];
                    int readBytes = 0;
                    do
                    {
                        readBytes = stream.Read(buf, 0, 4096);
                        allocatedStream.Write(buf, 0, readBytes);
                    }
                    while (readBytes > 0);
                    allocatedStream.Position = 0;
                    ms = allocatedStream;
                    allocatedStream = null;
                }
            }
            finally
            {
                if (allocatedStream != null)
                {
                    allocatedStream.Dispose();
                }
            }
            return ms;
        }
 
        private class InMemoryStaticDataSource : IStaticDataSource
        {
            private byte[ _source;
 
            public InMemoryStaticDataSource(byte[ source)
            {
                _source = source;
            }
 
            #region IStaticDataSource Members
 
            public Stream GetSource()
            {
                MemoryStream ms = new MemoryStream(_source, false);
                return ms;
            }
 
            #endregion
        }
    }

Viewing all articles
Browse latest Browse all 1764

Trending Articles