Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. GZipStream ends too early

GZipStream ends too early

Scheduled Pinned Locked Moved C#
csharphelplinq
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Paladin2000
    wrote on last edited by
    #1

    For some reason, while reading a GZip file I am having an issue where it stops after about 24K characters; it never reaches the end of file (I have tried it on several Gzip files of differing sizes, each containing one text file). I created a small console app to replicate the problem, see below. Any insight would be welcomed... Framework is .NET 3.5

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.IO.Compression;

    namespace GZipDump
    {
    class Program
    {
    static void Main(string[] args)
    {
    UInt64 charsRead = 0;
    string file;
    if (args.Length > 0)
    file = args[0];
    else
    {
    Console.Write("GZip file to be read (full path): ");
    file = Console.ReadLine();
    }

            using (GZipStream gz = new GZipStream(new FileInfo(file).OpenRead(), CompressionMode.Decompress))
            {
                byte\[\] bytes = new byte\[1\];
                while (gz.Read(bytes, 0, 1) > 0)
                {
                    Console.Write(Convert.ToChar(bytes\[0\]));
                    charsRead++;
                }
            }
    
            Console.WriteLine("\\n\\nCompleted.");
            Console.WriteLine("Characters Read: " + charsRead);
            Console.ReadLine();
        }
    }
    

    }

    A 1 Reply Last reply
    0
    • P Paladin2000

      For some reason, while reading a GZip file I am having an issue where it stops after about 24K characters; it never reaches the end of file (I have tried it on several Gzip files of differing sizes, each containing one text file). I created a small console app to replicate the problem, see below. Any insight would be welcomed... Framework is .NET 3.5

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.IO;
      using System.IO.Compression;

      namespace GZipDump
      {
      class Program
      {
      static void Main(string[] args)
      {
      UInt64 charsRead = 0;
      string file;
      if (args.Length > 0)
      file = args[0];
      else
      {
      Console.Write("GZip file to be read (full path): ");
      file = Console.ReadLine();
      }

              using (GZipStream gz = new GZipStream(new FileInfo(file).OpenRead(), CompressionMode.Decompress))
              {
                  byte\[\] bytes = new byte\[1\];
                  while (gz.Read(bytes, 0, 1) > 0)
                  {
                      Console.Write(Convert.ToChar(bytes\[0\]));
                      charsRead++;
                  }
              }
      
              Console.WriteLine("\\n\\nCompleted.");
              Console.WriteLine("Characters Read: " + charsRead);
              Console.ReadLine();
          }
      }
      

      }

      A Offline
      A Offline
      Andrew Rissing
      wrote on last edited by
      #2

      I've never treated the GZipStream like that, but you could try using one of the decompression example out of MSDN found here[^]. If you don't retrieve the original file out of the zip, you may be dealing with a different compression that isn't supported. If you do, then it'd be something to do with how you're reading it out here. Give it a whirl and see what happens.

      P 1 Reply Last reply
      0
      • A Andrew Rissing

        I've never treated the GZipStream like that, but you could try using one of the decompression example out of MSDN found here[^]. If you don't retrieve the original file out of the zip, you may be dealing with a different compression that isn't supported. If you do, then it'd be something to do with how you're reading it out here. Give it a whirl and see what happens.

        P Offline
        P Offline
        Paladin2000
        wrote on last edited by
        #3

        Unfortunately, that example is .NET 4.0 specific. This line:

        Decompress.CopyTo(outFile);

        Will not work in 3.5, because the CopyTo method does not exist (in fact, if you change the framework version in the link you provided, the example disappears). The example at this url at MSDN (the class page itself) does use the 3-parameter Read method (it is 3.5). Part of my objective is to "read" the contents of a Gzip file *without* extracting it to disk. However, having it stop early is a problem...

        A 1 Reply Last reply
        0
        • P Paladin2000

          Unfortunately, that example is .NET 4.0 specific. This line:

          Decompress.CopyTo(outFile);

          Will not work in 3.5, because the CopyTo method does not exist (in fact, if you change the framework version in the link you provided, the example disappears). The example at this url at MSDN (the class page itself) does use the 3-parameter Read method (it is 3.5). Part of my objective is to "read" the contents of a Gzip file *without* extracting it to disk. However, having it stop early is a problem...

          A Offline
          A Offline
          Andrew Rissing
          wrote on last edited by
          #4

          Now that I have access to VS today, I can test your code. After creating a GZip file with .NET, via the following code:

          using (GZipStream gz = new GZipStream(new FileInfo(file).Create(), CompressionMode.Compress))
          {
          byte[] bytes = File.ReadAllBytes(file);

          gz.Write(bytes, 0, bytes.Length);
          }

          I then ran your code against it and it worked just fine. The question I have is are you accessing a file that isn't local? If so, this may be the source of your problem. Otherwise, you might want to create the zip file with .NET and see if that solves your problem.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups