Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi ,

Couls some one please help me . I have to unzip a .gz file. The ziped file is of 33 MB and after zip it will be around 3GB . I have written the below code and able to unzip the file of only 256 kb.

C#
public static void Main()

        {

            string path = @"\\lousqlwis18\Ftproot\CGXRPT2\Predictive_Score";



            DirectoryInfo inf = new DirectoryInfo(path);



            foreach (FileInfo f in inf.GetFiles())

            {

                if (f.Name.ToString() == "TEST.dat.gz")

                {

                    decomp(f);

                }

            }



        }

        public static void decomp(FileInfo fi)

        {

            long i=0;long c;

            using (FileStream inFile = fi.OpenRead())

            {



                string curFile = fi.FullName;

                string origName = curFile.Remove(curFile.Length - fi.Extension.Length);





                using (FileStream outFile = File.Create(origName))

                {

                    using (GZipStream Dc = new GZipStream(inFile,

                            CompressionMode.Decompress))

                    {



                        byte[] buffer = new byte[10240];

                        int numRead;

                        while ((numRead = Dc.Read(buffer, 0, buffer.Length)) != 0)

                        {

                            outFile.Write(buffer, 0, numRead);

                            Console.Write(outFile.Length.ToString());

                            Console.Write(numRead.ToString());

                            c=(numRead+i);

                            Console.WriteLine(c.ToString());

                        }

                        Console.WriteLine("Decompressed: {0}", fi.Name);



                    }

                }

            }

        }


Thanks,
Preetpal
Posted
Updated 17-Jan-21 4:23am

is it just me ? or are you making the inner loop much harder than you need

I was thinking of something like

C#
using (GZipStream Dc = new GZipStream(inFile,                            CompressionMode.Decompress))
    {
        Dc.CopyTo(outFile);
    }
 
Share this answer
 
Comments
preetpal kapoor 22-Jul-13 7:26am    
By Doing Dc.CopyTo(outFile); Only 256 kb of data is getting decompressed. I want the full file to get decomposed.
Garth J Lancaster 22-Jul-13 8:52am    
hmmm, ok .. I still cant see anything fundamentally wrong with your code

Im at home so dont have my dev environment to check, but I think my last working code I used came from here http://blogs.msdn.com/b/bclteam/archive/2005/06/15/429542.aspx

He uses 'Stream', I think I cut and pasted from his into mine and it worked ... most of the time I actually use ionic.zip because I have multiple entries in a zip file

preetpal kapoor 22-Jul-13 10:29am    
The code is using mgunzip.exe which I can't use as per the client requirement. Could you please provide any other way to do this as this is not a big problem for me. Thanks in advance.
Garth J Lancaster 22-Jul-13 20:18pm    
Im pretty sure you've just proved you cant read code for sh*t

He's writing a program, that in the unix style, is the same code used for two programs - if you run it as mgzip, it does a compress, or you run it as mgunzip, it does a decompress .. It has all the code you need

look at the routines compress, decompress, pump, and the setup required from main()
preetpal kapoor 24-Jul-13 6:43am    
Hi Buddy!!!

I know the requirement and that code is doing the same thing which I have already done and shared . If you have something different which can work then reply otherwise shut your mouth.
from http://blogs.msdn.com/b/bclteam/archive/2005/06/15/429542.aspx[^]

C#
private static void Compress(Stream source, Stream destination)
{
    // We must explicitly close the output stream, or GZipStream will not
    // write the compression's footer to the file.  So we'll get a file, but
    // we won't be able to decompress it.  We'll get back 0 bytes.
    using(GZipStream output = new GZipStream(destination, CompressionMode.Compress)) {
        Pump(source, output);
    }
}


private static void Decompress(Stream source, Stream destination)
{
    using(GZipStream input = new GZipStream(source, CompressionMode.Decompress)) {
        Pump(input, destination);
    }
}


private static void Pump(Stream input, Stream output)
{
    byte[] bytes = new byte[4096];
    int n;
    while((n = input.Read(bytes, 0, bytes.Length)) != 0) {
        output.Write(bytes, 0, n);
    }
}


...

String inputFileName = @"fully qualified path to input file";
String outputFileName = @"fully qualified path to output file";

// no checks/tidyups done on itput/output file(s)

Stream src = null;
Stream dest = null;
dest = File.Create(outputFileName);
using(src = File.OpenRead(inputFleName)) {
    using(dest)
    {
        Decompress(src, dest);
    }
}
 
Share this answer
 
C#
// Given filePath    = location of file
//       newFileName = name of file to save
//
// Ensure you have "using System.IO.Compression"

FileInfo file = new FileInfo(filePath);

using (FileStream originalFileStream = file.OpenRead())
{
    using (FileStream outputfileStream = File.Create(newFileName))
    {
        using (GZipStream gZipStream = new GZipStream(originalFileStream,
                                                     CompressionMode.Decompress))
        {
            gZipStream.CopyTo(outputfileStream);
        }
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900