2

I have a scenario where by I want to zip an email attachment using SharpZipLib. Then the end user will open the attachment and will unzip the attached file.

Will the file originally zipped file using SharpZipLib be easily unzipped by other programs for my end user?

Alex Aza
  • 76,499
  • 26
  • 155
  • 134
DotNetInfo
  • 3,344
  • 6
  • 33
  • 39

1 Answers1

4

It depends on how you use SharpZipLib. There is more than one way to compress the data with this library.

Here is example of method that will create a zip file that you will be able to open in pretty much any zip aware application:

private static byte[] CreateZip(byte[] fileBytes, string fileName)
{
    using (var memoryStream = new MemoryStream())
    using (var zipStream = new ZipOutputStream(memoryStream))
    {
        var crc = new Crc32();
        crc.Reset();
        crc.Update(fileBytes);

        var zipEntry =
            new ZipEntry(fileName)
            {
                Crc = crc.Value,
                DateTime = DateTime.Now,
                Size = fileBytes.Length
            };
        zipStream.PutNextEntry(zipEntry);
        zipStream.Write(fileBytes, 0, fileBytes.Length);
        zipStream.Finish();
        zipStream.Close();
        return memoryStream.ToArray();
    }
}

Usage:

var fileBytes = File.ReadAllBytes(@"C:/1.xml");

var zipBytes = CreateZip(fileBytes, "MyFile.xml");

File.WriteAllBytes(@"C:/2.zip", zipBytes);

This CreateZip method is optimized for the cases when you already have bytes in memory and you just want to compress them and send without even saving to disk.

Alex Aza
  • 76,499
  • 26
  • 155
  • 134
  • 1
    "Deflate" is probably a misleading name, I'd assume it would produce a raw DEFLATE stream, not a zip file. Also, I stylisticly prefer taking an output stream in the implementation method, and (optionally) making an overload that returns byte[], it would let you skip the write to memory in the usage example. – Simon Buchan Jun 03 '11 at 01:51
  • @Simon Buchan - about deflate - I agree, fixed. About stream - depends on your requirements, in my case in the place where I use it I don't need to write results to file system. I just send the data over to a 3rd party web service that accepts binary array. – Alex Aza Jun 03 '11 at 02:04
  • @Alex Thanks mate. Your code def compresses the file. I am having a tricky situation, may be you can help. I am generating a csv file from csv stream (csvStream) --> compressed stream using your function results in (compressedStream) --> new Attachment(compressedStream,content-type) sends me 2mb mail (before it was 34mb), but now when i click the attached csv file, it is unreadable, all encoded. Any suggestion how to attach a csv file after zipping it – DotNetInfo Jun 03 '11 at 02:49
  • 1
    When you zip it, it is not csv file anymore. It is zip file. So it should have zip file extension. – Alex Aza Jun 03 '11 at 02:56
  • I am using your CreateZip function. I have changed my procedure a bit. I am saving a physical file name xyz.csv in a temp folder and then want to zip it up using your CreateZip function, do I need to send the whole path instead of MyFile.xml? – DotNetInfo Jun 03 '11 at 03:08
  • MyFile.xml is how you want to call your file inside of the zip. In your case it is probably xyz.csv. If you have bytes array, you don't need to save physical file, just give this byte array to the method I provided. – Alex Aza Jun 03 '11 at 03:11
  • Thanks for your help. I was simply out of my mind this morning (here and there). Simply overlooked your usage part. Your snippet def helped me alot. – DotNetInfo Jun 03 '11 at 03:30