<p>Does anyone know of a good way to compress or decompress files and folders in C# quickly? Handling large files might be necessary.</p>

[{'answer_id': 149, 'author': 'alumb', 'author_id': 80, 'author_profile': 'https://Stackoverflow.com/users/80', 'pm_score': 3, 'selected': False, 'text': '<p>As of .Net 1.1 the only available method is reaching into the java libraries.</p>\n\n<p><a href="https://learn.microsoft.com/en-us/archive/msdn-magazine/2003/june/using-jsharp-class-libraries-to-compress-files-and-data-with-csharp" rel="nofollow noreferrer">Using the Zip Classes in the J# Class Libraries to Compress Files and Data with C#</a></p>\n\n<p>Not sure if this has changed in recent versions.</p>\n'}, {'answer_id': 151, 'author': 'Tom Grochowicz', 'author_id': 44, 'author_profile': 'https://Stackoverflow.com/users/44', 'pm_score': 5, 'selected': False, 'text': '<p>I\'ve always used the SharpZip Library.</p>\n\n<p><a href="http://sharpdevelop.net/OpenSource/SharpZipLib/Download.aspx" rel="noreferrer">Here\'s a link</a></p>\n'}, {'answer_id': 165, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>You can use a <a href="http://sharpdevelop.net/OpenSource/SharpZipLib/" rel="nofollow noreferrer">3rd-party library such as SharpZip</a> as Tom pointed out.</p>\n\n<p>Another way (without going 3rd-party) is to use the Windows Shell API. You\'ll need to set a reference to the Microsoft Shell Controls and Automation COM library in your C# project. Gerald Gibson has an example at:</p>\n\n<p><a href="http://web.archive.org/web/20130724085455/http://geraldgibson.net/dnn/Home/CZipFileCompression/tabid/148/Default.aspx" rel="nofollow noreferrer">Internet Archive\'s copy of the dead page</a></p>\n'}, {'answer_id': 1716, 'author': 'shsteimer', 'author_id': 292, 'author_profile': 'https://Stackoverflow.com/users/292', 'pm_score': 3, 'selected': False, 'text': '<p>This is very easy to do in java, and as stated above you can reach into the java.util.zip libraries from C#.  For references see:</p>\n\n<p><a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/zip/package-summary.html" rel="noreferrer">java.util.zip javadocs</a>\n<br>\n<a href="http://www.devx.com/getHelpOn/10MinuteSolution/20447" rel="noreferrer">sample code</a></p>\n\n<p>I used this a while ago to do a deep (recursive) zip of a folder structure, but I don\'t think I ever used the unzipping.  If I\'m so motivated I may pull that code out and edit it into here later.</p>\n'}, {'answer_id': 2142, 'author': 'Dave Anderson', 'author_id': 371, 'author_profile': 'https://Stackoverflow.com/users/371', 'pm_score': 5, 'selected': False, 'text': '<p>The .Net 2.0 framework namespace <code>System.IO.Compression</code> supports GZip and Deflate algorithms. Here are two methods that compress and decompress a byte stream which you can get from your file object. You can substitute <code>GZipStream</code> for <code>DefaultStream</code> in the methods below to use that algorithm. This still leaves the problem of handling files compressed with different algorithms though.</p>\n\n<pre><code>public static byte[] Compress(byte[] data)\n{\n    MemoryStream output = new MemoryStream();\n\n    GZipStream gzip = new GZipStream(output, CompressionMode.Compress, true);\n    gzip.Write(data, 0, data.Length);\n    gzip.Close();\n\n    return output.ToArray();\n}\n\npublic static byte[] Decompress(byte[] data)\n{\n    MemoryStream input = new MemoryStream();\n    input.Write(data, 0, data.Length);\n    input.Position = 0;\n\n    GZipStream gzip = new GZipStream(input, CompressionMode.Decompress, true);\n\n    MemoryStream output = new MemoryStream();\n\n    byte[] buff = new byte[64];\n    int read = -1;\n\n    read = gzip.Read(buff, 0, buff.Length);\n\n    while (read &gt; 0)\n    {\n        output.Write(buff, 0, read);\n        read = gzip.Read(buff, 0, buff.Length);\n    }\n\n    gzip.Close();\n\n    return output.ToArray();\n}\n</code></pre>\n'}, {'answer_id': 8438622, 'author': 'Rok', 'author_id': 634954, 'author_profile': 'https://Stackoverflow.com/users/634954', 'pm_score': 3, 'selected': False, 'text': '<p>Another good alternative is also <a href="http://dotnetzip.codeplex.com/" rel="noreferrer">DotNetZip</a>.</p>\n'}, {'answer_id': 10568788, 'author': 'Darryl', 'author_id': 1391700, 'author_profile': 'https://Stackoverflow.com/users/1391700', 'pm_score': 3, 'selected': False, 'text': '<p><a href="http://www.dotnetperls.com/gzipstream" rel="noreferrer">GZipStream</a> is a really good utility to use.</p>\n'}, {'answer_id': 20032943, 'author': 'Amit Joki', 'author_id': 3001736, 'author_profile': 'https://Stackoverflow.com/users/3001736', 'pm_score': 3, 'selected': False, 'text': '<p>My answer would be close your eyes and opt for <a href="http://dotnetzip.codeplex.com/" rel="noreferrer">DotNetZip</a>. It\'s been tested by a large community.</p>\n'}, {'answer_id': 48174631, 'author': 'mohsen_1687', 'author_id': 3311484, 'author_profile': 'https://Stackoverflow.com/users/3311484', 'pm_score': 0, 'selected': False, 'text': '<p>You can create zip file with this method:</p>\n\n<pre class="lang-cs prettyprint-override"><code>public async Task&lt;string&gt; CreateZipFile(string sourceDirectoryPath, string name)\n{\n    var path = HostingEnvironment.MapPath(TempPath) + name;\n    await Task.Run(() =&gt;\n    {\n        if (File.Exists(path)) File.Delete(path);\n        ZipFile.CreateFromDirectory(sourceDirectoryPath, path);\n    });\n    return path;\n}\n</code></pre>\n\n<p>and then you can unzip zip file with this methods:</p>\n\n<p>1- This method work with zip file path</p>\n\n<pre class="lang-cs prettyprint-override"><code>public async Task ExtractZipFile(string filePath, string destinationDirectoryName)\n{\n    await Task.Run(() =&gt;\n    {\n        var archive = ZipFile.Open(filePath, ZipArchiveMode.Read);\n        foreach (var entry in archive.Entries)\n        {\n            entry.ExtractToFile(Path.Combine(destinationDirectoryName, entry.FullName), true);\n        }\n        archive.Dispose();\n    });\n}\n</code></pre>\n\n<p>2- This method work with zip file stream</p>\n\n<pre class="lang-cs prettyprint-override"><code>public async Task ExtractZipFile(Stream zipFile, string destinationDirectoryName)\n{\n    string filePath = HostingEnvironment.MapPath(TempPath) + Utility.GetRandomNumber(1, int.MaxValue);\n    using (FileStream output = new FileStream(filePath, FileMode.Create))\n    {\n        await zipFile.CopyToAsync(output);\n    }\n    await Task.Run(() =&gt; ZipFile.ExtractToDirectory(filePath, destinationDirectoryName));\n    await Task.Run(() =&gt; File.Delete(filePath));\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/145', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/87/']