如何使用C#压缩文件及注意的问题!
2004-12-08 17:55
447 查看
首选,先要找一个开源的C#压缩组件。
如:ICSharpCode.SharpZipLib 下载地址:http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx
根据它的帮助你就可以做自己需要的东东了。
我在使用这个组件行,遇到了一个问题。
当压缩小文件时没有什么错误,一旦源文件达到150M时,它会让你的机器垮掉。(至少是我的机器)
为什么会这样,因为如果源文件是150M时,你就需要在内存申请一个150M大小的字节数组。好点的机器还没问题,一般的机器可就惨了。如果文件在大的话,好机器也受不了的。
为了解决大文件压缩的问题,可以使用分段压缩的方法。
private string CreateZIPFile(string path,int M)
{
try
{
Crc32 crc = new Crc32();
ICSharpCode.SharpZipLib.Zip.ZipOutputStream zipout=new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(System.IO.File.Create(path+".zip"));
System.IO.FileStream fs=System.IO.File.OpenRead(path);
long pai=1024*1024*M;//每M兆写一次
long forint=fs.Length/pai+1;
byte[] buffer=null;
ZipEntry entry = new ZipEntry(System.IO.Path.GetFileName(path));
entry.Size = fs.Length;
entry.DateTime = DateTime.Now;
zipout.PutNextEntry(entry);
for(long i=1;i<=forint;i++)
{
if(pai*i<fs.Length)
{
buffer = new byte[pai];
fs.Seek(pai*(i-1),System.IO.SeekOrigin.Begin);
}
else
{
if(fs.Length<pai)
{
buffer = new byte[fs.Length];
}
else
{
buffer = new byte[fs.Length-pai*(i-1)];
fs.Seek(pai*(i-1),System.IO.SeekOrigin.Begin);
}
}
fs.Read(buffer,0,buffer.Length);
crc.Reset();
crc.Update(buffer);
zipout.Write(buffer,0, buffer.Length);
zipout.Flush();
}
fs.Close();
zipout.Finish();
zipout.Close();
System.IO.File.Delete(path);
return path+".zip";
}
catch(Exception ex)
{
string str=ex.Message;
return path;
}
}
如:ICSharpCode.SharpZipLib 下载地址:http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx
根据它的帮助你就可以做自己需要的东东了。
我在使用这个组件行,遇到了一个问题。
当压缩小文件时没有什么错误,一旦源文件达到150M时,它会让你的机器垮掉。(至少是我的机器)
为什么会这样,因为如果源文件是150M时,你就需要在内存申请一个150M大小的字节数组。好点的机器还没问题,一般的机器可就惨了。如果文件在大的话,好机器也受不了的。
为了解决大文件压缩的问题,可以使用分段压缩的方法。
private string CreateZIPFile(string path,int M)
{
try
{
Crc32 crc = new Crc32();
ICSharpCode.SharpZipLib.Zip.ZipOutputStream zipout=new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(System.IO.File.Create(path+".zip"));
System.IO.FileStream fs=System.IO.File.OpenRead(path);
long pai=1024*1024*M;//每M兆写一次
long forint=fs.Length/pai+1;
byte[] buffer=null;
ZipEntry entry = new ZipEntry(System.IO.Path.GetFileName(path));
entry.Size = fs.Length;
entry.DateTime = DateTime.Now;
zipout.PutNextEntry(entry);
for(long i=1;i<=forint;i++)
{
if(pai*i<fs.Length)
{
buffer = new byte[pai];
fs.Seek(pai*(i-1),System.IO.SeekOrigin.Begin);
}
else
{
if(fs.Length<pai)
{
buffer = new byte[fs.Length];
}
else
{
buffer = new byte[fs.Length-pai*(i-1)];
fs.Seek(pai*(i-1),System.IO.SeekOrigin.Begin);
}
}
fs.Read(buffer,0,buffer.Length);
crc.Reset();
crc.Update(buffer);
zipout.Write(buffer,0, buffer.Length);
zipout.Flush();
}
fs.Close();
zipout.Finish();
zipout.Close();
System.IO.File.Delete(path);
return path+".zip";
}
catch(Exception ex)
{
string str=ex.Message;
return path;
}
}
相关文章推荐
- 某游戏公司笔试题
- 陈久霖怎样搞垮中航油 自己操盘期货期权一起上
- 中航油折戟 7家A股上市公司应警惕突然死亡出现
- 数据类型的问题
- 在Asp.Net中使用SmtpMail发送邮件的方法
- Apache2的httpd.conf翻译
- FTP搜索引擎的设计与实现
- Tomcat配置技巧Top 10
- 用PHP实现windows资源管理器风格的树型菜单
- 国内网络安全风险评估市场与技术操作
- smart client 技术
- 需求分析与企业信息架构
- 从Coding Fan到真正的技术专家
- php.ini 的必读配置中文文档
- 1. PHP数据库连接说明及例子
- JUnit介绍
- asp.net常用函数
- 全球DNS服务器更新间隔将缩短为几秒
- 需求啊需求
- 一种简单方法实现页面导出(Excel)功能