如何压缩.net以最小的大小压缩文件 [英] how i can compress files with minimize size by .net

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

问题描述

我如何通过.net压缩具有最小大小的文件
即时通讯使用了此代码

how i can compress files with minimize size by .net
im used this code

// Read the file we are going to compress into a FileStream
            string filename = Program.x;

            FileStream infile = File.OpenRead(filename);
            byte[] buffer = new byte[infile.Length];
            infile.Read(buffer, 0, buffer.Length);
            infile.Close();

            // Create the output file
            FileStream outfile = File.Create(Path.ChangeExtension(filename, "zip"));

            // Compress the input stream and write it to the output FileStream
            GZipStream gzipStream = new GZipStream(outfile, CompressionMode.Compress);
            gzipStream.Write(buffer, 0, buffer.Length);
            gzipStream.Close();


但要给我的新文件压缩后的大小要大于文件的原始大小.原始大小为300mb,给我的新文件压缩为900mb.

还有其他方法吗?

感谢您的帮助.


but give my new file is compressed with larger than the original size of the file. Of original size is 300mb give my new file compressed is 900 mb.

There is other way?

Thanks for any help

推荐答案

您的代码可以正常工作(当然,如果您使用适当的文件名),并且当您使用文本文件之类的代码对其进行测试时,它也会压缩.可能您尝试了一些已经很好压缩的文件-GZip在压缩率方面并不是最好的.如果原始文件使用ZIP压缩,则我确认代码创建的文件更大.

顺便问一下: http://dotnetzip.codeplex.com/ [
You code works all right (of course if you take appropriate file name) and it does compress when you test it with something like a text file. Probably you tried some file which was already well compressed - GZip is not very the best at compression ratio. I confirm the code created larger file if original file was compressed with ZIP.

By the way, how about this: http://dotnetzip.codeplex.com/[^]?


如果您看一下HowTo:在 MSDN [
If you take a look at the HowTo: for this on MSDN[^] you will see that they use a slightly different technique.

Specifically:
inFile.CopyTo(gzipStream);



他们根本不使用缓冲区.

为什么不尝试一下?



They don''t use a buffer at all.

Why not give it a try?


这篇关于如何压缩.net以最小的大小压缩文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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