Problem When Compressing File using vb.net

Posted by Amr Elnashar on Stack Overflow See other posts from Stack Overflow or by Amr Elnashar
Published on 2011-01-04T14:50:24Z Indexed on 2011/01/04 14:53 UTC
Read the original article Hit count: 207

Filed under:
|

I have File Like "Sample.bak" and when I compress it to be "Sample.zip" I lose the file extension inside the zip file I meann when I open the compressed file I find "Sample" without any extension.

I use this code :

 Dim name As String = Path.GetFileName(filePath).Replace(".Bak", "")
 Dim source() As Byte = System.IO.File.ReadAllBytes(filePath)
 Dim compressed() As Byte = ConvertToByteArray(source)
 System.IO.File.WriteAllBytes(destination & name & ".Bak" & ".zip", compressed)

Or using this code :

Public Sub cmdCompressFile(ByVal FileName As String)

'Stream object that reads file contents
Dim streamObj As Stream = New StreamReader(FileName).BaseStream

'Allocate space in buffer according to the length of the file read
Dim buffer(streamObj.Length) As Byte

'Fill buffer
streamObj.Read(buffer, 0, buffer.Length)
streamObj.Close()

'File Stream object used to change the extension of a file
Dim compFile As System.IO.FileStream = File.Create(Path.ChangeExtension(FileName, "zip"))

'GZip object that compress the file
Dim zipStreamObj As New GZipStream(compFile, CompressionMode.Compress)

'Write to the Stream object from the buffer
zipStreamObj.Write(buffer, 0, buffer.Length)
zipStreamObj.Close()

End Sub

please I need to compress the file without loosing file extension inside compressed file.

thanks,

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about compression