{"id":228,"date":"2012-05-21T13:31:59","date_gmt":"2012-05-21T05:31:59","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=228"},"modified":"2018-09-03T22:09:48","modified_gmt":"2018-09-03T14:09:48","slug":"how-to-compress-and-decompress-files-in-c","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-compress-and-decompress-files-in-c\/","title":{"rendered":"How to compress and decompress files in C#"},"content":{"rendered":"<p>Files are useful for applications to communicate with others. For instance, web applications can communicate with Google's crawlers via sitemaps on the Internet. A web application write urls to its pages to its sitemap. When Google's crawlers come to this web applications, they will download these sitemaps to index urls to the pages of .<\/p>\n<p>To reduce the time needed for files to be trasmitted over the network, it is a good idea for applications to compress files before sending them out to the network, or for a web application, making them available for <a href=\"http:\/\/www.techcoil.com\/blog\/quick-references\/downloading-a-file-from-via-http-post-and-http-get-in-c\/\" title=\"Downloading a file via HTTP post and HTTP get in C#\" target=\"_blank\">download via HTTP<\/a>.<\/p>\n<p>In this post, I document how we can compress and decompress files via the GZip implementation in C# - <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.io.compression.gzipstream.aspx\" title=\"MSDN reference to System.IO.Compression.GZipstream\" target=\"_blank\"><code>System.IO.Compression.GZipStream<\/code><\/a>.<\/p>\n<p><code>GZipStream<\/code> is a wrapper which wraps around other derivatives of the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.io.stream.aspx\" title=\"MSDN reference for System.IO.Stream\" target=\"_blank\"><code>System.IO.Stream<\/code><\/a> to provide for data compression and decompression. It runs in one of two modes which is specified by one of <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.io.compression.compressionmode.aspx\" title=\"MSDN reference for System.IO.Compression.CompressionMode enumeration\" target=\"_blank\"><code>System.IO.Compression.CompressionMode<\/code><\/a> enumeration members:<\/p>\n<ul>\n<li><strong><code>CompressionMode.Compress<\/code><\/strong> - Configures the <code>GZipStream<\/code> instance to compress data. When a <code>GZipStream<\/code> instance is being configured to compress data, we will <a href=\"http:\/\/www.techcoil.com\/blog\/quick-references\/how-to-read-from-file-in-c\/\" title=\"How to read from file in C#\" target=\"_blank\">read from a file<\/a> and write the contents of the file to the GZipStream instance.<\/li>\n<li><strong><code>CompressionMode.Decompress<\/code><\/strong> - Configures the <code>GZipStream<\/code> instance to decompress data. When a <code>GZipStream<\/code> instance is being configured to decompress data, we will read data from it and <a href=\"http:\/\/www.techcoil.com\/blog\/quick-references\/how-to-write-to-file-in-c\/\" title=\"How to write to file in C#\" target=\"_blank\">write the data to another file<\/a>.<\/li>\n<\/ul>\n<h3>Compressing a file using GZipStream<\/h3>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nFileStream sourceFileStream = File.OpenRead(&quot;sitemap.xml&quot;);\r\nFileStream destFileStream = File.Create(&quot;sitemap.xml.gz&quot;);\r\n\r\nGZipStream compressingStream = new GZipStream(destFileStream, \r\n    CompressionMode.Compress);\r\n\r\nbyte&#x5B;] bytes = new byte&#x5B;2048];\r\nint bytesRead;\r\nwhile ((bytesRead = sourceFileStream.Read(bytes, 0, bytes.Length)) != 0)\r\n{\r\n    compressingStream.Write(bytes, 0, bytesRead);\r\n}\r\n\r\nsourceFileStream.Close();\r\ncompressingStream.Close();\r\ndestFileStream.Close();\r\n<\/pre>\n<p>The code segment begins by first gaining read access to <code>sitemap.xml<\/code>, via <code>sourceFileStream<\/code>, which we are going to compress. After that, we gain write access to <code>sitemap.xml.gz<\/code>, via <code>destFileStream<\/code>, which we are going to have our <code>GZipStream<\/code> instance write the compressed data into.<\/p>\n<p>When we had gained access to the relevant files, we create a <code>GZipStream<\/code> instance by passing in <code>destFileStream<\/code> and the <code>CompressionMode.Compress<\/code> enumeration option into <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/as1ff51s.aspx\" title=\"MSDN reference for System.IO.Compression.GZipStream Constructor (Stream, CompressionMode)\" target=\"_blank\">one of its constructors<\/a>. We set this instance to <code>compressingStream<\/code>. <\/p>\n<p>Once we have the necessary <code>Stream<\/code> objects, we begin to compress <code>sitemap.xml<\/code>. To do that, we simply read from <code>sourceFileStream<\/code> and write its contents to <code>compressingStream<\/code>. Note that we are writing 2048 bytes at a time to <code>compressingStream<\/code> instead of writing byte by byte. This is because if we <strong>write byte by byte<\/strong> to <code>compressingStream<\/code>, <strong>our <code>GZipStream<\/code> instance won't be able to compress our file well<\/strong>.<\/p>\n<p>When we are done with compressing <code>sitemap.xml<\/code>, we relinquish accesses to <code>sourceFileStream<\/code>, <code>destFileStream<\/code> and <code>compressingStream<\/code>.<\/p>\n<h3>Decompressing a file using GZipStream<\/h3>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nFileStream sourceFileStream = File.OpenRead(&quot;sitemap.xml.gz&quot;);\r\nFileStream destFileStream = File.Create(&quot;sitemap.xml&quot;);\r\n\r\nGZipStream decompressingStream = new GZipStream(sourceFileStream, \r\n    CompressionMode.Decompress);\r\nint byteRead;\r\nwhile((byteRead = decompressingStream.ReadByte()) != -1) \r\n{\r\n    destFileStream.WriteByte((byte)byteRead);\r\n}\r\n\r\ndecompressingStream.Close();\r\nsourceFileStream.Close();\r\ndestFileStream.Close();\r\n<\/pre>\n<p>The code segment begins by first gaining code access to <code>sitemap.xml.gz<\/code>, via <code>sourceFileStream<\/code>, which we are going to decompress. After that, we gain write access to <code>sitemap.xml<\/code>, via <code>destFileStream<\/code>, which we are going to write decompressed data read from our <code>GZipStream<\/code> instance.<\/p>\n<p>When we had gained code access to the relevant files, we create a <code>GZipStream<\/code> instance, passing <code>sourceFileStream<\/code> and the <code>CompressionMode.Decompress<\/code> enumeration option into one of its constructors. We set this instance to <code>decompressingStream<\/code>.<\/p>\n<p>Once we have the necessary <code>Stream<\/code> objects, we begin to decompress <code>sitemap.xml.gz<\/code>. To do that, we simply read from <code>decompressingStream<\/code> and write its contents to <code>destFileStream<\/code>.<\/p>\n<p>When we are done with decompressing <code>sitemap.xml.gz<\/code>, we relinquish accesses to <code>decompressingStream<\/code>, <code>sourceFileStream<\/code> and <code>destFileStream<\/code>.<\/p>\n<h3>Other posts that may interest you<\/h3>\n<ul>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/quick-references\/handy-vbscript-functions-for-dealing-with-zip-files-and-folders\/\" title=\"Handy vbscript functions for dealing with zip files and folders\" target=\"_blank\">Handy vbscript functions for dealing with zip files and folders<\/a><\/li>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/quick-references\/how-to-interact-with-applications-via-command-line-in-c\/\" title=\"How to interact with applications via command line in C#\" target=\"_blank\">How to interact with applications via command line in C#<\/a><\/li>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/quick-references\/c-facilities-for-dealing-with-folders\/\" title=\"C# facilities for dealing with folders\" target=\"_blank\">C# facilities for dealing with folders<\/a><\/li>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/quick-references\/a-mechanism-for-serving-http-requests-in-c\/\" title=\"A mechanism for serving HTTP requests in C#\" target=\"_blank\">A mechanism for serving HTTP requests in C#<\/a><\/li>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/quick-references\/sending-a-file-and-some-form-data-via-http-post-in-c\/\" title=\"Sending a file and some form data via HTTP post in C#\" target=\"_blank\">Sending a file and some form data via HTTP post in C#<\/a><\/li>\n<\/ul>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-3G\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-3G&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-3G&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F228&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>Files are useful for applications to communicate with others. For instance, web applications can communicate with Google&#8217;s crawlers via sitemaps on the Internet. A web application write urls to its pages to its sitemap. When Google&#8217;s crawlers come to this web applications, they will download these sitemaps to index urls to the pages of .<\/p>\n<p>To reduce the time needed for files to be trasmitted over the network, it is a good idea for applications to compress files before sending them out to the network, or for a web application, making them available for <a href=\"http:\/\/www.techcoil.com\/blog\/quick-references\/downloading-a-file-from-via-http-post-and-http-get-in-c\/\" title=\"Downloading a file via HTTP post and HTTP get in C#\" target=\"_blank\">download via HTTP<\/a>.<\/p>\n<p>In this post, I document how we can compress and decompress files via the GZip implementation in C# &#8211; <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.io.compression.gzipstream.aspx\" title=\"MSDN reference to System.IO.Compression.GZipstream\" target=\"_blank\"><code>System.IO.Compression.GZipStream<\/code><\/a>.<\/p>\n","protected":false},"author":1,"featured_media":1189,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375],"tags":[139,140,138],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/C-Sharp-Logo.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-3G","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/228"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=228"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/228\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1189"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}