天天看點

Using ICSharpCode.SharpZipLib for zip file

View Code

1 try
 2             {
 3                 VQP.Common.FZip fileZip = new VQP.Common.FZip();
 4                 fileZip.FileNamesToZIP = new List<string>();
 5                 fileZip.FileNamesToZIP.Add(@"C:\test.txt");
 6                 fileZip.FileNameZipped = @"C:\test.zip";
 7                 fileZip.ZipFiles();
 8             }
 9             catch (Exception ex)
10             {
11                 lblMessage.Text = ex.ToString(); 
12             }       
      

View Code

1 using System;
  2  using System.Collections.Generic;
  3 using System.Text;
  4 using ICSharpCode.SharpZipLib.Zip;
  5 using ICSharpCode.SharpZipLib.Checksums;
  6 using ICSharpCode.SharpZipLib.GZip;
  7 using System.IO;
  8 
  9 namespace VQP.Common
 10 {
 11     public class FZip : IDisposable
 12     {
 13         private string m_FolderToZIP;
 14         private List<string> m_FileNamesToZIP;
 15         private string m_FileNameZipped;
 16         
 17         private ZipOutputStream m_ZipStream = null;
 18         private Crc32 m_Crc;
 19 
 20         #region Begin for Public Properties
 21         /// <summary>
 22         /// Folder Name to ZIP
 23         /// Like "C:Test"
 24         /// </summary>
 25         public  string FolderToZIP
 26         {
 27             get { return m_FolderToZIP; }
 28             set { m_FolderToZIP = value; }
 29         }
 30 
 31         /// <summary>
 32         /// File Name to ZIP
 33         /// Like "C:TestTest.txt"
 34         /// </summary>
 35         public  List<string> FileNamesToZIP
 36         {
 37             get { return m_FileNamesToZIP; }
 38             set { m_FileNamesToZIP = value; }
 39         }
 40 
 41         /// <summary>
 42         /// Zipped File Name 
 43         /// Like "C:TestMyZipFile.ZIP"
 44         /// </summary>
 45         public  string FileNameZipped
 46         {
 47             get { return m_FileNameZipped; }
 48             set { m_FileNameZipped = value; }
 49         }
 50         #endregion
 51 
 52         /// <summary>
 53         /// The construct
 54         /// </summary>
 55         public FZip()
 56         {
 57             this.m_FolderToZIP = "";
 58             this.m_FileNamesToZIP = new List<string>();
 59             this.m_FileNameZipped = "";
 60         }
 61 
 62         #region ZipFolder
 63         /// <summary>
 64         /// Zip one folder : single level
 65         /// Before doing this event, you must set the Folder and the ZIP file name you want
 66         /// </summary>
 67         public void ZipFolder()
 68         {
 69             if (this.m_FolderToZIP.Trim().Length == 0)
 70             {
 71                 throw new Exception("You must setup the folder name you want to zip!");
 72             }
 73 
 74             if(Directory.Exists(this.m_FolderToZIP) == false)
 75             {
 76                 throw new Exception("The folder you input does not exist! Please check it!");
 77             }
 78             
 79             if (this.m_FileNameZipped.Trim().Length == 0)
 80             {
 81                 throw new Exception("You must setup the zipped file name!");
 82             }
 83             
 84             string[] fileNames = Directory.GetFiles(this.m_FolderToZIP.Trim());
 85 
 86             if (fileNames.Length == 0)
 87             {
 88                 throw new Exception("Can not find any file in this folder(" + this.m_FolderToZIP + ")!");
 89             }
 90 
 91             // Create the Zip File
 92             this.CreateZipFile(this.m_FileNameZipped);
 93 
 94             // Zip all files
 95             foreach(string file in fileNames)
 96             {
 97                 this.ZipSingleFile(file);
 98             }
 99 
100             // Close the Zip File
101             this.CloseZipFile();
102         }
103         #endregion
104 
105         #region ZipFiles
106         /// <summary>
107         /// Zip files
108         /// Before doing this event, you must set the Files name and the ZIP file name you want
109         /// </summary>
110         public void ZipFiles()
111         {
112             if (this.m_FileNamesToZIP.Count == 0)
113             {
114                 throw new Exception("You must setup the files name you want to zip!");
115             }
116 
117             foreach(object file in this.m_FileNamesToZIP)
118             {
119                 if(File.Exists(((string)file).Trim()) == false)
120                 {
121                     throw new Exception("The file(" + (string)file + ") you input does not exist! Please check it!");
122                 }
123             }
124 
125             if (this.m_FileNameZipped.Trim().Length == 0)
126             {
127                 throw new Exception("You must input the zipped file name!");
128             }
129 
130             // Create the Zip File
131             this.CreateZipFile(this.m_FileNameZipped);
132 
133             // Zip this File
134             foreach(object file in this.m_FileNamesToZIP)
135             {
136                 this.ZipSingleFile((string)file);
137             }
138 
139             // Close the Zip File
140             this.CloseZipFile();
141         }
142         #endregion
143 
144         #region CreateZipFile
145         /// <summary>
146         /// Create Zip File by FileNameZipped
147         /// </summary>
148         /// <param name="fileNameZipped">zipped file name like "C:TestMyZipFile.ZIP"</param>
149         private void CreateZipFile(string fileNameZipped)
150         {
151             this.m_Crc = new Crc32();
152             this.m_ZipStream = new ZipOutputStream(File.Create(fileNameZipped));
153             this.m_ZipStream.SetLevel(6); // 0 - store only to 9 - means best compression
154         }
155         #endregion
156 
157         #region CloseZipFile
158         /// <summary>
159         /// Close the Zip file
160         /// </summary>
161         private void CloseZipFile()
162         {
163             this.m_ZipStream.Finish();
164             this.m_ZipStream.Close();
165             this.m_ZipStream = null;
166         }
167         #endregion
168 
169         #region ZipSingleFile
170         /// <summary>
171         /// Zip single file 
172         /// </summary>
173         /// <param name="fileName">file name like "C:TestTest.txt"</param>
174         private void ZipSingleFile(string fileNameToZip)
175         {
176             // Open and read this file
177             FileStream fso = File.OpenRead(fileNameToZip);
178 
179             // Read this file to Buffer
180             byte[] buffer = new byte[fso.Length];
181             fso.Read(buffer,0,buffer.Length);
182 
183             // Create a new ZipEntry
184             ZipEntry zipEntry = new ZipEntry(fileNameToZip); 
185           //  ZipEntry zipEntry = new ZipEntry(fileNameToZip.Split('\')[fileNameToZip.Split('\').Length - 1]); 
186             
187             zipEntry.DateTime = DateTime.Now;
188             // set Size and the crc, because the information
189             // about the size and crc should be stored in the header
190             // if it is not set it is automatically written in the footer.
191             // (in this case size == crc == -1 in the header)
192             // Some ZIP programs have problems with zip files that don't store
193             // the size and crc in the header.
194             zipEntry.Size = fso.Length;
195 
196             fso.Close();
197             fso = null;
198 
199             // Using CRC to format the buffer
200             this.m_Crc.Reset();
201             this.m_Crc.Update(buffer);
202             zipEntry.Crc = this.m_Crc.Value;
203 
204             // Add this ZipEntry to the ZipStream
205             this.m_ZipStream.PutNextEntry(zipEntry);
206             this.m_ZipStream.Write(buffer,0,buffer.Length);
207         }
208         #endregion
209 
210         #region IDisposable member
211 
212         /// <summary>
213         /// Release all objects
214         /// </summary>
215         public void Dispose()
216         {
217             if(this.m_ZipStream != null)
218             {
219                 this.m_ZipStream.Close();
220                 this.m_ZipStream = null;
221             }
222         }
223 
224         #endregion
225     }
226 }