银河

SKYIV STUDIO

  博客园 :: 首页 ::  ::  :: 订阅 订阅 :: 管理 ::
  105 随笔 :: 2 文章 :: 751 评论 :: 22 Trackbacks
在FCL2.0中增加了System.IO.Compression命名空间, 用以进行文件压缩和解压操作,如下所示:
using System;
using System.IO;
using System.IO.Compression;

namespace Skyiv.Helper
{
    
static class Zip
    
{
      
public static void CompressFile(string sourceFile, string destinationFile)
      
{
        
if (!File.Exists(sourceFile)) throw new FileNotFoundException();
      
using (FileStream sourceStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read))
      
{
        
byte [] buffer = new byte[sourceStream.Length];
        
int checkCounter = sourceStream.Read(buffer, 0, buffer.Length);
        
if (checkCounter != buffer.Length) throw new ApplicationException();
          
using (FileStream destinationStream = new FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write))
          
{
            
using (GZipStream compressedStream = new GZipStream(destinationStream, CompressionMode.Compress, true))
            
{
              compressedStream.Write(buffer, 
0, buffer.Length);
            }

          }

        }

      }

    
    
public static void DecompressFile(string sourceFile, string destinationFile)
      
{
        
if (!File.Exists( sourceFile)) throw new FileNotFoundException();
      
using (FileStream sourceStream = new FileStream(sourceFile, FileMode.Open))
        
{
        
byte [] quartetBuffer = new byte[4];
        
int position = (int)sourceStream.Length - 4;
        sourceStream.Position 
= position;
        sourceStream.Read(quartetBuffer, 
04);
        sourceStream.Position 
= 0;
        
int checkLength = BitConverter.ToInt32(quartetBuffer, 0);
        
byte[] buffer = new byte[checkLength + 100];
          
using (GZipStream decompressedStream = new GZipStream(sourceStream, CompressionMode.Decompress, true))
          
{
            
int total = 0;
          
for (int offset = 0; ; )
          
{
            
int bytesRead = decompressedStream.Read(buffer, offset, 100);
            
if (bytesRead == 0break;
            offset 
+= bytesRead;
            total 
+= bytesRead;
          }

              
using (FileStream destinationStream = new FileStream(destinationFile, FileMode.Create))
              
{
                destinationStream.Write(buffer, 
0, total);
                destinationStream.Flush();
              }

        }

      }

    }

    }

}

using System;
using System.IO;
using Skyiv.Helper;

namespace Skyiv.Test
{
    
sealed class TestMain
    
{
        
static void Main()
        
{
            
try
            
{
                
string [] nameList = {"zip.zip""zip.cs""zip.txt"};
                Zip.CompressFile(nameList[
1], nameList[0]);
                Zip.DecompressFile(nameList[
0], nameList[2]);
                
foreach (string name in nameList)
                
{
                    Console.WriteLine(
"{0,-11}: {1,11:N0} bytes", name, new FileInfo(name).Length);
                }

            }

            
catch (Exception ex)
            
{
                Console.WriteLine(ex.Message);
            }

        }

    }

}

运行结果如下:
zip.zip    :         773 bytes
zip.cs     :       2,196 bytes
zip.txt    :       2,196 bytes
但是,如果有使用Zip.DecompressFile()方法去解压标准的ZIP文件,就会出现以下错误:
GZip 头中的幻数不正确。请确保正在传入 GZip 流。
此外,使用Zip.CompressFile()方法也不能将多个文件放入一个ZIP包。
看来,FCL2.0还是不能处理标准的ZIP文件,我目前是使用第三方的ICSharpZipLib来处理ZIP文件:
http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx
不知各位是使用什么方案来处理ZIP文件的?
posted on 2005-09-17 10:55 银河 阅读(4737) 评论(9)  编辑 收藏 所属分类: .NET Framework

评论

#1楼  2005-09-17 14:15 小力      
自己写文件打包类
  回复  引用  查看    

#2楼  2005-09-17 22:15 Jeky [未注册用户]
上传一个WinRar,然后调用它来在线操作。
  回复  引用    

#3楼  2005-09-18 16:09 阿良.NET [未注册用户]
@Jeky.
我写过调用WinRar压缩类,
http://chjl.cn/article/Read.aspx?ArticleID=78
但是服务器设置了一些限定,不允许调用压缩。有什么办法可以解决?

@楼主银河:我可以转帖您的这篇文章的吗?
  回复  引用    

#4楼 [楼主] 2005-09-18 17:49 银河      
@阿良.NET
可以转贴。转贴之后请在评论中贴出URL。谢谢。

  回复  引用  查看    

#5楼 [楼主] 2005-10-31 21:54 银河      
我在正文中提到:
-----------------------------------------------------------------------------------
但是,如果有使用Zip.DecompressFile()方法去解压标准的ZIP文件,就会出现以下错误:
GZip 头中的幻数不正确。请确保正在传入 GZip 流。
此外,使用Zip.CompressFile()方法也不能将多个文件放入一个ZIP包。
看来,FCL2.0还是不能处理标准的ZIP文件,我目前是使用第三方的ICSharpZipLib来处理ZIP文件
-----------------------------------------------------------------------------------
既然 FCL 2.0 中 Zip 处理不尽如人意,那么,还有一种选择,就是“通过 C# 使用 J# 类库中的 Zip 类压缩文件和数据”(Ianier Munoz 写于 2004年12月13日,有些内容已经有点过时)。前提条件是必须安装“Microsoft Visual J# Version 2.0 Redistributable Package”(Download Size: 3708 KB)。
  回复  引用  查看    

ICSharpZipLib
的帮助文档是坏的

楼主不知道能把你的发给我一份,不胜感激

我的Mail:fuxqhrb#gmail.com

顺便问一下,这个类可以实现分卷压缩吗?
  回复  引用    

#7楼 [楼主] 2006-11-04 20:51 银河      
@海东青[匿名]
ICSharpZipLib 的帮助不是坏的
可能将该文档放在目录名包括 # 的目录的下, 导致不能打开该文档.
这是 M$ 的一个 BUG, .CHM 文件在含有 # 的目录下不能正确打开.

  回复  引用  查看    

#8楼  2008-01-17 16:01 csdn2008 [未注册用户]
System.IO.Compression连100MB的文件都支持不了
根本没有什么价值
不知道楼主是否测试过大文件?


程序执行到byte [] buffer = new byte[sourceStream.Length];
就会产生内存溢出,而我机器的内存是3GB
少于100M 没有什么问题!

真正效率最高的是winrar
  回复  引用    

#9楼  2008-08-26 14:10 zjw123 [未注册用户]
文家压缩后,如果手动解压缩,解压出的文件不完成,没有文件后缀名
  回复  引用    


标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
博客园首页

新闻频道

社区

小组

博问

网摘

闪存

  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2005-10-07 18:17 编辑过
成果网帮您增加网站收入


相关链接: