<p>I want to compress some files (into the <a href="http://en.wikipedia.org/wiki/ZIP_%28file_format%29" rel="nofollow noreferrer">ZIP</a> format) and encrypt them if possible using C#. Is there some way to do this?</p>

<p>Can encryption be done as a part of the compression itself?</p>

[{'answer_id': 71099, 'author': 'Skizz', 'author_id': 1898, 'author_profile': 'https://Stackoverflow.com/users/1898', 'pm_score': 5, 'selected': True, 'text': '<p>For compression, look at the <a href="http://msdn.microsoft.com/en-us/library/system.io.compression.aspx" rel="nofollow noreferrer"><code>System.IO.Compression</code></a> namespace and for encryption look at <a href="http://msdn.microsoft.com/en-us/library/system.security.cryptography.aspx" rel="nofollow noreferrer"><code>System.Security.Cryptography</code></a>.</p>\n'}, {'answer_id': 71101, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': -1, 'selected': False, 'text': '<p>Here is a useful topic:</p>\n\n<p><a href="https://stackoverflow.com/questions/71000/help-in-creating-zip-files-from-net-and-reading-them-from-java">Help in creating Zip files from .Net and reading them from Java</a></p>\n\n<p>System.IO.Packaging namespace gives you useful classes to compress data in zip format and <a href="http://msdn.microsoft.com/en-us/library/ms580548.aspx" rel="nofollow noreferrer">support</a> rights management.</p>\n'}, {'answer_id': 71102, 'author': 'Vinko Vrsalovic', 'author_id': 5190, 'author_profile': 'https://Stackoverflow.com/users/5190', 'pm_score': 1, 'selected': False, 'text': '<p>The <a href="http://blogs.msdn.com/bclteam/archive/2005/06/15/429542.aspx" rel="nofollow noreferrer">GZipStream</a> class is a native way to handle compression.</p>\n\n<p>As for encryption, there are <a href="http://www.codeproject.com/KB/security/SimpleEncryption.aspx" rel="nofollow noreferrer">many</a> <a href="http://www.codeproject.com/KB/security/encryption_decryption.aspx" rel="nofollow noreferrer">ways</a> to do it, most of them in the System.Security namespace. They can be done chained (encrypt a compressed stream or compress an encrypted stream).</p>\n'}, {'answer_id': 71103, 'author': 'Mark Ingram', 'author_id': 986, 'author_profile': 'https://Stackoverflow.com/users/986', 'pm_score': -1, 'selected': False, 'text': '<p>There isn\'t anything you can use directly in C#, however you can use some libraries from J# to do it for you:</p>\n\n<p><a href="http://msdn.microsoft.com/en-us/magazine/cc164129.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/magazine/cc164129.aspx</a></p>\n\n<p>Should do just what you want?</p>\n\n<p>With regards to the encryption, have a look at these links:</p>\n\n<p><a href="http://www.codeproject.com/KB/security/fileencryptdecrypt.aspx" rel="nofollow noreferrer">http://www.codeproject.com/KB/security/fileencryptdecrypt.aspx</a></p>\n\n<p><a href="http://www.obviex.com/samples/EncryptionWithSalt.aspx" rel="nofollow noreferrer">http://www.obviex.com/samples/EncryptionWithSalt.aspx</a></p>\n'}, {'answer_id': 71109, 'author': 'Hank Gay', 'author_id': 4203, 'author_profile': 'https://Stackoverflow.com/users/4203', 'pm_score': 0, 'selected': False, 'text': '<p>I\'m not sure if the steps can be combined, but .NET has good support for basic crypto. Here\'s an <a href="http://www.ondotnet.com/pub/a/dotnet/2003/02/10/dotnetcryto.html" rel="nofollow noreferrer">article on it</a>.</p>\n'}, {'answer_id': 71155, 'author': 'prakash', 'author_id': 123, 'author_profile': 'https://Stackoverflow.com/users/123', 'pm_score': 3, 'selected': False, 'text': '<p>For Zip Compression, have you seen <a href="http://www.icsharpcode.net/OpenSource/SharpZipLib/" rel="nofollow noreferrer">http://www.icsharpcode.net/OpenSource/SharpZipLib/</a></p>\n'}, {'answer_id': 71292, 'author': 'Rik', 'author_id': 5409, 'author_profile': 'https://Stackoverflow.com/users/5409', 'pm_score': 0, 'selected': False, 'text': '<p>If they cannot be combined, do compression first and then encryption. Compressing an already encrypted file will lead to poor compression ratios, because a lot of redundancy is removed.</p>\n'}, {'answer_id': 71831, 'author': 'Ian Nelson', 'author_id': 2084, 'author_profile': 'https://Stackoverflow.com/users/2084', 'pm_score': 1, 'selected': False, 'text': '<p><a href="http://www.chilkatsoft.com/" rel="nofollow noreferrer">Chilkat</a> provides .NET libraries for compression and encryption.</p>\n'}, {'answer_id': 123411, 'author': 'Martin Plante', 'author_id': 4898, 'author_profile': 'https://Stackoverflow.com/users/4898', 'pm_score': 3, 'selected': False, 'text': '<p>I know the question is already old, but I must add my two cents.</p>\n\n<p>First, some definitions:</p>\n\n<ul>\n<li><strong>Zip</strong>: Archive format for regrouping files and folders into a single file, and optionally encrypting data.</li>\n<li><strong>Deflate</strong>: One of the compression algorithms used within a Zip file to compress the data. The most popular one.</li>\n<li><strong>GZip</strong>: A single file compressed with deflate, with a small header and footer.</li>\n</ul>\n\n<p>Now, System.IO.Compression does <strong>not</strong> do Zip archiving. It does <strong>deflate</strong> and <strong>gzip</strong> compression, thus will compress a single blob of data into another single blob of data.</p>\n\n<p>So, if you\'re looking for an archive format that can group many files and folders, you need Zip libraries like:</p>\n\n<ul>\n<li><a href="http://xceed.com" rel="noreferrer">Xceed Zip</a> (it does support strong encryption)</li>\n<li><a href="http://www.icsharpcode.net/OpenSource/SharpZipLib/" rel="noreferrer">SharpZipLib</a></li>\n</ul>\n\n<p>If you only need to compress and encrypt a single blob of data, then look under <a href="http://msdn.microsoft.com/en-us/library/system.io.compression.aspx" rel="noreferrer">System.IO.Compression</a> and <a href="http://msdn.microsoft.com/en-us/library/system.security.cryptography.aspx" rel="noreferrer">System.Security.Cryptography</a>.</p>\n'}]
['https://Stackoverflow.com/questions/71077', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/184/']