鉴于许多博客讲C#打包Zip的都写的相对啰嗦且没有注释,不适合小白观看,特写此篇讲述一下

先贴代码,直接创建一个.Net Core控制台就能运行

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;

namespace Zip
{
    class Program
    {
        static void Main(string[] args)
        {
            var zipFiles = new List<ZipFile>
            {
                new ZipFile
                {
                    RelativePath = "1/2/3/公文签批单223.pdf",
                    Content = Extend.GetBytesFromPath("E:/公文签批单.pdf")
                },
                new ZipFile
                {
                    RelativePath = "1/2/3/公文签批单2233.pdf", //相同路径下文件名重名时,解压时会提示是否覆盖
                    Content = Extend.GetBytesFromPath("E:/公文签批单.pdf")
                }
            };

            var zipBytes = zipFiles.PackedZip();
            zipBytes.WriteToFile("E:/222/Zip测试.zip");
            Console.WriteLine("打包Zip成功");
            Console.ReadKey();
        }
    }

    public static class Extend
    {
        /// <summary>
        /// 将流转成字节数组
        /// </summary>
        /// <param name="stream">文件流</param>
        /// <returns></returns>
        public static byte[] ToBytes(this Stream stream)
        {
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            // 设置当前流的位置为流的开始
            stream.Seek(0, SeekOrigin.Begin);
            return bytes;
        }

        /// <summary>
        /// 获取文件的内容(字节数组)
        /// </summary>
        /// <param name="absolutePath">绝对路径</param>
        /// <returns></returns>
        public static byte[] GetBytesFromPath(string absolutePath)
        {
            using (Stream fileStream = new FileStream(absolutePath, FileMode.OpenOrCreate))
            {
                return fileStream.ToBytes();
            }
        }

        /// <summary>
        /// 字节数组写入文件
        /// </summary>
        /// <param name="bytes">文件内容</param>
        /// <param name="absolutePath">绝对路径</param>
        public static void WriteToFile(this byte[] bytes, string absolutePath)
        {
            if (File.Exists(absolutePath))
            {
                File.Delete(absolutePath);
            }

            using (var fs = new FileStream(absolutePath, FileMode.Create))
            {
                fs.Write(bytes, 0, bytes.Length);
            }
        }

        /// <summary>
        /// 将Zip文件描述对象打包成Zip文件,并返回字节数组
        /// </summary>
        /// <param name="zipFiles">Zip文件描述对象数组</param>
        /// <returns></returns>
        public static byte[] PackedZip(this List<ZipFile> zipFiles)
        {
            using (Stream memoryStream = new MemoryStream())
            {
                using (var zipArchive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
                {
                    foreach (var zipFile in zipFiles)
                    {
                        zipArchive.AddFile(zipFile.RelativePath, zipFile.Content);
                    }
                }

                memoryStream.Seek(0, SeekOrigin.Begin); //这句要在using ZipArchive外面,否则压缩文件会被损坏
                return memoryStream.ToBytes();
            }
        }

        /// <summary>
        /// 向Zip文件中添加文件
        /// </summary>
        /// <param name="zipArchive"></param>
        /// <param name="relativePath">相对路径</param>
        /// <param name="bytes">文件内容</param>
        /// <returns></returns>
        private static bool AddFile(this ZipArchive zipArchive, string relativePath, byte[] bytes)
        {
            try
            {
                ZipArchiveEntry entry = zipArchive.CreateEntry(relativePath);
                using (Stream entryStream = entry.Open())
                {
                    entryStream.Write(bytes, 0, bytes.Length);
                }

                return true;
            }
            catch
            {
                return false;
            }
        }
    }

    /// <summary>
    /// 描述打包成Zip时的一个文件的信息
    /// </summary>
    public class ZipFile
    {
        /// <summary>
        /// 文件相对路径,带文件名和拓展名
        /// </summary>
        public string RelativePath { get; set; }
        /// <summary>
        /// 字节数组(文件内容)
        /// </summary>
        public byte[] Content { get; set; }
    }
}
打包文件到Zip

相关文章:

  • 2022-02-24
  • 2021-12-27
  • 2021-07-07
  • 2022-12-23
  • 2021-04-02
  • 2022-02-01
  • 2022-12-23
猜你喜欢
  • 2021-11-01
  • 2022-02-03
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-26
  • 2022-12-23
相关资源
相似解决方案