Answer the question
In order to leave comments, you need to log in
How to change a file inside a zip archive?
I have many archives, each with three files. Two of them need to be changed.
// Открываю архив
using (ZipArchive archive = new ZipArchive(ms, ZipArchiveMode.Update))
{
// idxs - memoryStream указывающие на файлы которые надо изменить
foreach (var idx in idxs)
{ //десериализация xml
var msIndex = Deserialize.SerializeObjAlt(idx.Value);
// удаляем старый файл
ZipArchiveEntry Entry0 = archive.GetEntry(idx.Key);
Entry0.Delete();
// создаем новый файл
ZipArchiveEntry Entry1 = archive.CreateEntry(idx.Key);
using (var writer = Entry1.Open())
{
msIndex.Position = 0;
msIndex.CopyTo(writer);
}
}
}
Answer the question
In order to leave comments, you need to log in
Just add Flush
using (var writer = Entry1.Open())
{
msIndex.Position = 0;
msIndex.CopyTo(writer);
writer.Flush();
}
1. Create a temporary directory
2. Unpack files into it
3. Modify files
4. Pack the directory into an archive
5. Delete the temporary directory
In the System.IO.Compression.FileSystem.dll namespace, there is a convenient ZipFile class that can both pack and unpack directories.
ZipFile.CreateFromDirectory(tmp, zipPath);
ZipFile.ExtractToDirectory(zipFile, targetFolder);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question