SharpDevelop Community

Get your problems solved!
Welcome to SharpDevelop Community Sign in | Join | Help
in Search

reading a zip file in Windows(Re: CleanName in ZipEntry.cs)

Last post 07-16-2015 3:18 PM by gimmely. 6 replies.
Page 1 of 1 (7 items)
Sort Posts: Previous Next
  • 04-28-2015 10:20 PM

    reading a zip file in Windows(Re: CleanName in ZipEntry.cs)

  • 05-11-2015 3:23 PM In reply to

    Re: reading a zip file in Windows(Re: CleanName in ZipEntry.cs)

  • 05-27-2015 5:04 PM In reply to

    Re: reading a zip file in Windows(Re: CleanName in ZipEntry.cs)

    (re-post in Firefox)

    I came across this issue only recently, more than a year after the same code was successfully tested and implemented in production.  So, I don't know if I had an incorrect design/coding in the first place or had tested it incorrectly or something changed in Windows or else.  But here it is:

    I have the following lines in my own program in VB.NET:

    ...

                For Each sRenamedzipfile In arrayRenamedZipFileList
    ...

                    Try

                        zfZipFile = New ZipFile(File.OpenRead(sRenamedzipfile))
    ...

                            Try

    ...
                                For Each zeZipEntry As ZipEntry In zfZipFile
                                    Dim sEntryFileName As String = funcSimplifyString(zeZipEntry.Name)

    ...
                            Catch exUnzippingToTempDirectory As Exception
    ...
                            End Try

    ...
                    Catch exZipException As ZipException
    ...

                    Finally

                        If zfZipFile IsNot Nothing Then

                            zfZipFile.IsStreamOwner = True     ' Makes close also shut the underlying stream
                            zfZipFile.Close()

                        End If

                    End Try
    ...

    When a zip file shows up with a file inside having some special character(s) unacceptable to Windows in its name, the program crashes at the line of zfZipFile = New ZipFile(File.OpenRead(sRenamedzipfile)).  Because the environment is Windows, handling unacceptable characters in filename was one task in the original design, which, however, happens before the content of a zip file is to be unzipped(funcSimplifyString(zeZipEntry.Name)) and after the content has been read at this line.

    I'd tried all three methods to read a zip file, but got the same result as program crashing at the same line.  Although I was very confused why this situation wasn't caught in my testing, I had to find a way to make the program work.  So, I found this:

    The crash actually happened on the line of ReadEntries(); as below in ZipFile.cs:

            public ZipFile(string name)
            {
                if ( name == null ) {
                    throw new ArgumentNullException("name");
                }
                
                name_ = name;

                baseStream_ = File.Open(name, FileMode.Open, FileAccess.Read, FileShare.Read);
                isStreamOwner = true;
                
                try {
                    ReadEntries();
                }
                catch {
                    DisposeInternal(true);
                    throw;
                }
            }

    Long story short, I had to add the following lines into public static string CleanName(string name) in ZipEntry.cs:

                name = name.Replace(@":""");
                name = name.Replace(@"*""");
                name = name.Replace(@"?""");
                name = name.Replace(@"""""");
                name = name.Replace(@"<""");
                name = name.Replace(@">""");
                name = name.Replace(@"|""");

    to make it look like:

            public static string CleanName(string name)
            {
                if (name == null) {
                    return string.Empty;
                }
                
                name = name.Replace(@":""");
                name = name.Replace(@"*""");
                name = name.Replace(@"?""");
                name = name.Replace(@"""""");
                name = name.Replace(@"<""");
                name = name.Replace(@">""");
                name = name.Replace(@"|""");

                if (Path.IsPathRooted(name)) {
                    // NOTE:
                    // for UNC names...  \\machine\share\zoom\beet.txt gives \zoom\beet.txt
                    name = name.Substring(Path.GetPathRoot(name).Length);
                }

                name = name.Replace(@"\""/");
                
                while ( (name.Length > 0) && (name[0] == '/')) {
                    name = name.Remove(01);
                }
                return name;
            }

    Of course, my program started to work (again) after the DLL was re-built and added as the correct/new reference in my program.

    I believe this is a common issue in Windows, so I don't know if I'm on the right path in taking care of it or just overkilling it.  I'd very much appreciate any comment or idea.

  • 06-08-2015 3:17 PM In reply to

    Re: reading a zip file in Windows(Re: CleanName in ZipEntry.cs)

    anyone care to comment?

  • 06-09-2015 12:49 AM In reply to

    Re: reading a zip file in Windows(Re: CleanName in ZipEntry.cs)

    I'll have a look at this one. I don't recall anyone with these characters before. Are they from a Unix box ?

  • 06-09-2015 3:02 PM In reply to

    Re: reading a zip file in Windows(Re: CleanName in ZipEntry.cs)

    Thanks, David.

    I think Unix box is a possibility, but most, if not all, of my cases are about files from Apple products.

  • 07-16-2015 3:18 PM In reply to

    Re: reading a zip file in Windows(Re: CleanName in ZipEntry.cs)

    David and others,

    Has this case been considered, further investigated or concluded?

Page 1 of 1 (7 items)
Powered by Community Server (Commercial Edition), by Telligent Systems
Don't contact us via this (fleischfalle@alphasierrapapa.com) email address.