利用 SharpZipLib方便地压缩和解压缩文件[ZT]
最新版本的SharpZipLib(0.84)增加了很多新的功能,其中包括增加了FastZip类,这让我们可以非常方便地把一个目录压缩成一个压缩包,经测试可以很好地支持文件中包含中文以及空格的情况。
 1 /// <summary>
   /// <summary>
2 /// Create a zip archive.
    /// Create a zip archive.
3 /// </summary>
    /// </summary>
4 /// <param name="filename">The filename.</param>
    /// <param name="filename">The filename.</param>
5 /// <param name="directory">The directory to zip.</param>
    /// <param name="directory">The directory to zip.</param> 
6 public static void PackFiles(string filename, string directory)
    public static void PackFiles(string filename, string directory)
7 {
    {
8 try
        try
9 {
        {
10 FastZip fz = new FastZip();
            FastZip fz = new FastZip();
11 fz.CreateEmptyDirectories = true;
            fz.CreateEmptyDirectories = true;
12 fz.CreateZip(filename, directory, true, "");
            fz.CreateZip(filename, directory, true, "");
13 fz = null;
            fz = null;
14 }
        }
15 catch (Exception)
        catch (Exception)
16 {
        {
17 throw;
            throw;
18 }
        }
19 }
    }
20
21 /// <summary>
    /// <summary>
22 /// Unpacks the files.
    /// Unpacks the files.
23 /// </summary>
    /// </summary>
24 /// <param name="file">The file.</param>
    /// <param name="file">The file.</param>
25 /// <returns>if succeed return true,otherwise false.</returns>
    /// <returns>if succeed return true,otherwise false.</returns>
26 public static bool UnpackFiles(string file, string dir)
    public static bool UnpackFiles(string file, string dir)
