0

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,

1 Answer 1

3

GZipStream compresses a stream of bytes, it does no more than that. It does not embed file info in the stream, so you need to embed it somewhere.

The easiest way to restore the file name is to name it with the original name e.g. Sample.bak.zip.

If that doesn't work for you can use SharpZipLib (samples here) which does the embedding of the file info for you.

If you don't want to go that either of thoes you can either create your own file format and embed the file info or you can try and implement the standard gzip format

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.