Search code examples
c#.netdotnetzip

.NET 3.5 DotNetZip Problems searching through extracted files


I have zip files I need to search through I have working code for .NET 4.5 using System.IO.Compression and it works flawlessly but I can't seem to get it to work using DotNetZip in .NET 3.5

Here is my code

di = new DirectoryInfo(sFullLogPath + "\\");
                files = di.GetFiles(sExtension);
                logs.Info("Searching " + sType + " logs on  " + sDate + " for " + sSearchTerm);
                Console.WriteLine("");
                try
                {
                    foreach(var file in files)
                    {
                        //Console.WriteLine(file.FullName);
                        using (ZipFile archive = ZipFile.Read(sFullLogPath + "\\" + file.Name))
                        {
                            foreach(var entry in archive)
                            {
                                if(entry.FileName.EndsWith(".ininlog",StringComparison.OrdinalIgnoreCase))
                                {
                                    //Console.WriteLine(entry.FileName);
                                    try
                                    {
                                        Directory.CreateDirectory(sTemp);
                                        entry.Extract(Path.Combine(sTemp, entry.FileName));
                                        Stream streamTemp = new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                                        StreamReader streamReader = new StreamReader(streamTemp);
                                        string line;
                                        while ((line = streamReader.ReadLine()) != null)
                                        {
                                            if (line.Contains(sSearchTerm))
                                            { logs.Info("{0} contains \"{1}\"", file.Name, sSearchTerm); break; }
                                            else
                                            { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("{0} does not contain \"{1}\"", file.Name, sSearchTerm); Console.ResetColor(); break; }
                                        }
                                    }
                                    catch (UnauthorizedAccessException ae)
                                    {
                                        logs.Error(ae.Message);
                                    }
                                    catch (SystemException se)
                                    {
                                        logs.Error(se.Message);
                                    }
                                    catch (ApplicationException ape)
                                    {
                                        logs.Error(ape.Message);
                                    }
                                    catch (Exception e)
                                    {
                                        logs.Error(e.Message);
                                    }
                                    finally
                                    {
                                        Directory.Delete(sTemp, true);
                                        archive.Dispose();
                                    }
                                }
                            }
                        }
                    }
                }
                catch (UnauthorizedAccessException ae)
                {
                    logs.Error(ae.Message);
                }
                catch (SystemException se)
                {
                    logs.Error(se.Message);
                }
                catch (ApplicationException ape)
                {
                    logs.Error(ape.Message);
                }
                catch (Exception e)
                {
                    logs.Error(e.Message);
                }

Expected Behavior:

Each zipped file in a directory is unzipped to a temporary location then searched to see if the string is in that file, if found it will output the name of the file it was found in

Current Behavior:

Not finding and referencing to the string I am looking for even though I know it's there and it works fine with the .NET 4.5 version on the same file

Why don't I use .NET 4.5, we have custom software on our servers and it currently has issues with .NET 4.5 .NET 4.5 Version using System.IO.Compression .NET 3.5 Version Using DotNetZip

Fixed code thanks to @Thomas - looks like the else was causing the false positive

else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("{0} does not contain \"{1}\"", file.Name, sSearchTerm); Console.ResetColor(); break; } 

Here is the fixed code

di = new DirectoryInfo(sFullLogPath + "\\");
                files = di.GetFiles(sExtension);
                logs.Info("Searching " + sType + " logs on  " + sDate + " for " + sSearchTerm);
                Console.WriteLine("");
                try
                {
                    foreach(var file in files)
                    {
                        //Console.WriteLine(file.Name);
                        using (ZipFile archive = ZipFile.Read(file.FullName))
                        {
                            foreach(var entry in archive)
                            {
                                if(entry.FileName.EndsWith(".ininlog", StringComparison.OrdinalIgnoreCase))
                                {
                                    try
                                    {
                                        using (var tempStream = entry.OpenReader())
                                        using (var streamReader = new StreamReader(tempStream))
                                        {
                                            string line;
                                            while ((line = streamReader.ReadLine()) != null)
                                            {
                                                if(line.Contains(sSearchTerm))
                                                {
                                                    logs.Info("{0} contains \"{1}\"", file.Name, sSearchTerm);
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                    catch (UnauthorizedAccessException ae)
                                    {
                                        logs.Error(ae.Message);
                                    }
                                    catch (SystemException se)
                                    {
                                        logs.Error(se.Message);
                                    }
                                    catch (ApplicationException ape)
                                    {
                                        logs.Error(ape.Message);
                                    }
                                    catch (Exception e)
                                    {
                                        logs.Error(e.Message);
                                    }
                                    finally
                                    {
                                        archive.Dispose();
                                        //Directory.Delete(sTemp + "\\" + entry.FileName);
                                    }
                                }
                            }
                        }
                    }
                }
                catch (UnauthorizedAccessException ae)
                {
                    logs.Error(ae.Message);
                }
                catch (SystemException se)
                {
                    logs.Error(se.Message);
                }
                catch (ApplicationException ape)
                {
                    logs.Error(ape.Message);
                }
                catch (Exception e)
                {
                    logs.Error(e.Message);
                }
                finally
                {
                    //
                }
                break;

Solution

  • You're opening the wrong file here:

    Stream streamTemp = new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    

    file is the ZIP file, not the file you just extracted.

    BTW, I don't think you need to extract the files, DotNetZip probably allows you to write the uncompressed stream directly.


    EDIT: I just checked: you can use the OpenReader method to get a stream to read the uncompressed content of the entry. Just create the StreamReader around that stream; no need to actually extract the file.

    ...
    try
    {
        using (var stream = entry.OpenReader())
        using (var reader = new StreamReader(stream))
        {
            string line;
            while ((line = streamReader.ReadLine()) != null)
            {
                if (line.Contains(sSearchTerm))
                { logs.Info("{0} contains \"{1}\"", file.Name, sSearchTerm); break; }
                else
                { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("{0} does not contain \"{1}\"", file.Name, sSearchTerm); Console.ResetColor(); break; }
            }
        }
    }
    ...