C#打包文件到Zip

C#打包⽂件到Zip 鉴于许多博客讲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;
}
catchewt
{
return false;
}
}
}
///<summary>
///描述打包成Zip时的⼀个⽂件的信息
/
//</summary>
public class ZipFile
{
///<summary>
///⽂件相对路径,带⽂件名和拓展名
///</summary>
public string RelativePath { get; set; }
///<summary>
///字节数组(⽂件内容)
///</summary>
public byte[] Content { get; set; }
}
}
打包⽂件到Zip
打包时⽤的是.Net⾃带ZipArchive类,此类在System.IO.Compression.dll的程序集的System.IO.Compression命名空间下,⼀般装了VS都有,想⽤其他别的类库操作Zip的可以上Nuget ,但笔者写此⽂时⽤的最多就是这个
基本流程如下:
1、获得⽂件流(⽰例代码是⽤FileStream读取物理⽂件),并把⽂件流转成字节数组;(在操作⽂件流时,常⽤的桥接⽅式就是⽤字节数组,⾄少我们公司封装的⽅法就是如此)【如上⾯代码的GetBytesFromPath⽅法】
2、创建⼀个MemeryStream(内存流)对象,并⽤这个MemeryStream对象初始化ZipArchive对象(ZipArchive是.Net⾃带的⼀个操作Zip的类)【如上⾯代码的PackedZip⽅法】
p.s. 许多博客都⽤的物理⽂件初始化,那结果只能保存回那个物理⽂件,ZipArchive类本⾝不具备导
出流对象的⽅法;但⽤流初始化呢,当对ZipArchive进⾏修改时都会同步到流对象,有了流对象就有了字节数组,我既可以保存回物理⽂件,也可以把流传到⽹络上任何地⽅
3、⽤ZipArchive对象创建ZipArchiveEntry对象,并打开ZipArchiveEntry对象的流,往流⾥写⼊⽂件,这样就相当于往这个Zip⽂件⾥⾯装⽂件了,创建ZipArchiveEntry对象时使⽤的是相对路径,⽂件夹不需要实际存在【如上⾯代码的AddFile⽅法】
4、当装填好Zip对象之后就把之前初始化ZipArchive对象的流转成字节数组返回,⾄此就⼤功告成了,之后要导出到物理⽂件或⽹络上都随你
附录,微软官⽅的教程:

本文发布于:2024-09-22 01:41:25,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/158884.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:对象   数组   字节   物理   位置
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议