














最近遇到个新需求,要对压缩包内的数据进行更新。因为数据量不大,没有采用解压后重新打包的方式,直接采用了内存流进行处理。代码如下:
void ModifyZip(string zipFilePath)
{
byte[] zipBytes = File.ReadAllBytes(zipFilePath);
// 读取 ZIP 文件到内存流
using (MemoryStream zipStream = new MemoryStream(zipBytes,true))
{
zipStream.Write(zipBytes, 0, zipBytes.Length);
using (ZipArchive archive = new ZipArchive(zipStream, ZipArchiveMode.Update))
{
//过滤要修改的文件
var fs = archive.Entries.Where((entry) => entry.Name.EndsWith("<customFilter>")).ToArray();
//foreach (var entry in fs)
for (int i = 0; i < fs.Length; i++)
{
var entry = fs[i];
string content;
using (StreamReader reader = new StreamReader(entry.Open()))
{
content = reader.ReadToEnd();
}
// 重新组合内容,根据自己的业务逻辑,重新拼接内容。
// 如果不是文本,需要根据对象组织byte数组
string newContent = "new";
string path = entry.FullName;
// 替换原文件内容
entry.Delete();
ZipArchiveEntry newEntry = archive.CreateEntry(path);
using (StreamWriter writer = new StreamWriter(newEntry.Open(), Encoding.UTF8))//注意数据的字符集
{
writer.Write(newContent);
}
}
}
File.WriteAllBytes(zipFilePath, zipStream.ToArray());
}
}
这里有两个个小点,需要注意。
System.NotSupportedException:“Memory stream is not expandable.”
_expandable默认为false,只有使用容量值初始化时,才会为true。所以采用了调用容量值构造,后进行写入bytes[]的方式,来加载流数据。此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。