Site icon Dotnet Helpers

Code shows how to zip while uploading the file.

This code shows how to zip while uploading the file.

Need to include System.IO.Compression namespace to zip the upload files. In click event write the below code.

using System.IO.Compression;
using System.IO;

protected void btnUpload_Click(object sender, EventArgs e)
{
string strName = Path.GetFileName( FileUpload1.PostedFile.FileName);
Stream myStream = FileUpload1.PostedFile.InputStream;
byte[] myBuffer = new byte[myStream.Length + 1];
myStream.Read(myBuffer, 0, myBuffer.Length);
myStream.Close();
FileStream myCompressedFile = default(FileStream);
myCompressedFile = File.Create(Server.MapPath(Path.ChangeExtension(strName, “gz”)));
GZipStream myStreamZip = new GZipStream(myCompressedFile, CompressionMode.Compress);
myStreamZip.Write(myBuffer, 0, myBuffer.Length);
myStreamZip.Close();
}

Here i am set size as +1 of upladed file size.

Exit mobile version