we have two methods to zip and unzip as below, but when we unzip and file we’re getting proper results, but if we zip after unzip and later unzip we’re not getting same number of files.
in our example, zip file => unzipped => 25 files => zipped => unzipped (contains only 10 files).
//Decompress Files
public static List<(string fileName, byte[] fileBytes)> DecompressZipFile(Stream zipFileStream)
{
var filesInZip = new List<(string fileName, byte[] fileBytes)>();
using (Archive archive = new Archive(zipFileStream))
{
MemoryStream ms;
foreach (var file in archive.Entries)
{
try
{
ms = new MemoryStream();
file.Open().CopyTo(ms);
filesInZip.Add((file.Name, ms.ToArray()));
}
catch(Exception x)
{
Console.WriteLine(x.StackTrace);
throw;
}
}
}
return filesInZip;
}
//Compressing Multiple Files
public static MemoryStream CompressFiles(List<(string fileName, byte[] fileBytes)> filesToZip, string archiveComment)
{
var zipFileStream = new MemoryStream();
using (var archive = new Archive())
{
foreach (var file in filesToZip)
{
archive.CreateEntry(file.fileName, new MemoryStream(file.fileBytes));
}
archive.Save(zipFileStream, new ArchiveSaveOptions() { Encoding = Encoding.UTF8, ArchiveComment = archiveComment });
}
zipFileStream.Position = 0;
return zipFileStream;
}