27 {
    {
28 try
        try
29 {
        {
30 if (!Directory.Exists(dir))
            if (!Directory.Exists(dir))
31 Directory.CreateDirectory(dir);
                Directory.CreateDirectory(dir);
32
33 ZipInputStream s = new ZipInputStream(File.OpenRead(file));
            ZipInputStream s = new ZipInputStream(File.OpenRead(file));
34
35 ZipEntry theEntry;
            ZipEntry theEntry;
36 while ((theEntry = s.GetNextEntry()) != null)
            while ((theEntry = s.GetNextEntry()) != null)
37 {
            {
38
39 string directoryName = Path.GetDirectoryName(theEntry.Name);
                string directoryName = Path.GetDirectoryName(theEntry.Name);
40 string fileName = Path.GetFileName(theEntry.Name);
                string fileName = Path.GetFileName(theEntry.Name);
41
42 if (directoryName != String.Empty)
                if (directoryName != String.Empty)
43 Directory.CreateDirectory(dir + directoryName);
                    Directory.CreateDirectory(dir + directoryName);
44
45 if (fileName != String.Empty)
                if (fileName != String.Empty)
46 {
                {
47 FileStream streamWriter = File.Create(dir + theEntry.Name);
                    FileStream streamWriter = File.Create(dir + theEntry.Name);
48
49 int size = 2048;
                    int size = 2048;
50 byte[] data = new byte[2048];
                    byte[] data = new byte[2048];
51 while (true)
                    while (true)
52 {
                    {
53 size = s.Read(data, 0, data.Length);
                        size = s.Read(data, 0, data.Length);
54 if (size > 0)
                        if (size > 0)
55 {
                        {
56 streamWriter.Write(data, 0, size);
                            streamWriter.Write(data, 0, size);
57 }
                        }
58 else
                        else
59 {
                        {
60 break;
                            break;
61 }
                        }
62 }
                    }
63
64 streamWriter.Close();
                    streamWriter.Close();
65 }
                }
66 }
            }
67 s.Close();
            s.Close();
68 return true;
            return true;
69 }
        }
70 catch (Exception)
        catch (Exception)
71 {
      {
72 throw;
            throw;
73 }上面代码需要引用:
        }上面代码需要引用:
 /// <summary>
   /// <summary>2
 /// Create a zip archive.
    /// Create a zip archive.3
 /// </summary>
    /// </summary>4
 /// <param name="filename">The filename.</param>
    /// <param name="filename">The filename.</param>5
 /// <param name="directory">The directory to zip.</param>
    /// <param name="directory">The directory to zip.</param> 6
 public static void PackFiles(string filename, string directory)
    public static void PackFiles(string filename, string directory)7
 {
    {8
 try
        try9
 {
        {10
 FastZip fz = new FastZip();
            FastZip fz = new FastZip();11
 fz.CreateEmptyDirectories = true;
            fz.CreateEmptyDirectories = true;12
 fz.CreateZip(filename, directory, true, "");
            fz.CreateZip(filename, directory, true, "");13
 fz = null;
            fz = null;14
 }
        }15
 catch (Exception)
        catch (Exception)16
 {
        {17
 throw;
            throw;18
 }
        }19
 }
    }20

21
 /// <summary>
    /// <summary>22
 /// Unpacks the files.
    /// Unpacks the files.23
 /// </summary>
    /// </summary>24
 /// <param name="file">The file.</param>
    /// <param name="file">The file.</param>25
 /// <returns>if succeed return true,otherwise false.</returns>
    /// <returns>if succeed return true,otherwise false.</returns>26
 public static bool UnpackFiles(string file, string dir)
    public static bool UnpackFiles(string file, string dir)27
 {
    {28
 try
        try29
 {
        {30
 if (!Directory.Exists(dir))
            if (!Directory.Exists(dir))31
 Directory.CreateDirectory(dir);
                Directory.CreateDirectory(dir);32

33
 ZipInputStream s = new ZipInputStream(File.OpenRead(file));
            ZipInputStream s = new ZipInputStream(File.OpenRead(file));34

35
 ZipEntry theEntry;
            ZipEntry theEntry;36
 while ((theEntry = s.GetNextEntry()) != null)
            while ((theEntry = s.GetNextEntry()) != null)37
 {
            {38

39
 string directoryName = Path.GetDirectoryName(theEntry.Name);
                string directoryName = Path.GetDirectoryName(theEntry.Name);40
 string fileName = Path.GetFileName(theEntry.Name);
                string fileName = Path.GetFileName(theEntry.Name);41

42
 if (directoryName != String.Empty)
                if (directoryName != String.Empty)43
 Directory.CreateDirectory(dir + directoryName);
                    Directory.CreateDirectory(dir + directoryName);44

45
 if (fileName != String.Empty)
                if (fileName != String.Empty)46
 {
                {47
 FileStream streamWriter = File.Create(dir + theEntry.Name);
                    FileStream streamWriter = File.Create(dir + theEntry.Name);48

49
 int size = 2048;
                    int size = 2048;50
 byte[] data = new byte[2048];
                    byte[] data = new byte[2048];51
 while (true)
                    while (true)52
 {
                    {53
 size = s.Read(data, 0, data.Length);
                        size = s.Read(data, 0, data.Length);54
 if (size > 0)
                        if (size > 0)55
 {
                        {56
 streamWriter.Write(data, 0, size);
                            streamWriter.Write(data, 0, size);57
 }
                        }58
 else
                        else59
 {
                        {60
 break;
                            break;61
 }
                        }62
 }
                    }63

64
 streamWriter.Close();
                    streamWriter.Close();65
 }
                }66
 }
            }67
 s.Close();
            s.Close();68
 return true;
            return true;69
 }
        }70
 catch (Exception)
        catch (Exception)71
 {
      {72
 throw;
            throw;73
 }上面代码需要引用:
        }上面代码需要引用:
 using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Zip; using System.IO;
using System.IO;使用方法就不介绍了。
你可以到其官方网站下载最新版本的SharpZipLib:
http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx
或者直接使用该链接下载:
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号