Click here to Skip to main content
15,886,825 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello All,

I have implemented Gzip comphression in my application,
But some places like when View document,view attached file in messages ,,it will display error message "there was an error to opening this document. The file is damaged & could not be repaired(for eg. it was sen as an email attachment & it was not correctly decoded)"

And view document not opened..

Please give me solution on that..
Posted
Updated 14-May-12 0:39am
v2
Comments
sjelen 14-May-12 6:16am    
This is very general error description. If you expect help, provide more details and some code ilustrating your Gzip implementation

1 solution

It could be because the file is not properly unzipped. There is some error in your code. Check against the examples you have already implemented.

For Compression:
C#
// Will open the file to be compressed
                FileStream fsSource;

                // Will write the new zip file
                FileStream fsDest;
                GZipStream gzCompressed;

                using (fsSource = new FileStream(aFile.FullName, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    // Open the FileStream to write to
                    string archivedFileName = Path.GetFileNameWithoutExtension(aFile.FullName);
                    archivedFileName = Path.Combine(archiveDirectory.FullName, archivedFileName + ".gz");
                    using (fsDest = new FileStream(archivedFileName, FileMode.OpenOrCreate, FileAccess.Write))
                    {
                        // Will hold the compressed stream created from the destination stream
                        using (gzCompressed = new GZipStream(fsDest, CompressionMode.Compress, true))
                        {
                            byte[] buffer = new byte[4096];
                            int numRead;
                            while ((numRead = fsSource.Read(buffer, 0, buffer.Length)) != 0)
                            {
                                gzCompressed.Write(buffer, 0, numRead);
                            }

                            // Close the streams
                            fsSource.Close();
                            gzCompressed.Close();
                            fsDest.Close();
                        }
                    }
                }


For De-COmpression:
C#
FileInfo fi = "your file";
// Get the stream of the source file.
            using (FileStream inFile = fi.OpenRead())
            {
                // Get original file extension, for example "doc" from report.doc.gz.
                string curFile = fi.FullName;
                string origName = curFile.Remove(curFile.Length - fi.Extension.Length) +".txt";

                //Create the decompressed file.
                using (FileStream outFile = File.Create(origName))
                {
                    using (GZipStream Decompress = new GZipStream(inFile,
                            CompressionMode.Decompress))
                    {
                        //Copy the decompression stream into the output file.
                        byte[] buffer = new byte[4096];
                        int numRead;
                        while ((numRead = Decompress.Read(buffer, 0, buffer.Length)) != 0)
                        {
                            outFile.Write(buffer, 0, numRead);
                        }
                    }
                }
            }
 
Share this answer
 

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