Results 1 to 4 of 4

Thread: GZipStream extracts only 1/4 of the file

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    74

    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.ObjectByVal e As System.EventArgsHandles 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
    .DecompressFalse)

                
    input.Read(buffer0buffer.Length)
                
    destinationFile.Write(buffer0n)
            
    End Using

            
    ' Close the files.
            sourceFile.Close()
            destinationFile.Close()
        End Sub
    End Class 
    Attached Files Attached Files
    • File Type: gz 1.gz (2.8 KB, 40 views)
    Last edited by goldenix; Mar 9th, 2010 at 12:19 PM.

    M.V.B. 2008 Express Edition

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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).
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    74

    Re: GZipStream extracts only 1/4 of the file

    Quote Originally Posted by stanav View Post
    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 input.Read(buffer0buffer.LengthTo n buffer.Length
                    destinationFile
    .Write(buffer0n)

                    
    input.Read(bufferbuffer.Lengthbuffer.Length buffer.Length)
                
    Next

            End Using 
    Last edited by goldenix; Mar 9th, 2010 at 01:50 PM.

    M.V.B. 2008 Express Edition

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width