如何在C#中压缩(将zip文件大小从2 mb减小到300kb).zip文件 [英] How to compress(reduce a zip file size from 2 mb to 300kb) .zip file in C#

查看:286
本文介绍了如何在C#中压缩(将zip文件大小从2 mb减小到300kb).zip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string path = createzip(txtnum.Text.ToString());

DirectoryInfo dir = new DirectoryInfo(path);

FileInfo [] zip = dir.GetFiles (* .zip);

if(zip [0] .FullName!= null)

{

using(FileStream target = new FileStream(zip [0] .FullName,FileMode.Create,FileAccess.Write))

using(GZipStream alg = new GZipStream(target,CompressionMode.Compress))

{

byte [] data = File.ReadAllBytes(zip [0] .FullName);

alg.Write(data,0,data.Length);

alg.Flush();



}

}

解决方案

你不能,不一定 - 它取决于文件保存多少可以压缩而不会丢失数据。一些文件格式压缩得很好:例如XLSX文件是基于XML(主要是文本)的,并且往往压缩得很好,而AVI文件已经被压缩了,你可以做很多事情。



当然,你无法保证按任意数量压缩任何文件 - 由于压缩开销,压缩后某些文件可能会变大!



但如果可以,请查看 SharpZipLib [ ^ ]或类似。


我建​​议你研究无压缩和无压缩之间的区别有损压缩,并明确在特定情况下要使用哪一个。



并且,请注意,给定文件对于它有多少限制可以通过任何特定的压缩方法(Zip,WinRar,7-Zip等)无损压缩:我看到一些文件被一个无损压缩WinRar中的因子比使用Zip小25%,而其他文件只有轻微差异。



如果文件是编码的,加密的,本机压缩的(如位图一样)文件一般... .jpg,.png等,音频和视频文件等)然后进一步压缩通常是无用的。



.flac是一个无损音乐文件格式的例子,但仍然可以压缩到一定程度。



.jpg当然是一种有损压缩方法,压缩因子您可以设置的变量。 .png是无损的,除了通过改变调色板的位深度,你可以改变文件的大小:当然,如果你降低调色板的位深度,你可能会丢失颜色信息。

string path = createzip(txtnum.Text.ToString());
DirectoryInfo dir = new DirectoryInfo(path);
FileInfo[] zip = dir.GetFiles("*.zip");
if (zip[0].FullName != null)
{
using (FileStream target = new FileStream(zip[0].FullName, FileMode.Create, FileAccess.Write))
using (GZipStream alg = new GZipStream(target, CompressionMode.Compress))
{
byte[] data = File.ReadAllBytes(zip[0].FullName);
alg.Write(data, 0, data.Length);
alg.Flush();

}
}

解决方案

You can't, not necessarily - it depends on what the file holds how much it can be compressed without losing data. Some file formats compress well: XLSX files for example are XML (largely text) based and tend to compress well, while AVI files are already compressed and there isn't a lot you can do.

Certainly, you can't guarantee to compress "any file" by any amount - and some files may get larger after compression as a result of the overheads of compression!

But if it can be done, then look at SharpZipLib[^] or similar.


I suggest you study the difference between "losless compression" and "lossy compression," and get clear which one you want to use in a given situation.

And, be aware that a given file has a certain limit to how much it can be compressed losslessly by any specific method of compression (Zip, WinRar, 7-Zip, etc.): I have seen some files that losslessly compressed by a factor of 25% smaller in WinRar than with Zip, and other files that had only slight differences.

If the file is encoded, encrypted, natively compressed (as are bitmap files in general ... .jpg, .png, etc., and audio, and vidoe files, etc.) then further compression is usually useless.

.flac is an example of a music file format which is lossless, but still can compress to some degree.

.jpg is, of course, a lossy compression method, with the compression factor a variable you can set. .png is lossless, except that by altering the bit-depth of the color-palette you can change the size of the file: of course, if you lower the bit-depth of the palette you will potentially lose color information.


这篇关于如何在C#中压缩(将zip文件大小从2 mb减小到300kb).zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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