home *** CD-ROM | disk | FTP | other *** search
/ Freelog 119 / FreelogNo119-MarsAvril2014.iso / Securite / Granite / Granite.exe / ZipContentsDialog.cs < prev    next >
Text File  |  2011-06-11  |  8KB  |  227 lines

  1. // ZipContentsDialog.cs
  2. // ------------------------------------------------------------------
  3. //
  4. // Copyright (c) 2009 Dino Chiesa
  5. // All rights reserved.
  6. //
  7. // This code module is part of DotNetZip, a zipfile class library.
  8. //
  9. // ------------------------------------------------------------------
  10. //
  11. // This code is licensed under the Microsoft Public License. 
  12. // See the file License.txt for the license details.
  13. // More info on: http://dotnetzip.codeplex.com
  14. //
  15. // ------------------------------------------------------------------
  16. //
  17.  
  18. namespace Ionic.Zip.Forms
  19. {
  20.     using System;
  21.     using System.Collections.Generic;
  22.     using System.Windows.Forms;
  23.     using Ionic.Zip;
  24.     
  25.     public partial class ZipContentsDialog : Form
  26.     {
  27.         public ZipContentsDialog()
  28.         {
  29.             InitializeComponent();
  30.             FixTitle();
  31.         }
  32.  
  33.         private void FixTitle()
  34.         {
  35.             this.Text = String.Format("Contents of the zip archive (DotNetZip v{0})",
  36.                                       Ionic.Zip.ZipFile.LibraryVersion.ToString());
  37.         }
  38.  
  39.         public ZipFile ZipFile
  40.         {
  41.             set
  42.             {
  43.                 listView1.Clear();
  44.                 listView1.BeginUpdate();
  45.  
  46.                 string[] columnHeaders = new string[] { "name", "lastmod", "original", "ratio", "compressed", "enc?", "CRC" };
  47.                 foreach (string label in columnHeaders)
  48.                 {
  49.                     SortableColumnHeader ch = new SortableColumnHeader(label);
  50.                     if (label != "name" && label != "lastmod")
  51.                         ch.TextAlign = HorizontalAlignment.Right;
  52.                     listView1.Columns.Add(ch);
  53.                 }
  54.  
  55.                 foreach (ZipEntry e in value)
  56.                 {
  57.                     ListViewItem item = new ListViewItem(e.FileName);
  58.  
  59.                     string[] subitems = new string[] { 
  60.                         e.LastModified.ToString("yyyy-MM-dd HH:mm:ss"),
  61.                         e.UncompressedSize.ToString(),
  62.                         String.Format("{0,5:F0}%", e.CompressionRatio),
  63.                         e.CompressedSize.ToString(),
  64.                         (e.UsesEncryption) ? "Y" : "N",
  65.                         String.Format("{0:X8}", e.Crc)};
  66.  
  67.                     foreach (String s in subitems)
  68.                     {
  69.                         ListViewItem.ListViewSubItem subitem = new ListViewItem.ListViewSubItem();
  70.                         subitem.Text = s;
  71.                         item.SubItems.Add(subitem);
  72.                     }
  73.  
  74.                     this.listView1.Items.Add(item);
  75.                 }
  76.  
  77.                 listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
  78.  
  79.                 // adjust size of the entire form
  80.                 int aggWidth= 0;
  81.                 for (int i = 0; i < this.listView1.Columns.Count; i++)
  82.                 {
  83.                     aggWidth += this.listView1.Columns[i].Width + 1;
  84.                 }
  85.                 // plus a fudge factor
  86.                 //aggWidth += this.listView1.Columns[this.listView1.Columns.Count - 1].Width / 2 + this.listView1.Location.X * 4;
  87.                 aggWidth += this.listView1.Location.X * 4 + 4;
  88.                 this.Size = new System.Drawing.Size(aggWidth, this.Height);
  89.  
  90.                 this.listView1.EndUpdate();
  91.             }
  92.         }
  93.  
  94.  
  95.  
  96.  
  97.         private void button1_Click(object sender, EventArgs e)
  98.         {
  99.             this.Close();
  100.         }
  101.  
  102.         private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
  103.         {
  104.             // Create an instance of the ColHeader class.
  105.             SortableColumnHeader clickedCol = (SortableColumnHeader)this.listView1.Columns[e.Column];
  106.  
  107.             // Set the ascending property to sort in the opposite order.
  108.             clickedCol.SortAscending = !clickedCol.SortAscending;
  109.  
  110.             // Get the number of items in the list.
  111.             int numItems = this.listView1.Items.Count;
  112.  
  113.             // Turn off display while data is repoplulated.
  114.             this.listView1.BeginUpdate();
  115.  
  116.             // Populate an ArrayList with a SortWrapper of each list item.
  117.             List<ItemWrapper> list = new List<ItemWrapper>();
  118.             for (int i = 0; i < numItems; i++)
  119.             {
  120.                 list.Add(new ItemWrapper(this.listView1.Items[i], e.Column));
  121.             }
  122.  
  123.             if (e.Column == 2 || e.Column == 4)
  124.                 list.Sort(new ItemWrapper.NumericComparer(clickedCol.SortAscending));
  125.             else
  126.                 list.Sort(new ItemWrapper.StringComparer(clickedCol.SortAscending));
  127.  
  128.             // Clear the list, and repopulate with the sorted items.
  129.             this.listView1.Items.Clear();
  130.             for (int i = 0; i < numItems; i++)
  131.                 this.listView1.Items.Add(list[i].Item);
  132.  
  133.             // Turn display back on.
  134.             this.listView1.EndUpdate();
  135.         }
  136.  
  137.     }
  138.  
  139.  
  140.  
  141.     // The ColHeader class is a ColumnHeader object with an
  142.     // added property for determining an ascending or descending sort.
  143.     // True specifies an ascending order, false specifies a descending order.
  144.     public class SortableColumnHeader : ColumnHeader
  145.     {
  146.         public bool SortAscending;
  147.         public SortableColumnHeader(string text)
  148.         {
  149.             this.Text = text;
  150.             this.SortAscending = true;
  151.         }
  152.     }
  153.  
  154.  
  155.     // An instance of the SortWrapper class is created for
  156.     // each item and added to the ArrayList for sorting.
  157.     public class ItemWrapper
  158.     {
  159.         internal ListViewItem Item;
  160.         internal int Column;
  161.  
  162.         // A SortWrapper requires the item and the index of the clicked column.
  163.         public ItemWrapper(ListViewItem item, int column)
  164.         {
  165.             Item = item;
  166.             Column = column;
  167.         }
  168.  
  169.         // Text property for getting the text of an item.
  170.         public string Text
  171.         {
  172.             get { return Item.SubItems[Column].Text; }
  173.         }
  174.  
  175.         // Implementation of the IComparer
  176.         public class StringComparer : IComparer<ItemWrapper>
  177.         {
  178.             bool ascending;
  179.  
  180.             // Constructor requires the sort order;
  181.             // true if ascending, otherwise descending.
  182.             public StringComparer(bool asc)
  183.             {
  184.                 this.ascending = asc;
  185.             }
  186.  
  187.             // Implemnentation of the IComparer:Compare
  188.             // method for comparing two objects.
  189.             public int Compare(ItemWrapper xItem, ItemWrapper yItem)
  190.             {
  191.                 string xText = xItem.Item.SubItems[xItem.Column].Text;
  192.                 string yText = yItem.Item.SubItems[yItem.Column].Text;
  193.                 return xText.CompareTo(yText) * (this.ascending ? 1 : -1);
  194.             }
  195.         }
  196.  
  197.         public class NumericComparer : IComparer<ItemWrapper>
  198.         {
  199.             bool ascending;
  200.  
  201.             // Constructor requires the sort order;
  202.             // true if ascending, otherwise descending.
  203.             public NumericComparer(bool asc)
  204.             {
  205.                 this.ascending = asc;
  206.             }
  207.  
  208.             // Implemnentation of the IComparer:Compare
  209.             // method for comparing two objects.
  210.             public int Compare(ItemWrapper xItem, ItemWrapper yItem)
  211.             {
  212.                 int x = 0, y = 0;
  213.                 try
  214.                 {
  215.                     x = Int32.Parse(xItem.Item.SubItems[xItem.Column].Text);
  216.                     y = Int32.Parse(yItem.Item.SubItems[yItem.Column].Text);
  217.                 }
  218.                 catch
  219.                 {
  220.                 }
  221.                 return (x - y) * (this.ascending ? 1 : -1);
  222.             }
  223.         }
  224.     }
  225.  
  226. }
  227.