DeflateStream CopyTo writes nothing and throws no exceptions

孤者浪人 提交于 2019-12-11 04:34:23

问题


I've basically copied this code sample directly from msdn with some minimal changes. The CopyTo method is silently failing and I have no idea why. What would cause this behavior? It is being passed a 78 KB zipped folder with a single text file inside of it. The returned FileInfo object points to a 0 KB file. No exceptions are thrown.

    public static FileInfo DecompressFile(FileInfo fi)
    {
        // Get the stream of the source file.
        using (FileStream inFile = fi.OpenRead())
        {
            // Get original file extension, 
            // for example "doc" from report.doc.cmp.
            string curFile = fi.FullName;
            string origName = curFile.Remove(curFile.Length
                    - fi.Extension.Length);

            //Create the decompressed file.
            using (FileStream outFile = File.Create(origName))
            {
                // work around for incompatible compression formats found
                // here http://george.chiramattel.com/blog/2007/09/deflatestream-block-length-does-not-match.html
                inFile.ReadByte();
                inFile.ReadByte();

                using (DeflateStream Decompress = new DeflateStream(inFile,
                    CompressionMode.Decompress))
                {
                    // Copy the decompression stream 
                    // into the output file.
                    Decompress.CopyTo(outFile);

                    return new FileInfo(origName);
                }
            }
        }
    }

回答1:


In a comment you say that you are trying to decompress a zip file. The DeflateStream class can not be used like this on a zip file. The MSDN example you mentioned uses DeflateStream to create individual compressed files and then uncompresses them.

Although zip files might use the same algorithm (not sure about that) they are not just compressed versions of a single file. A zip file is a container that can hold many files and/or folders.

If you can use .NET Framework 4.5 I would suggest to use the new ZipFile or ZipArchive class. If you must use an earlier framework version there are free libraries you can use (like DotNetZip or SharpZipLib).



来源:https://stackoverflow.com/questions/19165884/deflatestream-copyto-writes-nothing-and-throws-no-exceptions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!