---
title: "Compress a file while upload the file is possible in asp.net?"  
description: "Compress a file while upload the file is possible in asp.net?"  
author: "Anonymous User"  
published: 2014-09-20  
updated: 2014-09-20  
canonical: https://www.mindstick.com/forum/2259/compress-a-file-while-upload-the-file-is-possible-in-asp-dot-net  
category: "asp.net"  
tags: ["c#", ".net", "asp.net"]  
reading_time: 1 minute  

---

# Compress a file while upload the file is possible in asp.net?

here i browsing a [file](https://www.mindstick.com/articles/59/encrypting-and-decrypting-files-using-c-sharp) and elect and that i clicking [button](https://www.mindstick.com/articles/63/how-to-add-button-in-datagridview-in-csharp-dot-net) to [transfer](https://www.mindstick.com/blog/11733/debt-management-for-startups-consolidation-loan-vs-balance-transfer) currently the file ought to compress and will not be save in nothing format.as it is that if i transfer [pdf](https://www.mindstick.com/articles/311856/6-of-the-best-pdf-editors-to-meet-your-business-needs) it should be compress and save in a very folder as pdf format...please create this [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) on behalf of me...

```
protected void btnUpload_Click(object sender, EventArgs e)   {       if (fileUpload.HasFile)       {           string filename = Path.GetFileName(fileUpload.PostedFile.FileName);           string path = Server.MapPath("~/SampleFiles/" + filename);           fileUpload.SaveAs(path);           lblTextMsg.Text = "File Uploaded Successfully.";       }   }
```

## Replies

### Reply by Anchal Kesharwani

hi ankit,\

You can use GZipStream class.

```
string fileToBeCompressed =//path of original file;string zipFilename = //path of zipped file; using (FileStream target = new FileStream(zipFilename, FileMode.Create,FileAccess.Write))using (GZipStream alg = new GZipStream(target, CompressionMode.Compress)){    byte[] data = File.ReadAllBytes(fileToBeCompressed);    alg.Write(data, 0, data.Length);    alg.Flush();}
```


---

Original Source: https://www.mindstick.com/forum/2259/compress-a-file-while-upload-the-file-is-possible-in-asp-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
