如何用C#实现压缩文件
目录
- 一、单文件压缩
- 二、多文件压缩
- 三、多文件异步压缩
- 四、压缩文件夹
一、单文件压缩
场景,文件可能比较大,需要压缩传输,比如上传和下载
/// <summary> /// 单文件压缩 /// </summary> /// <param name="sourceFile">源文件</param> /// <param name="zipedFile">zip压缩文件</param> /// <param name="blockSize">缓冲区大小</param> /// <param name="compressionLevel">压缩级别</param> public static void ZipFile(string sourceFile, string zipedFile, int blockSize = 1024, int compressionLevel = 6) { if (!File.Exists(sourceFile)) { throw new System.IO.FileNotFoundException("The specified file " + sourceFile + " could not be found."); } var fileName = System.IO.Path.GetFileNameWithoutExtension(sourceFile); FileStream streamToZip = new FileStream(sourceFile, FileMode.Open, FileAccess.Read); FileStream zipFile = File.Create(zipedFile); ZipOutputStream zipStream = new ZipOutputStream(zipFile); ZipEntry zipEntry = new ZipEntry(fileName); zipStream.PutNextEntry(zipEntry); //存储、最快、较快、标准、较好、最好 0-9 zipStream.SetLevel(compressionLevel); byte[] buffer = new byte[blockSize]; int size = streamToZip.Read(buffer, 0, buffer.Length); zipStream.Write(buffer, 0, size); try { while (size < streamToZip.Length) { int sizeRead = streamToZip.Read(buffer, 0, buffer.Length); zipStream.Write(buffer, 0, sizeRead); size += sizeRead; } } catch (Exception ex) { throw ex; } zipStream.Finish(); zipStream.Close(); streamToZip.Close(); }
说明:26行,blocksize为缓存区大小,不能设置太大,如果太大也会报异常。26-38行,把文件通过FileStream流,读取到缓冲区中,再写入到ZipOutputStream流。你可以想象,两个管道,一个读,另一个写,中间是缓冲区,它们的工作方式是同步的方式。想一下,能不能以异步的方式工作,读的管道只管读,写的管道只管写?如果是这样一个场景,读的特别快,写的比较慢,比如,不是本地写,而是要经过网络传输,就可以考虑异步的方式。怎么做,读者可以自行改造。关键一点,流是有顺序的,所以要保证顺序的正确性即可。
二、多文件压缩
这种场景也是比较多见,和单文件压缩类似,无非就是多循环几次。
/// <summary> /// 多文件压缩 /// </summary> /// <param name="zipfile">zip压缩文件</param> /// <param name="filenames">源文件集合</param> /// <param name="password">压缩加密</param> public void ZipFiles(string zipfile, string[] filenames, string password = "") { ZipOutputStream s = new ZipOutputStream(System.IO.File.Create(zipfile)); s.SetLevel(6); if (password != "") s.Password = Md5Help.Encrypt(password); foreach (string file in filenames) { //打开压缩文件 FileStream fs = File.OpenRead(file); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); var name = Path.GetFileName(file); ZipEntry entry = new ZipEntry(name); entry.DateTime = DateTime.Now; entry.Size = fs.Length; fs.Close(); s.PutNextEntry(entry); s.Write(buffer, 0, buffer.Length); } s.Finish(); s.Close(); }
说明:21行,缓冲区大小直接为文件大小,所以一次读完,没有循环读写。这种情况下,单个文件不能太大,比如超过1G。14行,可以为压缩包设置密码,MD5的生成方法如下:
public class Md5Help { /// <summary> ///32位 MD5加密 /// </summary> /// <param name="str">加密字符</param> /// <returns></returns> public static string Encrypt(string str) { MD5 md5 = new MD5CryptoServiceProvider(); byte[] encryptdata = md5.ComputeHash(Encoding.UTF8.GetBytes(str)); return Convert.ToBase64String(encryptdata); } }
三、多文件异步压缩
上面同步的压缩的前提是,假设文件不大,而且文件数不多,但是现实是,不光文件大,而且文件数比较多。这种情况,就要考虑异步方法了。否则会阻塞主线程,就是我们平常说的卡死
/// <summary> /// 异步压缩文件为zip压缩包 /// </summary> /// <param name="zipfile">压缩包存储路径</param> /// <param name="filenames">文件集合</param> public static async void ZipFilesAsync(string zipfile, string[] filenames) { await Task.Run(() => { ZipOutputStream s = null; try { s = new ZipOutputStream(System.IO.File.Create(zipfile)); s.SetLevel(6); // 0 - store only to 9 - means best compression foreach (string file in filenames) { //打开压缩文件 FileStream fs = System.IO.File.OpenRead(file); var name = Path.GetFileName(file); ZipEntry entry = new ZipEntry(name); entry.DateTime = DateTime.Now; entry.Size = fs.Length; s.PutNextEntry(entry); //如果文件大于1G long blockSize = 51200; var size = (int)fs.Length; var oneG = 1024 * 1024 * 1024; if (size > oneG) { blockSize = oneG; } byte[] buffer = new byte[blockSize]; size = fs.Read(buffer, 0, buffer.Length); s.Write(buffer, 0, size); while (size < fs.Length) { int sizeRead = fs.Read(buffer, 0, buffer.Length); s.Write(buffer, 0, sizeRead); size += sizeRead; } s.Flush(); fs.Close(); } } 【转自:http://www.1234xp.com/xggf.html 欢迎转载】 catch (Exception ex) { Console.WriteLine("异步压缩文件出错:" + ex.Message); } finally { s?.Finish(); s?.Close(); } }); }
四、压缩文件夹
实际的应用当中,是文件和文件夹一起压缩,所以这种情况,就干脆把要压缩的东西全部放到一个文件夹,然后进行压缩。
主方法如下:
/// <summary> /// 异步压缩文件夹为zip压缩包 /// </summary> /// <param name="zipfile">压缩包存储路径</param> /// <param name="sourceFolder">压缩包存储路径</param> /// <param name="filenames">文件集合</param> public static async void ZipFolderAsync(string zipfile, string sourceFolder, string[] filenames) { await Task.Run(() => { ZipOutputStream s = null; try { s = new ZipOutputStream(System.IO.File.Create(zipfile)); s.SetLevel(6); // 0 - store only to 9 - means best compression CompressFolder(sourceFolder, s, sourceFolder); } catch (Exception ex) { Console.WriteLine("异步压缩文件出错:" + ex.Message); } finally { s?.Finish(); s?.Close(); } }); }
压缩的核心方法:
/// <summary> /// 压缩文件夹 /// </summary> /// <param name="source">源目录</param> /// <param name="s">ZipOutputStream对象</param> /// <param name="parentPath">和source相同</param> public static void CompressFolder(string source, ZipOutputStream s, string parentPath) { string[] filenames = Directory.GetFileSystemEntries(source); foreach (string file in filenames) { if (Directory.Exists(file)) { CompressFolder(file, s, parentPath); //递归压缩子文件夹 } else { using (FileStream fs = System.IO.File.OpenRead(file)) { var writeFilePath = file.Replace(parentPath, ""); ZipEntry entry = new ZipEntry(writeFilePath); entry.DateTime = DateTime.Now; entry.Size = fs.Length; s.PutNextEntry(entry); //如果文件大于1G long blockSize = 51200; var size = (int)fs.Length; var oneG = 1024 * 1024 * 1024; if (size > oneG) { blockSize = oneG; } byte[] buffer = new byte[blockSize]; size = fs.Read(buffer, 0, buffer.Length); s.Write(buffer, 0, size); while (size < fs.Length) { int sizeRead = fs.Read(buffer, 0, buffer.Length); s.Write(buffer, 0, sizeRead); size += sizeRead; } s.Flush(); //清除流的缓冲区,使得所有缓冲数据都写入到文件中 fs.Close(); } } } }
唯一需要注意的地方,可能解压出来的目录结构和压缩前的文件目录不同,这时候检查parentPath参数,它在ZipEntry实体new的时候用,替换绝对路径为当前的相对路径,也就是相对压缩文件夹的路径。
上面的方法比较复杂,还有一种相对简单的方式,直接调用api:
public static string ZipFolder(string sourceFolder, string zipFile) { string result = ""; try { //创建压缩包 if (!Directory.Exists(sourceFolder)) return result = "压缩文件夹不存在"; DirectoryInfo d = new DirectoryInfo(sourceFolder); var files = d.GetFiles(); if (files.Length == 0) { //找子目录 var ds = d.GetDirectories(); if (ds.Length > 0) { files = ds[0].GetFiles(); } } if (files.Length == 0) return result = "待压缩文件为空"; System.IO.Compression.ZipFile.CreateFromDirectory(sourceFolder, zipFile); } catch (Exception ex) { result += "压缩出错:" + ex.Message; } return result; }
以上就是如何用C#实现压缩文件的详细内容,更多关于C#压缩文件的资料请关注海外IDC网其它相关文章!