0

Here's the GZipStream I used but the support dept wants me to use Zip instead of GZip cuz Windows doesnt support GZip and customers don't know how to open it.

Basically what I have is PDF bytes in memory and attached it to email.

So, I had to convert it to Zip but having trouble to do so. I tried ZipArchive but it require disk. I tried Deflate but got 0 bytes in the end.

So, what do you guys do to make it work?

    [Original]
    public static byte[] CompressFile(byte[] parmRawBytes)
    {
        using (var memoryStream = new MemoryStream())
        {
            using (var gzipStream = new GZipStream(memoryStream, CompressionMode.Compress))
            {
                gzipStream.Write(parmRawBytes.ToArray(), 0, parmRawBytes.ToArray().Length);
            }

            return memoryStream.ToArray();
        }
    }

    [New]
    public static byte[] CompressFile(byte[] parmRawBytes)
    {
        using (var sourceMemoryStream = new MemoryStream(parmRawBytes))
        {
            sourceMemoryStream.Position = 0;  //Reset memory-stream pointer...

            using (var targetMemoryStream = new MemoryStream())
            {
                //using (var zipArchive = new ZipArchive(memoryStream, ZipArchiveMode.Create, false))
                //{
                //    var zipArchiveEntry = zipArchive.CreateEntry("foo.pdf");
                //
                //    using (var stream = zipArchiveEntry.Open())
                //    {
                //        memoryStream.CopyTo(stream);
                //    }
                //}
                using (var deflateStream = new DeflateStream(sourceMemoryStream, CompressionMode.Compress))
                {
                    targetMemoryStream.CopyTo(deflateStream);
                }

                return targetMemoryStream.ToArray();
            }
        }
    }
fletchsod
  • 3,560
  • 7
  • 39
  • 65
  • That link use disk file saving which won't work here. – fletchsod Aug 18 '16 at 16:19
  • Only as the last step. If you don't feel like making a file, then just pick up the `MemoryStream` object. – Robert Harvey Aug 18 '16 at 16:20
  • Need to see some working example of it with `MemoryStream`. Also, no 3rd party component allowed here. – fletchsod Aug 18 '16 at 16:22
  • You're not taking the time to read that question and its answer properly. There's no 3rd party component there; it uses `System.IO.Compression` The code already works; the result is contained in the `MemoryStream` object. – Robert Harvey Aug 18 '16 at 16:25
  • Robert Harvey - Not you on the 3rd party component. The other commenter said that then he/she removed himself/herself. That commenter told me to Google DotNetZip. – fletchsod Aug 18 '16 at 16:29
  • Robert Harvey - Are you referring to the answer posted by `luci79rom`. No, then which of the several answer are you referring to? Thanks. – fletchsod Aug 18 '16 at 16:35
  • 1
    The accepted answer. – Robert Harvey Aug 18 '16 at 16:37

0 Answers0