I'm trying to add a bunch of files to a zipfile in c# but it doesn't seem to work properly.
using (var memoryStream = new MemoryStream())
{
using (var zip = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
for (int i = 0; i < kaart_data.GetLength(0); i++)
{
Image img = array[i];
var file = zip.CreateEntry(i + ".bmp");
using (var stream = new MemoryStream())
{
img.Save(stream, ImageFormat.Bmp);
using (var entryStream = file.Open())
{
stream.CopyTo(entryStream);
}
}
}
}
//saves the archive to disk
using (var fileStream = new FileStream(@"C:\Temp\test.zip", FileMode.Create))
{
memoryStream.Seek(0, SeekOrigin.Begin);
memoryStream.CopyTo(fileStream);
}
}
The thing is it does create and save the zip file into disk with the expected file size.
But when I try to open them in windows photo viewer they seem to be corrupted.
It almost took 3 hrs to figure out what's the issue. If u look at the the size of original image and extracted one, there's a tiny difference.
using (var memoryStream = new MemoryStream())
{
using (var zip = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
for (var i = 0; i < images.Length; i++)
{
var img = images[i];
var file = zip.CreateEntry(i + ".bmp");
using (var stream = new MemoryStream())
{
img.Save(stream, ImageFormat.Bmp);
using (var entryStream = file.Open())
{// to keep it as image better to have it as bytes
var bytes = stream.ToArray();
entryStream.Write(bytes, 0, bytes.Length);
}
}
}
}
using (var fileStream = new FileStream(@"test.zip", FileMode.Create))
{
memoryStream.Seek(0, SeekOrigin.Begin);
memoryStream.CopyTo(fileStream);
}
}
I've tried it it works like a charm!