Home > DeveloperSection > Forums > Compress a file while upload the file is possible in asp.net?
Ankit Singh

Total Post:341

Points:2389
Posted on    September-20-2014 2:29 AM

 ASP.Net .NET  ASP.Net  C# 
Ratings:


 1 Reply(s)
 588  View(s)
Rate this:

here i browsing a file and elect and that i clicking button to transfer currently the file ought to compress and will not be save in nothing format.as it is that if i transfer pdf it should be compress and save in a very folder as pdf format...please create this code 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.";

       }

   }



Anchal Kesharwani
Anchal Kesharwani

Total Post:10

Points:70
Posted on    September-20-2014 2:49 AM

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();

}


Don't want to miss updates? Please click the below button!

Follow MindStick