0

Is DeflateStream supposed to create archived stream that can be stored as standard .zip archive?

I'm trying to create in-memory zip (to be sent remotely) from a local file. I used a DeflateStream to get a compressed byte array from the file on local disk:

public static byte[] ZipFile(string csvFullPath)
    {
        using (FileStream csvStream = File.Open(csvFullPath, FileMode.Open, FileAccess.Read))
        {
            using (MemoryStream compressStream = new MemoryStream())
            {
                using (DeflateStream deflateStream = new DeflateStream(compressStream, CompressionLevel.Optimal))
                {
                    csvStream.CopyTo(deflateStream);
                    deflateStream.Close();
                    return compressStream.ToArray();
                }
            }
        }
    }

This works great. However when I dump the resulting bytes to a zip file:

byte[] zippedBytes = ZipFile(FileName);
File.WriteAllBytes("Sample.zip", zippedBytes);

I cannot open the resulting .zip archive with windows build-in .zip functionality (or with any other 3rd party archive tool).

An alternative I'm planning now is using ZipArchive - however that would require creating temporary files on disk (first copy the file into separate directory, then zip it, then read it into byte array and then delete it)

Jan
  • 1,905
  • 17
  • 41

1 Answers1

2

You can use this nice library https://dotnetzip.codeplex.com/

or you can use ZipArchive and it works with MemoryStream pretty good:

public static byte[] ZipFile(string csvFullPath)
{
    using (FileStream csvStream = File.Open(csvFullPath, FileMode.Open, FileAccess.Read))
    {
        using (MemoryStream zipToCreate = new MemoryStream())
        {
            using (ZipArchive archive = new ZipArchive(zipToCreate, ZipArchiveMode.Create, true))
            {
                ZipArchiveEntry fileEntry = archive.CreateEntry(Path.GetFileName(csvFullPath));
                using (var entryStream = fileEntry.Open())
                {
                    csvStream.CopyTo(entryStream);
                }
            }

            return zipToCreate.ToArray();
        }
    }
}
Jan
  • 1,905
  • 17
  • 41
Sergey L
  • 1,402
  • 1
  • 9
  • 11
  • The code works fine, but if I dump the bytes into .zip file, then It cannot be opened (allegedly corrupted archive). Btw. the binary content of that file is almost identical to file created with example in question – Jan May 25 '16 at 18:26
  • 1
    your code brought me to correct direction - but it actually needs to dispose the ZipArchive before using the memory stream (plus leaveOpen needs to be set ot true). Credits belong to this answer: http://stackoverflow.com/questions/12347775/ziparchive-creates-invalid-zip-file/12350106#12350106 – Jan May 25 '16 at 18:46