Upload and compress a file in ASP.NET
How to upload a file and compress it using the compression methods in ASP .NET.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Mayur GujrathiPosted Jun 13, 2011, 3:47 AM
Posted Jun 13, 2011, 3:19 AM
'A String object reads the file name (locally)
Dim strName As String = Path.GetFileName(fileUpload.PostedFile.FileName)
'Create a stream object to read the file.
Dim myStream As Stream = fileUpload.PostedFile.InputStream
'Allocate space in buffer for use according to length of file.
Dim myBuffer(myStream.Length) As Byte
'Read the file using the Stream object and fill the buffer.
myStream.Read(myBuffer, 0, myBuffer.Length)
myStream.Close()
'Change the extension of the file creating a FileStream object.
Dim myCompressedFile As FileStream
myCompressedFile = File.Create( _
Server.MapPath(Path.ChangeExtension(strName, "gz")))
'GZip object that compress the file
Dim myStreamZip As New GZipStream(myCompressedFile, CompressionMode.Compress)
'Write Back
myStreamZip.Write(myBuffer, 0, myBuffer.Length)
myStreamZip.Close()
Me.lblResult.Text = """" + strName lblResult.Text = lblResult.Text + " was compressed &uploaded successfully."
http://www.beansoftware.com/ASP.NET-Tutorials/File-Upload-Compression.aspx