C# 多文件压缩

一、.zip压缩格式(System.IO.Compression.ZipArchive)

1.压缩文件

List<(MemoryStream, string)> ZipList = new List<(MemoryStream, string)>();
ZipList.Add((new MemoryStream(bytes_1), fileName1));
ZipList.Add((new MemoryStream(bytes_2), fileName2));
//压缩文件
MemoryStream zipStream = new MemoryStream();
System.IO.Compression.ZipArchive zipArchive = new System.IO.Compression.ZipArchive(zipStream, System.IO.Compression.ZipArchiveMode.Create, true);
foreach (var ms in ZipList)
{
    System.IO.Compression.ZipArchiveEntry entry = zipArchive.CreateEntry(ms.Item2);
    using (Stream entryStream = entry.Open())
    {
        ms.Item1.Seek(0, SeekOrigin.Begin);
        ms.Item1.CopyTo(entryStream);
    }
}

2.可能遇到的问题-"解压提示文件末端错误"

解决方法:

 zipArchive.Dispose();//释放资源
 zipStream.Seek(0, SeekOrigin.Begin); // 重置zipStream的位置
 zipStream.Flush();//确保数据写入 清空缓冲区

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值