context: using System;
  using System.Collections.Generic;
  using System.Drawing;
  using System.IO;
  using System.Text;
  using ICSharpCode.SharpZipLib.Zip;
  using DarkMouse.Explorer;
  using DarkMouse.Util;
  
  namespace DarkMouse.Explorer.Zip
  {
      public enum LoadResult
      {
          Success,
          NoFile,
          NoPassword,
          OpenError
      }
  
      public class ZipUtil
      {
          public static bool InZip = false;
          public static bool Preload = false;
          public static string FileName = "";
          public static string Password = "";
          public static string ZipPath = "";
          public static Dictionary<string, Entry> ZipTree = new Dictionary<string, Entry>();
  
          public static LoadResult Load(string file, string password, bool preload)
          {
              FileInfo fi = new FileInfo(file);
              if (!fi.Exists)
              {
                  return LoadResult.NoFile;
              }
  
              Preload = preload;
              FileName = fi.Name;
              Password = password;
              ZipPath = "";
              Entry root = new Entry();
              root.Name = fi.Name;
              ZipTree.Clear();
              ZipTree.Add("", root);
  
              using (ZipInputStream s = new ZipInputStream(File.OpenRead(file)))
              {
                  if (Password.Length > 0)
                  {
                      s.Password = Password;
                  }
  
                  ZipEntry ze;
                  try
                  {
                      while ((ze = s.GetNextEntry()) != null)
                      {
                          Entry entry = new Entry();
                          entry.IsFile = ze.IsFile;
                          entry.Name = ze.Name.TrimEnd('/');
                          if (entry.Name.IndexOf('/') != -1)
                          {
                              entry.Name = entry.Name.Substring(entry.Name.LastIndexOf('/') + 1);
                          }
                          if (ze.IsDirectory)
                          {
                              ZipTree.Add(ze.Name.Replace('/', '\\'), entry);
                          }
                          else
                          {
                              entry.Size = ze.Size;
                              if (Preload && FileUtil.IsFileAImage(entry.Name))
                              {
                                  try
                                  {
                                      entry.ImageStream = new MemoryStream();
                                      int size = 2048;
                                      byte[] data = new byte[2048];
                                      while (true)
                                      {
                                          size = s.Read(data, 0, data.Length);
                                          if (size > 0)
                                          {
                                              entry.ImageStream.Write(data, 0, size);
                                          }
                                          else
                                          {
                                              break;
                                          }
                                      }
                                      entry.Image = Image.FromStream(entry.ImageStream);
                                  }
                                  catch (Exception)
                                  {
                                      if (entry.Image != null)
                                      {
                                          entry.Image.Dispose();
                                          entry.Image = null;
                                      }
                                      if (entry.ImageStream != null)
                                      {
                                          entry.ImageStream.Close();
                                          entry.ImageStream = null;
                                      }
                                  }
                              }
                          }
                          if (ze.Name.TrimEnd('/').IndexOf('/') == -1)
                          {
                              ZipTree[""].Children.Add(entry);
                          }
                          else
                          {
                              string parent = ze.Name.Substring(0, ze.Name.TrimEnd('/').LastIndexOf('/') + 1);
                              ZipTree[parent.Replace('/', '\\')].Children.Add(entry);
                          }
                      }
                  }
                  catch (Exception e)
                  {
                      if (e.Message.Equals("No password set."))
                      {
                          return LoadResult.NoPassword;
                      }
                      else
                      {
                          return LoadResult.OpenError;
                      }
                  }
                  s.Close();
                  return LoadResult.Success;
              }
          }
  
          public static Image GetImage(string name)
          {
              if (ZipTree == null || !ZipTree.ContainsKey(ZipPath))
              {
                  return null;
              }
              if (Preload)
              {
                  foreach (Entry entry in ZipTree[ZipPath].Children)
                  {
                      if (entry.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase) && entry.IsFile)
                      {
                          return entry.Image;
                      }
                  }
              }
              else
              {
                  using (ZipInputStream s = new ZipInputStream(File.OpenRead(ItemList.DirPath + FileName)))
                  {
                      if (Password.Length > 0)
                      {
                          s.Password = Password;
                      }
                      ZipEntry ze;
                      while ((ze = s.GetNextEntry()) != null)
                      {
                          if (ze.Name.Equals(ZipPath.Replace('\\', '/') + name, StringComparison.CurrentCultureIgnoreCase))
                          {
                              try
                              {
                                  Renderer.ImageStream = new MemoryStream();
                                  int size = 2048;
                                  byte[] data = new byte[2048];
                                  while (true)
                                  {
                                      size = s.Read(data, 0, data.Length);
                                      if (size > 0)
                                      {
                                          Renderer.ImageStream.Write(data, 0, size);
                                      }
                                      else
                                      {
                                          break;
                                      }
                                  }
                                  return Image.FromStream(Renderer.ImageStream);
                              }
                              catch (Exception)
                              {
                                  if (Renderer.ImageStream != null)
                                  {
                                      Renderer.ImageStream.Close();
                                      Renderer.ImageStream = null;
                                  }
                              }
                              break;
                          }
                      }
                  }
              }
              return null;
          }
  
          public static List<Entry> GetList()
          {
              if (ZipTree == null || !ZipTree.ContainsKey(ZipPath))
              {
                  return new List<Entry>();
              }
              return ZipTree[ZipPath].Children;
          }
  
          public static void Dispose()
          {
              foreach (KeyValuePair<string, Entry> kv in ZipTree)
              {
  
gt:                 if (kv.Value.Image != null)