COMPRESS A FILE WHILE UPLOAD THE FILE IS POSSIBLE IN ASP.NET?

Ankit Singh

Total Post:341

Points:2389
Posted by  Ankit Singh
.NET  ASP.Net  C# 
 1028  View(s)
Ratings:
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.";

       }

   }

  1. Anchal Kesharwani

    Post:10

    Points:70
    Re: Compress a file while upload the file is possible in asp.net?

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

    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!