0

Possible Duplicate:
How do I ZIP a file in C#, using no 3rd-party APIs?

I have an application which creates two files, one is txt and the other is an image file. I want to zip them together and then send it using the computer's default email setup. The reason I need to zip them because I am using mail to Link method. Here is the code:

public static void Main()
{
    SendMailWithMailTo(
        "dvader@deathstar.mil",
        "Rebel Base Detected",
        "Hoth, baybee!!",
        "\"C:\\Users\\Fett\\RebelBase[1].png\""); 
}

So I can attach only one file. So I thought zipping might be a good idea? Also I am using .NET 4.0. I believe there are some built in assemblies in .NET 4.5 but my application requirement is .NET 4.o. Also I don't want to use any outside tools if possible.

Community
  • 1
  • 1
C_sharp
  • 408
  • 5
  • 26
  • 1
    There has been support to zip a file built into .NET since version 2.0 – Security Hound Sep 11 '12 at 15:20
  • @Ramhound But I think that was just a single file, multiple files were added in 4.5 I think. Then again I might be thinking of compression streams, probably not the same. – Adam Houldsworth Sep 11 '12 at 15:20
  • looking for http://msdn.microsoft.com/en-us/library/system.io.compression%28v=vs.100%29.aspx ? – Najzero Sep 11 '12 at 15:21
  • 1
    @Ramhound: [ZipArchive](http://msdn.microsoft.com/en-us/library/system.io.compression.ziparchive.aspx) has only been recently added in 4.5. – user7116 Sep 11 '12 at 15:22
  • I seriously doubt it was limited to a single file. I am just to lasy to do the author's research for him. – Security Hound Sep 11 '12 at 15:22
  • @Ramhound I was referring to the time I used to use one of the compression streams, GZip I think, it didn't allow multiple files (that I knew of). That linked answer shows `ZipPackage` which appears to have support, but support again improved in 4.5. – Adam Houldsworth Sep 11 '12 at 15:23
  • @Ramhound: there has not been built in ZIP support, Deflate and GZip yes, but ZIP Archives no. – user7116 Sep 11 '12 at 15:23
  • @Druid ZipPackage appears to have been added in 3.0 - or at least it has docs for it. – Adam Houldsworth Sep 11 '12 at 15:33
  • @Druid: that isn't a traditional ZIP Archive, hence ZipArchive in 4.5. ZipPackage will only read ZIP archives which meet the Open Packaging Specification. – user7116 Sep 11 '12 at 15:33

3 Answers3

3

Png is already compressed so you won't benefit much by compressing it again. Why not simple use System.Net.Mail.Attachment. There is a nice example of how to do it on the link that i have provided

parapura rajkumar
  • 24,045
  • 1
  • 55
  • 85
2

.NET 4.5 includes a new ZipArchive class that allows for easy compression of multiple files.

This MSDN entry has some examples.

But as has been mentioned, some common image formats are already compressed and you likely aren't going to be saving much on text files assuming they are mediocre in size.

If you are stuck to 4.0, then the duplicate question's suggestion of ZipPackage is the one option for creating zip archives (though there appear to be caveats with what format it supports).

The compression streams in .NET will create files that can be decompressed by a zip client, but it doesn't natively support creating multi-file archives, so if ZipPackage doesn't work for you I'm guessing the answer is: no, you'll either need a third-party library or create your own implementation of the zip standard.

To me it seems easiest to not bother with the compression and add the files to the email directly, but this is a suggestion without knowing the average file size or gains from compression.

Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187
1

If you're trying to get the operating system to launch the default mail client so that the user can then build the email (which is what it appears you're after) you should consider using MapiMailMessage instead. Here is an article detailing it.

It is a third-party library because working with the Windows API's is not straight forward so there is quite a bit of code involved, but I urge you to realize there is nothing wrong with a third-party library that is open-source. In fact, here is an article with all the source code and an example of how it's used.

Why am I recommending this? Because it natively supports multiple attachments and you can stop worrying about zipping them (especially being PNG files which are already very compressed as another contributor already stated here).

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232