解 压缩文件

来源:互联网 发布:破碎人生知乎 编辑:程序博客网 时间:2024/04/29 11:06

使用一个开源的压缩和解压缩库-----SharpZipLib

下载地址:http://www.icsharpcode.net/OpenSource/SharpZipLib/

 

在这库中,一个压缩文件对应的对象类型为ZipFile,压缩文件由多个压缩条目ZipEntry组成,压缩文件可以看成ZipEntry的集合。

 

解压缩文件:

 

 

压缩文件 :

        private void ZipFile()
        {
            //通过文件流创建对象
            System.IO.Stream zipStream = new System.IO.FileStream("demo.zip", System.IO.FileMode.Create);

            //通过文件流创建压缩的输出流
            ICSharpCode.SharpZipLib.Zip.ZipOutputStream zip =
                new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(zipStream);

            //压缩执行文件夹下面的图片
            System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(".");
            System.IO.FileInfo[] fis = di.GetFiles("*.jpg");

            foreach (System.IO.FileInfo fi in fis)
            {
                //每一个文件对应一个压缩条目ZipEntry
                ICSharpCode.SharpZipLib.Zip.ZipEntry entry =
                    new ICSharpCode.SharpZipLib.Zip.ZipEntry(fi.Name);
                entry.Size = fi.Length;

                //加入到压缩流中
                zip.PutNextEntry(entry);

                //随后加入文件的内容,取得读取文件内容的流
                System.IO.Stream input = fi.OpenRead();

                Transmite(input, zip);
                input.Close();
            }

            zip.Flush();
            zip.Close();
        }