i have 2 zip files (zip1 and zip2), i need merge this files in one, how can i solve it? I understand that I can fix it decompressing ZIP1 to a temp folder and then add it to zip2, but i think it is inefficient, what is the faster method?
I'm using System.IO.Compression
library...
I use this code to explore Zip2:
using (ZipArchive archive = ZipFile.OpenRead(Zip2))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
/*...*/
}
}
PD/: The Zip1 and Zip2 files have FILES, FOLDERS AND SUBFOLDERS
I haven't really tested this, but you can give it a try:
public ZipArchive Merge(List<ZipArchive> archives)
{
if (archives == null) throw new ArgumentNullException("archives");
if (archives.Count == 1) return archives.Single();
using (var memoryStream = new MemoryStream())
{
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
foreach (var zipArchive in archives)
{
foreach (var zipArchiveEntry in zipArchive.Entries)
{
var file = archive.CreateEntry(zipArchiveEntry.FullName);
using (var entryStream = file.Open()) {
using (var streamWriter = new StreamWriter(entryStream)) { streamWriter.Write(zipArchiveEntry.Open()); }
}
}
}
return archive;
}
}
}