3

I want to compress some files (into the ZIP format) and encrypt them if possible using C#. Is there some way to do this?

Can encryption be done as a part of the compression itself?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Niyaz
  • 53,943
  • 55
  • 151
  • 182

9 Answers9

12

For compression, look at the System.IO.Compression namespace and for encryption look at System.Security.Cryptography.

Skizz
  • 69,698
  • 10
  • 71
  • 108
  • NOTE: System.IO.Compression is great for compressing individual files. It does not compress multiple source files into a single zip 'archive.' – Scott Fletcher Nov 07 '08 at 15:37
  • 2
    I have been using ZipFile.CreateFromDirectory in System.IO.Compression (.Net 4.6) and it creates a single zip archive from the files in the specified directory. – AH. Jul 09 '15 at 05:38
7

For Zip Compression, have you seen http://www.icsharpcode.net/OpenSource/SharpZipLib/

prakash
  • 58,901
  • 25
  • 93
  • 115
6

I know the question is already old, but I must add my two cents.

First, some definitions:

  • Zip: Archive format for regrouping files and folders into a single file, and optionally encrypting data.
  • Deflate: One of the compression algorithms used within a Zip file to compress the data. The most popular one.
  • GZip: A single file compressed with deflate, with a small header and footer.

Now, System.IO.Compression does not do Zip archiving. It does deflate and gzip compression, thus will compress a single blob of data into another single blob of data.

So, if you're looking for an archive format that can group many files and folders, you need Zip libraries like:

If you only need to compress and encrypt a single blob of data, then look under System.IO.Compression and System.Security.Cryptography.

Martin Plante
  • 4,553
  • 3
  • 33
  • 45
1

The GZipStream class is a native way to handle compression.

As for encryption, there are many ways 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).

Vinko Vrsalovic
  • 330,807
  • 53
  • 334
  • 373
1

Chilkat provides .NET libraries for compression and encryption.

Ian Nelson
  • 57,123
  • 20
  • 76
  • 103
0

I'm not sure if the steps can be combined, but .NET has good support for basic crypto. Here's an article on it.

Hank Gay
  • 70,339
  • 36
  • 160
  • 222
0

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.

Rik
  • 28,507
  • 14
  • 48
  • 67
-1

Here is a useful topic:

Help in creating Zip files from .Net and reading them from Java

System.IO.Packaging namespace gives you useful classes to compress data in zip format and support rights management.

Community
  • 1
  • 1
aku
  • 122,288
  • 32
  • 173
  • 203
-1

There isn't anything you can use directly in C#, however you can use some libraries from J# to do it for you:

http://msdn.microsoft.com/en-us/magazine/cc164129.aspx

Should do just what you want?

With regards to the encryption, have a look at these links:

http://www.codeproject.com/KB/security/fileencryptdecrypt.aspx

http://www.obviex.com/samples/EncryptionWithSalt.aspx

Mark Ingram
  • 71,849
  • 51
  • 176
  • 230