I'm trying to write a process that minify's images. The process accepts single images, or a zip file.
If it's a zip file, it extracts the zip and minify's valid files.
The process returns a zip file. However, when opening in windows, it throws an error that the zip can't be opened. If I open in 7zip, it works fine.
Has anyone run into this problem before and have any suggestions:
MemoryStream outputStream = new MemoryStream();
if (ImageMinificationHelpers.ZipTypes.Contains(file.ContentType))
{
ZipArchive compressedArchive = new ZipArchive(outputStream, ZipArchiveMode.Create);
using (ZipArchive archive = new ZipArchive(file.OpenReadStream()))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
if (ImageMinificationHelpers.ValidFileExtensions.Contains(Path.GetExtension(entry.FullName)))
{
ZipArchiveEntry zipArchiveEntry = compressedArchive.CreateEntry(entry.Name);
using (Stream zipStream = zipArchiveEntry.Open())
{
MemoryStream unzippedEntryStream = await ImageMinificationHelpers.Compress(entry.Open(), width, height, quality, cancellationToken);
await unzippedEntryStream.CopyToAsync(zipStream, cancellationToken);
}
}
}
}
}
else
{
outputStream = await ImageMinificationHelpers.Compress(file.OpenReadStream(), width, height, quality, cancellationToken);
}
outputStream.Seek(0, SeekOrigin.Begin);
return File(outputStream, "application/zip", file.FileName);