如何压缩多个文件/文件夹(GZipStream and C#)

如何压缩多个文件/文件夹(GZipStream and C#)


.Net Framework 2.0 中添加了System.IO.Compression 类来实现对文件的压缩/解压(GZipStream方法),下面我们来看一个简单的例子.

Code1:

 1        public static void Compress(string filePath, string zipPath)
 2        {
 3            FileStream sourceFile = File.OpenRead(filePath);
 4            FileStream destinationFile = File.Create(zipPath);
 5            byte[] buffer = new byte[sourceFile.Length];
 6            GZipStream zip = null;
 7            try
 8            {
 9                sourceFile.Read(buffer, 0, buffer.Length);
10                zip = new GZipStream(destinationFile, CompressionMode.Compress);
11                zip.Write(buffer, 0, buffer.Length);
12            }

13            catch
14            {
15                throw;
16            }

17            finally
18            {
19                zip.Close();
20                sourceFile.Close();
21                destinationFile.Close();
22            }

23        }

24
25        public static void Decompress(string zipPath,string filePath)
26        {
27            FileStream sourceFile = File.OpenRead(zipPath);
28
29            string path = filePath.Replace(Path.GetFileName(filePath), "");
30
31            if(!Directory.Exists(path))
32                Directory.CreateDirectory(path);
33
34            FileStream destinationFile = File.Create(filePath);
35            GZipStream unzip = null;
36            byte[] buffer = new byte[sourceFile.Length];
37            try
38            {
39                unzip = new GZipStream(sourceFile, CompressionMode.Decompress, true);
40                int numberOfBytes = unzip.Read(buffer, 0, buffer.Length);
41
42                destinationFile.Write(buffer, 0, numberOfBytes);
43            }

44            catch
45            {
46                throw;
47            }

48            finally
49            {
50                sourceFile.Close();
51                destinationFile.Close();
52                unzip.Close();
53            }

54        }

 

用例:

1.压缩

1            string folder = Path.Combine(Server.MapPath("~"), "TestCompress");//获取当前地址
2            string file = "file1.txt";//需要压缩的文件
3            string zip = "myzip";//保存文件名
4            //压缩
5            SampleCompress.Compress(Path.Combine(folder, file), Path.Combine(folder, zip));

2.解压

1            string folder = Path.Combine(Server.MapPath("~"), "TestCompress");
2            string file = "file1.txt";//需要压缩的文件
3            string zip = "myzip";//保存文件名
4            //解压
5            SampleCompress.Decompress(Path.Combine(folder, zip),
 Path.Combine(Path.Combine(folder, 
"zipfolder"), file));

由代码和使用例子我们可以了解到,Code1 只是支持单个文本文件的压缩/解压, 代码非常简单,但是却实际上却没什么用途,功能太少,只是让你有个初步的认识.下面介绍Code2来实现本文的主题内容.

Code2:

Code


用例:

1,压缩

1        string folder = Path.Combine(Server.MapPath("~"), "TestCompress");
2            FileInfo[] info = new FileInfo(Path.Combine(folder, "file2.ppt")), 
new
 FileInfo(Path.Combine(folder,"file3.doc")) }
;//获取两个文件FileInfo("file2.ppt","file3.doc")
3         //其中一个重载版本,压缩两个文件和一个目录("img"文件夹),保存为("myzip2.gzip")
            GZip.Compress(info, 
new string[] { Path.Combine(folder, "img") }, folder, folder, "myzip2.gzip");

2,解压

1        string folder = Path.Combine(Server.MapPath("~"), "TestCompress");
2        GZip.Decompress(folder, Path.Combine(folder,"newfolder"), "myzip2.gzip");//解压到("newfolder"文件夹)

Code2代码很长,但是却很好用,在上面用例中我对两个文件和一个文件夹压缩,压缩后保存到"myzip2.gzip"文件中,这只是其中一个重载的版本,原代码来源于http://www.vwd-cms.com/Forum/Forums.aspx?topic=18 ,可惜作者在网上给出的代码Bug很多,无法正常运行,不过作者的辛劳还是值得称赞的.修改后(Code2)已经可以很好的使用了,并增加了压缩方法的重载.支持多种多个文件/文件夹的压缩和解压还原(支持中文).下面简单介绍下工作原理:读取多文件,格式化后,按照某种规则保存到一个文件中(上面用例保存到myzip2.gzip文件中)

----------------------------------------------------

0,/file2.ppt,2009-2-5 1:52:07,9216

//.....格式化后内容...//

1,/file3.doc,2009-2-5 1:14:54,24064

//.....格式化后内容...//

2,/img/file4.gif,2009-2-3 0:53:47,729

//.....格式化后内容...//

----------------------------------------------------

在整个过程中是通过创建一个临时文件来处理,解压中也根据上面内容格式来进行.当然由于这种独特的格式,是不支持rar/zip来解压的.

最后,提供代码和例子(VS2008开发,.Net Framework 3.5(C Sharp)编写),希望你喜欢.谢谢!

以上有word 文档直接粘贴,排版可能不太好看,你可以通过下面来下载相应的代码/文档

1,文档

2,原代码

3,用例

文章为原创,如果需要引用,请保留原地址. 有什么问题/错误的地方请联系 fox7805034 (at) hotmail.com

posted @ 2009-02-05 11:30  Andy Huang  阅读(15563)  评论(12编辑  收藏  举报