Gzip压缩错误... [英] Gzip Comphression Error...

查看:123
本文介绍了Gzip压缩错误...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我已经在我的应用程序中实现了Gzip comphression,
但是有些地方,例如,当查看文档,查看邮件中的附件时,它会显示错误消息打开此文档时出错.该文件已损坏且无法修复(例如,它被当作电子邮件附件)并且未正确解码)"

并查看未打开的文档..

请给我解决方案..

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..

推荐答案

可能是因为文件未正确解压缩.您的代码中有一些错误.对照已实现的示例进行检查.

压缩:
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:
// 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:

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);
                        }
                    }
                }
            }


这篇关于Gzip压缩错误...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