一佳一

记录像1+1一样简洁的代码

导航

C# zip压缩

Posted on 2012-12-24 10:49  一佳一  阅读(11068)  评论(1编辑  收藏  举报

网上有集中压缩zip的方式,但是还是发现DotNetZip比较通用,使用也很简单

1.找到\DotNetZipLib-DevKit-v1.9\zip-v1.9\Release\Ionic.Zip.dll

 

2.添加引用

 

3.代码:

 /// <summary>
    /// 压缩zip
    /// </summary>
    /// <param name="fileToZips">文件路径集合</param>
    /// <param name="zipedFile">想要压成zip的文件名</param>
    public void Zip(string[] fileToZips, string zipedFile)
    {
        using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile(zipedFile, Encoding.Default))
        {
            foreach (string fileToZip in fileToZips)
            {
                using (FileStream fs = new FileStream(fileToZip, FileMode.Open, FileAccess.ReadWrite))
                {
                    byte[] buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, buffer.Length);
                    string fileName = fileToZip.Substring(fileToZip.LastIndexOf("\\") + 1);
                    zip.AddEntry(fileName,buffer);
                }
            }
            zip.Save();
        }
    }

 

解压的话

using (ZipFile zip = ZipFile.Read("test.zip")) 
            { 
                foreach (ZipEntry z in zip) 
                { 
                    z.Extract(@"F:\kk"); 
                } 
            } 

 

dll在这里https://files.cnblogs.com/linyijia/DotNetZipLib-DevKit-v1.9.zip

OK打完收工