<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>System.IO.Compression.FileSystem &#8211; Sibeesh Passion</title>
	<atom:link href="https://mail.sibeeshpassion.com/tag/system-io-compression-filesystem/feed/" rel="self" type="application/rss+xml" />
	<link>https://mail.sibeeshpassion.com</link>
	<description>My passion towards life</description>
	<lastBuildDate>Wed, 02 Jun 2021 15:15:08 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>/wp-content/uploads/2017/04/Sibeesh_Passion_Logo_Small.png</url>
	<title>System.IO.Compression.FileSystem &#8211; Sibeesh Passion</title>
	<link>https://mail.sibeeshpassion.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Programmatically Extract or Unzip Zip,Rar Files And Check</title>
		<link>https://mail.sibeeshpassion.com/programmatically-extract-or-unzip-ziprar-files-and-check/</link>
					<comments>https://mail.sibeeshpassion.com/programmatically-extract-or-unzip-ziprar-files-and-check/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Thu, 25 Feb 2016 00:00:22 +0000</pubDate>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Asp.Net]]></category>
		<category><![CDATA[CSharp]]></category>
		<category><![CDATA[Extract Files]]></category>
		<category><![CDATA[HttpFileCollectionBase]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[System.IO.Compression]]></category>
		<category><![CDATA[System.IO.Compression.FileSystem]]></category>
		<category><![CDATA[Unzip Files]]></category>
		<category><![CDATA[Upload in MVC]]></category>
		<category><![CDATA[ZipArchive]]></category>
		<guid isPermaLink="false">http://sibeecst_passion.com/?p=11281</guid>

					<description><![CDATA[In this pose we will see how we can extract or unzip the uploaded files and check for some files in it in a in a programmatic manner. We will use a normal MVC application to work with this demo. We will use Microsoft System.IO.Compression, System.IO.Compression.FileSystem namespaces and the classes, for example ZipArchive, in the namespace for extracting or unzipping the files we upload. Once it is uploaded we will check the same by looping through it. For the client side coding, we uses jQuery. I guess this is enough for the introduction, now we will move on to our [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>In this pose we will see how we can extract or unzip the uploaded files and check for some files in it in a in a programmatic manner. We will use a normal <a href="http://sibeeshpassion.com/category/mvc/" target="_blank" rel="noopener">MVC</a> application to work with this demo. We will use Microsoft System.IO.Compression, System.IO.Compression.FileSystem namespaces and the classes, for example ZipArchive, in the namespace for extracting or unzipping the files we upload. Once it is uploaded we will check the same by looping through it. For the client side coding, we uses <a href="http://sibeeshpassion.com/category/jQuery/" target="_blank" rel="noopener">jQuery</a>. I guess this is enough for the introduction, now we will move on to our application. Let us create it. I hope you will like this article. </p>
<p><strong>Download the source code</strong></p>
<p>You can always download the source code here: 	</p>
<li><a href="https://gallery.technet.microsoft.com/Programmatically-Extract-0544debd" target="_blank" rel="noopener">Programmatically Extract or Unzip Zip,Rar Files And Check</a></li>
<p><strong>Background</strong></p>
<p>As you are aware most websites accepts only .zip or any other zipped formats, this is for ensuring that only less amount of data is being uploaded to the server. We can reduce almost 60 percentage of the size if we zipped the same. Now the problem is, how you can validate any zipped item which is uploaded to your site? How can you check for any particular file in that zipped item? For example if you need to find out any files with extension of .sql, .ico or .txt? What if you want to intimate user that the uploaded items does not have any particular file that must be needed? You need to loop through the contents available in it right? This is what we are going to discuss in this post. I had a situation of uploading my old back ups of my personal website (<a href="http://sibeeshpassion.com/" target="_blank" rel="noopener">http://sibeeshpassion.com/</a>) and after uploading I wanted to check whether the uploaded .zip file has .ico file in it. The process we are going to do is listed below.</p>
<li>Create a MVC application</li>
<li>Install jQuery</li>
<li>Create Controller and View</li>
<li>Upload Action Using jQuery</li>
<li>Add System.IO.Compression, System.IO.Compression.FileSystem references</li>
<li>Checks for the file using ZipArchive class</li>
<p><strong>Using the code</strong></p>
<p>Now we will move into the coding part. I hope your MVC application is ready for action 🙂 with all the needed packages. </p>
<p><strong>Create a controller</strong></p>
<p>Now we will create a controller, view with an action in it as follows. </p>
<p>[csharp]<br />
public ActionResult Index()<br />
        {<br />
            return View();<br />
        }<br />
[/csharp]</p>
<p><strong>Update the view</strong></p>
<p>Now in the view, we will add a file upload and other needed elements. </p>
<p>[html]<br />
&lt;input type=&quot;file&quot; name=&quot;FileUpload1&quot; id=&quot;fileUpload&quot; placeholder=&quot;Please select the file&quot; /&gt;&lt;br /&gt;<br />
&lt;input id=&quot;btnUploadFile&quot; type=&quot;button&quot; value=&quot;Upload File&quot; /&gt;<br />
&lt;div id=&quot;message&quot;&gt;&lt;/div&gt;<br />
[/html]</p>
<p><strong>Add the JS references</strong></p>
<p>Add the references as follows.</p>
<p>[html]<br />
&lt;script src=&quot;~/scripts/jquery-1.10.2.min.js&quot;&gt;&lt;/script&gt;<br />
&lt;script src=&quot;~/scripts/MyScript.js&quot;&gt;&lt;/script&gt;<br />
[/html]</p>
<p>Here <em>MyScript.js</em> is where we are going to write some scripts. </p>
<p><strong>Style the elements (Optional) </strong></p>
<p>You can style your view as follows. This step is absolutely optional, for me the view should not as simple as default one. And to be frank I am not a good designer 🙂</p>
<p>[css]<br />
&lt;style&gt;<br />
    #fileUpload {<br />
        border: 1px solid #ccc;<br />
        padding: 10px;<br />
        border-radius: 5px;<br />
        font-size: 14px;<br />
        font-family: cursive;<br />
        color: blue;<br />
    }</p>
<p>    #btnUploadFile {<br />
        border: 1px solid #ccc;<br />
        padding: 10px;<br />
        border-radius: 5px;<br />
        font-size: 14px;<br />
        font-family: cursive;<br />
        color: blue;<br />
    }</p>
<p>    #message {<br />
        border-radius: 5px;<br />
        font-size: 14px;<br />
        font-family: cursive;<br />
        color: blue;<br />
        margin-top: 15px;<br />
    }<br />
    h2 {<br />
        border-radius: 5px;<br />
        font-size: 20px;<br />
        font-family: cursive;<br />
        color: blue;<br />
        margin-top: 15px;<br />
    }<br />
&lt;/style&gt;<br />
[/css]</p>
<p><strong>Add the upload action in script</strong></p>
<p>Now it is time to create upload action. For that, please go to the script file MyScript.js and do the coding as follows. </p>
<p>[js]<br />
$(document).ready(function () {<br />
    $(&#8216;#btnUploadFile&#8217;).on(&#8216;click&#8217;, function () {<br />
        var data = new FormData();<br />
        var files = $(&quot;#fileUpload&quot;).get(0).files;<br />
        // Add the uploaded image content to the form data collection<br />
        if (files.length &gt; 0) {<br />
            data.append(&quot;UploadedImage&quot;, files[0]);<br />
        }<br />
        // Make Ajax request with the contentType = false, and procesDate = false<br />
        var ajaxRequest = $.ajax({<br />
            type: &quot;POST&quot;,<br />
            url: &quot;Home/Upload&quot;,<br />
            contentType: false,<br />
            processData: false,<br />
            data: data,<br />
            success: function (data) {<br />
                $(&#8216;#message&#8217;).html(data);<br />
            },<br />
            error: function (e)<br />
            {<br />
                console.log(&#8216;Oops, Something went wrong!.&#8217; + e.message);<br />
            }<br />
        });<br />
        ajaxRequest.done(function (xhr, textStatus) {<br />
            // Do other operation<br />
        });<br />
    });<br />
});<br />
[/js]</p>
<p>If you are not aware of uploading and downloading in MVC, I strongly recommend you to have a look here: <a href="http://sibeeshpassion.com/uploading-and-downloading-in-mvc-step-by-step/" target="_blank" rel="noopener">Uploading and Downloading in MVC Step-by-Step</a>. </p>
<p>So we have set Home/Upload as the URL action in our <a href="http://sibeeshpassion.com/tag/jquery-ajax/" target="_blank" rel="noopener">Ajax</a> function right? So we are going to create that action. Go to your controller and create a JsonResult action as follows. </p>
<p>[csharp]<br />
public JsonResult Upload()<br />
        {<br />
            string isValid = checkIsValid(Request.Files);<br />
            return Json(isValid, JsonRequestBehavior.AllowGet);<br />
        }<br />
[/csharp]</p>
<p>So we have done that too, now stop for a while. To work with <em>ZipArchive</em> class you must include the namespace as listed below. </p>
<p>[csharp]<br />
using System.IO.Compression;<br />
[/csharp]</p>
<p>Now you will be shown an error as &#8220;Are you missing an assembly reference?&#8221;. Yes we are. We have not added that references right? So now we are going to add the references for System.IO.Compression and System.IO.Compression.FileSystem. </p>
<p>Right click on Reference in your project and browse for the references. If you uses Visual Studio 2015, the references will be in C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6. </p>
<blockquote><p>Please be noted that the folder names will differs according to your framework version</p></blockquote>
<div id="attachment_11282" style="width: 660px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/Compression_References-e1455888597687.png"><img fetchpriority="high" decoding="async" aria-describedby="caption-attachment-11282" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/Compression_References-e1455888597687.png" alt="Compression_References" width="650" height="447" class="size-full wp-image-11282" srcset="/wp-content/uploads/2016/02/Compression_References-e1455888597687.png 650w, /wp-content/uploads/2016/02/Compression_References-e1455888597687-300x206.png 300w, /wp-content/uploads/2016/02/Compression_References-e1455888597687-160x110.png 160w, /wp-content/uploads/2016/02/Compression_References-e1455888597687-400x275.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a><p id="caption-attachment-11282" class="wp-caption-text">Compression_References</p></div>
<p>Now we will go back to our controller and write the codes for the function <em>checkIsValid(Request.Files)</em></p>
<p>[csharp]<br />
private string checkIsValid(HttpFileCollectionBase files)<br />
        {<br />
            string isValid = string.Empty;<br />
            try<br />
            {<br />
                foreach (string upload in Request.Files)<br />
                {<br />
                    if (Request.Files[upload].ContentType == &quot;application/octet-stream&quot;) //Content type for .zip is application/octet-stream<br />
                    {<br />
                        if (Request.Files[upload].FileName != &quot;&quot;)<br />
                        {<br />
                            string path = AppDomain.CurrentDomain.BaseDirectory + &quot;/ App_Data / uploads /&quot;;<br />
                            if (!Directory.Exists(path))<br />
                            {<br />
                                // Try to create the directory.<br />
                                DirectoryInfo di = Directory.CreateDirectory(path);<br />
                            }</p>
<p>                            string filename = Path.GetFileName(Request.Files[upload].FileName);<br />
                            string pth = Path.Combine(path, filename);<br />
                            Request.Files[upload].SaveAs(pth);<br />
                            isValid = CheckForTheIcon(pth);<br />
                        }<br />
                    }<br />
                    else<br />
                    {<br />
                        isValid = &quot;Only .zip files are accepted.&quot;;<br />
                    }<br />
                }<br />
                return isValid;<br />
            }<br />
            catch (Exception)<br />
            {<br />
                return &quot;Oops!. Something went wrong&quot;;<br />
            }<br />
        }<br />
[/csharp]</p>
<p>As you can see we are looping through the <em>HttpFileCollectionBase</em> files and checks for the content type first. </p>
<blockquote><p>Please be noted that the content type for the .zip file is application/octet-stream</p></blockquote>
<p>Once the checking is done, we will save the files to a folder, we will send the path to the function <em>CheckForTheIcon(pth)</em>. So our next thing we need to do is to create the function CheckForTheIcon().</p>
<p>[csharp]<br />
  private string CheckForTheIcon(string strPath)<br />
        {<br />
            string result = string.Empty;<br />
            try<br />
            {<br />
                using (ZipArchive za = ZipFile.OpenRead(strPath))<br />
                {<br />
                    foreach (ZipArchiveEntry zaItem in za.Entries)<br />
                    {<br />
                        if (zaItem.FullName.EndsWith(&quot;.ico&quot;, StringComparison.OrdinalIgnoreCase))<br />
                        {<br />
                            result = &quot;Success&quot;;<br />
                        }<br />
                        else<br />
                        {<br />
                            result = &quot;No ico files has been found&quot;;<br />
                        }<br />
                    }<br />
                }<br />
                return result;<br />
            }<br />
            catch (Exception)<br />
            {<br />
                result = &quot;Oops!. Something went wrong&quot;;<br />
                return result;<br />
            }<br />
        }<br />
[/csharp]</p>
<p>As you can see we are looping through each <em>ZipArchive</em> class items and checks for the &#8216;.ico&#8217; file in it. So if there is no error and there is &#8216;.ico&#8217; file in the uploaded item zip item, you will get the message as &#8220;Success: or if it does not contain the &#8216;.ico&#8217; file, you will get the message as &#8220;No ico files has been found&#8221;. </p>
<p>Now it is time to see our output. Please run your application. </p>
<p><strong>Output</strong></p>
<div id="attachment_11283" style="width: 638px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/If_you_try_to_upload_not_zipped_item.png"><img decoding="async" aria-describedby="caption-attachment-11283" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/If_you_try_to_upload_not_zipped_item.png" alt="If_you_try_to_upload_not_zipped_item" width="628" height="298" class="size-full wp-image-11283" srcset="/wp-content/uploads/2016/02/If_you_try_to_upload_not_zipped_item.png 628w, /wp-content/uploads/2016/02/If_you_try_to_upload_not_zipped_item-300x142.png 300w, /wp-content/uploads/2016/02/If_you_try_to_upload_not_zipped_item-400x190.png 400w" sizes="(max-width: 628px) 100vw, 628px" /></a><p id="caption-attachment-11283" class="wp-caption-text">If_you_try_to_upload_not_zipped_item</p></div>
<div id="attachment_11284" style="width: 653px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/If_you_try_to_upload_zipped_item_which_does_not_have_ico_file.png"><img decoding="async" aria-describedby="caption-attachment-11284" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/If_you_try_to_upload_zipped_item_which_does_not_have_ico_file.png" alt="If_you_try_to_upload_zipped_item_which_does_not_have_ico_file" width="643" height="288" class="size-full wp-image-11284" srcset="/wp-content/uploads/2016/02/If_you_try_to_upload_zipped_item_which_does_not_have_ico_file.png 643w, /wp-content/uploads/2016/02/If_you_try_to_upload_zipped_item_which_does_not_have_ico_file-300x134.png 300w, /wp-content/uploads/2016/02/If_you_try_to_upload_zipped_item_which_does_not_have_ico_file-400x179.png 400w" sizes="(max-width: 643px) 100vw, 643px" /></a><p id="caption-attachment-11284" class="wp-caption-text">If_you_try_to_upload_zipped_item_which_does_not_have_ico_file</p></div>
<div id="attachment_11285" style="width: 660px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2016/02/If_you_try_to_upload_zipped_item_which_have_ico_file-e1455889647121.png"><img decoding="async" aria-describedby="caption-attachment-11285" src="http://sibeeshpassion.com/wp-content/uploads/2016/02/If_you_try_to_upload_zipped_item_which_have_ico_file-e1455889647121.png" alt="If_you_try_to_upload_zipped_item_which_have_ico_file" width="650" height="277" class="size-full wp-image-11285" srcset="/wp-content/uploads/2016/02/If_you_try_to_upload_zipped_item_which_have_ico_file-e1455889647121.png 650w, /wp-content/uploads/2016/02/If_you_try_to_upload_zipped_item_which_have_ico_file-e1455889647121-300x128.png 300w, /wp-content/uploads/2016/02/If_you_try_to_upload_zipped_item_which_have_ico_file-e1455889647121-400x170.png 400w" sizes="(max-width: 650px) 100vw, 650px" /></a><p id="caption-attachment-11285" class="wp-caption-text">If_you_try_to_upload_zipped_item_which_have_ico_file</p></div>
<p><strong>Conclusion</strong></p>
<p>Did I miss anything that you may think which is needed? Have you ever wanted to do this requirement? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.</p>
<p><strong>Your turn. What do you think?</strong></p>
<p>A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.</p>
<p>Kindest Regards<br />
Sibeesh Venu</p>
]]></content:encoded>
					
					<wfw:commentRss>https://mail.sibeeshpassion.com/programmatically-extract-or-unzip-ziprar-files-and-check/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>
