1 Attachment(s)
GZipStream extracts only 1/4 of the file
This code uncompresses 1/4 of the gzipped file. & I took it directely from the help file sample function codes.
Any idea why is it doing this or how to fix it? gz file is attached below & it is not damaged.
Edit: I read some stuff hire & there & found this: tutorial that says that MSDN doesn’t set the size of the array and uses a default size of 4086 bytes. But I have no clue how to implement it into my code.
In this gz file there are only around 50 lines but there are files with over 1000 lines. So I tested & indeed this seems to be the problem, because if I increase the buffer(4096) more lines are extracted.
PHP Code:
Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.IO.Compression
Public Class CompressionSnippet
Private Sub CompressionSnippet_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Hide()
UncompressFile("1.gz") ' Name of the compressed file
Me.Close()
End Sub
Public Shared Sub UncompressFile(ByVal path As String)
Dim sourceFile As FileStream = File.OpenRead(path)
Dim destinationFile As FileStream = File.Create(path + ".txt") ' Outputs test.gz.txt
' Because the uncompressed size of the file is unknown,
' we are imports an arbitrary buffer size.
Dim buffer(4096) As Byte
Dim n As Integer
Using input As New GZipStream(sourceFile, _
CompressionMode.Decompress, False)
n = input.Read(buffer, 0, buffer.Length)
destinationFile.Write(buffer, 0, n)
End Using
' Close the files.
sourceFile.Close()
destinationFile.Close()
End Sub
End Class
Re: GZipStream extracts only 1/4 of the file
That's because you only read from the input stream once. You need to have a loop and keep reading it and writing to the destinationFile until n < buffer.Length (which means you have no more bytes to read from the input stream).
Re: GZipStream extracts only 1/4 of the file
Quote:
Originally Posted by
stanav
That's because you only read from the input stream once. You need to have a loop and keep reading it and writing to the destinationFile until n < buffer.Length (which means you have no more bytes to read from the input stream).
Im pretty much novice in this, but I sord of understood this myself, from that tutorial. But I have no idea how to do this. Cuz I have only used autoitscript where I can say read a line from file, or a line from array & put it in a loop & read & write & all this is logical.
But VB makes no sense for me. Can you show me how to do this loop? And also include comments, so I can understand whats happening please?
Edit: but looping like this is only possibe with infinite loop & I cant make infinite loop in vb. i need some end value to loop until if I use for>next. What should I use as the end loop value?
Anyway I tried this, just like you said: didnt work
PHP Code:
Dim i = 0
For n = input.Read(buffer, 0, buffer.Length) To n < buffer.Length
destinationFile.Write(buffer, 0, n)
n = input.Read(buffer, i + buffer.Length, buffer.Length + buffer.Length)
Next
End Using
Re: GZipStream extracts only 1/4 of the file
Something like this (note that all I did was just stick your code inside a Do... Loop Until loop block)
Code:
Using input As New GZipStream(sourceFile, _
CompressionMode.Decompress, False)
Do
n = input.Read(buffer, 0, buffer.Length)
destinationFile.Write(buffer, 0, n)
Loop Until n < buffer.Length
End Using